Omittable parens is an evil
Nick Sabalausky
a at a.a
Sat Jul 19 12:42:22 PDT 2008
"Mike" <vertex at gmx.at> wrote in message news:op.uejk9fs8kgfkbn at lucia...
> On Sat, 19 Jul 2008 15:32:36 +0200, Koroskin Denis <2korden+dmd at gmail.com>
> wrote:
>
>> Why not have special syntax for properties, like:
>
> This has come up multiple times - that's one of the few things where C#
> wins over D. If I may repeat a suggestion I made once, maybe Walter can be
> hypnotized into implementing it if I just repeat it often enough :)
>
> class foo
> {
> private int _bar;
> property int bar
> {
> opGet() { return _bar; }
> opSet(auto value) { _bar = value; }
> opAddAssign(auto value) { _bar += value; } // it's extremely
> extendable!
> }
> }
>
> -Mike
I'm just kind of brainstorming possible improvements to the usual way of
doing properties. What if the above were adjusted into something like this
(there's a few things I've done differently here):
class foo
{
// Similar to how classes define an implict member "this",
// "property int bar" defines an implicit "private int bar.value"
// (Or maybe "protected int bar.value"? Or maybe adjustable somehow?).
// This 1, elimininates the need to manually create "private _bar"
// and 2, follows in the spirit of naming all constructors "this"
// instead of having a different ctor name for each class
// (and therefore achieves the same benefits - such as easier renaming).
property int bar
{
// "get" and "set" can each be set separately to "public",
"private", "protected"
// (Maybe "protected" would be useless, though? Depends if these
should
// be overrideable.)
get { return value; }
set { value = set; } // The rvalue "set" is an implied param, and
// works just like the implied "value" in
C#'s setters.
// Never needed, but possibly allowed just for things like
performance.
opAddAssign(auto addend) { value += addend; }
}
private func()
{
Stdout.formatln("{}", this.bar); // Use property
Stdout.formatln("{}", this.bar.value); // Sidestep property
}
}
void main()
{
int x;
auto f = new foo();
f.bar = 5; // ok
x = f.bar; // ok
f.func(); // ok, displays "5" twice
f.bar.value = 7; // Illegal, "bar.value" is private to "foo"
x = f.bar.value; // Illegal, "bar.value" is private to "foo"
}
More information about the Digitalmars-d
mailing list