You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
z =3f = x -> x + y + z
y =5z =100println (f 1)-- 106
z is marked as resolved and f captures its reference, so it later evaluates to 100 when f is called. y is properly hoisted such that it evaluates to 5 in the closure.
Instead, this should be a compile time failure since z is marked as immutable by f's capture.
Thanks for taking the time to put this in! Aside from hoisting, we need to document the semantics of mutability and immutability in Passerine, especially the concept of 'local mutability.' Maybe now would be a good time to start building out the Codex?
Here's a brief rundown of what should happen:
Variables are mutable in the scope in which they are defined, i.e. while on the stack.
-- this is validx =5x =4
Function calls are pass-by-value. Mutating a function parameter makes a copy[^1] and does not effect the original value:
f = x ->{ x =2; x }y =7f y -- is 2y -- is 7
Closures are capture-by-value. Mutating a captured value (inside or outside the closure) is a compile-time error:
-- compile-time error:x =7f =()->{ x =2; x }-- also:x =7f =()-> x
x =2
Variables may be shadowed:
x =7f = x ->{ x =2; x }-- shadowedf 7-- is 2x -- is 7
This is just a brief overview, I think we should provide more examples in the documentation. I'd like to include a method for closures to capture by reference in trivial cases, i.e. closures that do not escape the scope in which they are defined. I'd also like a way to introduce variables guaranteed to be immutable, through something like let x = ....
[1]: Pass-by-value does not always cause an actual copy to occur because of FBiP due to Vaporization. In the case of last-use, the original value is passed and may be mutated:
mutate_first = list ->{ list[0]=0; list }big_list =[...]big_list = mutate_first big_list -- last use of big_listprint big_list -- no copies have been made
Right now, this example prints 106:
z
is marked as resolved andf
captures its reference, so it later evaluates to100
whenf
is called.y
is properly hoisted such that it evaluates to 5 in the closure.Instead, this should be a compile time failure since
z
is marked as immutable byf
's capture.Discord thread: https://discordapp.com/channels/651996477333438474/730624907607539722/904815689154564227
The text was updated successfully, but these errors were encountered: