変数から変数への代入、関数の引数の引き渡しの仕様
代入時にコピーが発生する (値渡し)
内部的には、Valueオブジェクトをコピーしてから対象の変数に渡される
アノテーションなどにより参照渡しにも変更できる
- bool
- num
- str
代入時にコピーが発生しない (参照渡し)
内部的には、Valueオブジェクトがそのまま対象の変数に渡される
値渡しには変更できない
- obj
- arr
- fn
- null
変数から変数への代入 以外にも 関数への引数引き渡し にも関わりそう
関数も変数の代入と一緒かな
【英語を話してすみません】
I think it is an issue that function arguments are currently all pass-by-reference due to the implementation. Example:
@f(y){
y +<- 1
}
$x <- 1
f(x)
<: x // num 2 (🤯)
@g(y){
y <- (y + 1)
}
x <- 1
g(x)
<: x // num 1
The example above can be fixed by cloning the function arguments before setting them in the Scope object, i.e. replace args[i] with JSON.parse(JSON.stringify(args[i])) here:
https://github.com/syuilo/aiscript/blob/1aaf444248b53c02a7e560dc0deeeeb47b8b2330/src/interpreter/index.ts#L169
I think this happens because the argument value comes from Map.prototype.get:
If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the
Mapobject. — https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
But I think that would mean pass-by-value for everything?