> On Monday, 8 April 2019, 23:01:05 BST, Vivien Kraus <[email protected]> wrote: > I have come across this weird closure thing. In other programming > languages, I would expect the closure in t to have a copy of i, but it > only has a reference. Is it expected?
That's an interesting question! If you want a copy then pass it as an argument. If you want to usea variable from the enclosing scope then use a closure, but bewareif it changes in the enclosing scope it will change in the closure. That was my initial thought. Kind of made sense until I read you expected other programming languages to make a copy. If you look at the C code generated, use --ccode with valac, youwill see a struct, _data, is created to hold the enclosed variables.It makes sense then that i, although a value type, is a reference. Now there are some rough edges with how Vala handles value typesin generics:https://gitlab.gnome.org/GNOME/vala/issues/564https://gitlab.gnome.org/GNOME/vala/merge_requests/52 If you think there is also a problem with closures then head overto https://gitlab.gnome.org/GNOME/vala/issues and make your case. Al > public delegate void Thunk(); > public static void run (Thunk t) { > t (); > } > int main() { > int i = 0; > Thunk t = () => { > stdout.printf ("%d = 0, right?\n", i); > }; > i = 1; > run (t); > return 0; > } _______________________________________________ vala-list mailing list [email protected] https://mail.gnome.org/mailman/listinfo/vala-list
