On the subject of properties, and possibility of having them in the library
Simen Kjaeraas
simen.kjaras at gmail.com
Sat Jan 26 06:41:25 PST 2013
While the storm raged, I decided to try implementing properties as library
types. I encountered a few obstacles, which I will outline here.
First, my intended syntax:
class A {
int _n;
Property!(
() => _n,
value => _n = value
) n;
}
Property would then be a struct, with operators and functions defined as
required, copying disabled, and of course alias this.
Now, the obstacle here is I can't refer to _n in those lambdas. Why not?
I'm guessing the struct has no context member, and the lambdas don't
because the class is not yet instantiated. Could this be fixed? I think
so, and I think this is a feature with benefits beyond simple properties.
This library solution would not be able to do everything a language
solution could. Amongst those, typeof(property) would return
Property!(..., ...). Just as important, this would not work:
auto a = foo.property;
, because the struct's postblit is marked @disable. Perhaps alias this
should be attempted in such a situation?
--
Simen
More information about the Digitalmars-d
mailing list