[nem-en] closures in constructors
Kamil Skalski
kamil.skalski at gmail.com
Sun Apr 1 11:34:44 CEST 2007
The problem is that you could anything with 'f', even store it
somewhere and call after object construction:
class A {
i : int;
problem : int -> void;
public this () {
def f (x) {
i = f;
}
problem = f;
}
Set (x : int) : void {
problem (x)
}
}
Now you could assign 'i' several times, even after A's constructor
has finished. And this is exactly why we forbid it. In your example we
could actually make it work, since 'f' is used just once and should be
inlined... but it seems that usage of [].Iter somehow confuses the
closure creating algorithm and make more conservative. So yes, this
particular part of code should work, but if you e.g. called 'f' two
times or pass it somewhere as functional value we would need to reject
it anyways.
2007/4/1, Don Reba <don_reba at inbox.ru>:
> Ran into this bug. In the example below, a function nested inside a constructor attempts to assign a class member, but fails, since it is part of a closure. If this is done inside the constructor itself, and not inside a nested function, everything is fine.
>
>
> class C
> {
> i : int;
> public this()
> {
> def f()
> {
> // assignment of a class member
> // error: assignment to immutable field is allowed only
> // inside code, which is executed inside constructor,
> // but this field access is done through closure
> i = 3;
> // creation of a closure
> mutable j;
> [].Iter(fun (_) { j = 3 });
> }
> f();
> }
> }
>
> _______________________________________________
> https://nemerle.org/mailman/listinfo/devel-en
>
--
Kamil Skalski
http://nazgul.omega.pl
More information about the devel-en
mailing list