read-only access

Jesse Phillips jessekphillips+D at gmail.com
Tue Nov 2 12:54:13 PDT 2010


spir Wrote:

> Hello,
> 
> Is the D way to make read-only symbols (of a class, struct, module) to write a getter for a private symbols?

Yes, though it isn't like the getValue() from java.

@property  int i() {return this.my_i;}

> Additional question: just realised one can omit () on func calls! Is this systematic when a func has no param? I thought it was not the case, because it does not work with writeln (the first func on which I tried, indeed!). Is it only because writeln is parameterized (with (T...)) or is there any other reason I'm not aware of?

Calling functions without () is a legacy feature left over which is how D use to do properties. Some will say this is going to be removed, but do not recall this ever being said (only that it was implied it would happen if properties were used).

Personally if it doesn't go away I see no reason to have @property, as the only reason I thought it was a good idea was for when you were taking the address of a function who returns a function (makes it a little hard to read). Otherwise I like the omit-able ().

writeln is a template, so I suppose you can't call it this way since it can't instantiate an instance until it knows what parameters are past. You probably could call it like this writeln!() but that kind of defeats the idea of leaving off the ().


More information about the Digitalmars-d-learn mailing list