Revised RFC on range design for D2
Steven Schveighoffer
schveiguy at yahoo.com
Thu Oct 2 09:53:02 PDT 2008
"Bruno Medeiros" wrote
> Andrei Alexandrescu wrote:
> > Eiffel also has properties
> > that are obtained via syntactic conventions (getX and setX implement
> > property x)
>
> Hum, interesting, I just replied in another part of the thread with a
> suggestion that is just like that (and didn't know about it).
In fact, C++.net did something similar in that if you defined a property x
in C#, it really defined two hidden functions, get_x() and set_x() that
could be called from C++.
But the new C++.net is much better, you can actually use and define real
properties.
I consider this type of thing a hack, that probably shouldn't be used since
we are talking about a developing language and not something that has
decades of real-world use. I personally think it was one of those Microsoft
marketing things where they made C++.net so it could interact with other
languages, but made it so ugly that you perceived C# as being soo much
better ;) But that's just the cynic in me.
All that is needed is a keyword to signify that a function is really a
property, kind of like 'pure', and enforce the rules that have been
discussed. Since D2 is already getting new keywords and is almost
backwards-incompatible with existing D1 code, I don't see this as a huge
issue.
The other option is to do something funky with the syntax, like C# does
(note that get, set, and value are contextual keywords in C#, so their
keyword status is not universal):
int myproperty
{
get
{
return _myprivateproperty;
}
set
{
return _myprivateproperty = value;
}
}
In light of some other posts about overloading the set operators, I think
actually it might be nicer to define set as a contextual function inside a
property:
int myproperty
{
get
{
return _myprivateproperty;
}
set(int value)
{
return _myprivateproperty = value;
}
set(string value)
{
return _myprivateproperty = to!(int)(value);
}
}
I'm not a language designer, so I don't know if this falls within the rules
of D grammar, but I definitely think this is one of the gems of C# syntax
(note, no idea if C# invented this, but it's the first time I saw it).
The downside is that you can't really take addresses of the set/get
functions, or could you with something like &myproperty.set? That would be
one reason to make the set/get symbols keywords.
-Steve
More information about the Digitalmars-d-announce
mailing list