View mode: basic / threaded / horizontal-split · Log in · Help
November 02, 2010
read-only access
Hello,

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

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?

struct P {
   private int my_i;
   int i() {return this.my_i;}
   void doit() {writeln("...doing...");}
}
void main () {
   auto p = P(1);
   writeln("p.i: ",p.i);
   p.doit;
}


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com
November 02, 2010
Re: read-only access
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 ().
November 02, 2010
Re: read-only access
On Tue, 02 Nov 2010 15:54:13 -0400, Jesse Phillips  
<jessekphillips+D@gmail.com> wrote:

> 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).

TDPL states it will be required.

-Steve
November 02, 2010
Re: read-only access
Steven Schveighoffer Wrote:

> On Tue, 02 Nov 2010 15:54:13 -0400, Jesse Phillips  
> <jessekphillips+D@gmail.com> wrote:
> 
> > 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).
> 
> TDPL states it will be required.
> 
> -Steve

I do not believe it does. I don't recall seeing it, nor do I think it makes any sense to even mention it considering it shouldn't mention bugs in the implementation. That said, I don't recall it mentioning functions being callable without parens.
November 02, 2010
Re: read-only access
On Tue, 02 Nov 2010 16:52:38 -0400, Jesse Phillips  
<jessekphillips+D@gmail.com> wrote:

> Steven Schveighoffer Wrote:
>
>> On Tue, 02 Nov 2010 15:54:13 -0400, Jesse Phillips
>> <jessekphillips+D@gmail.com> wrote:
>>
>> > 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).
>>
>> TDPL states it will be required.
>>
>> -Steve
>
> I do not believe it does. I don't recall seeing it, nor do I think it  
> makes any sense to even mention it considering it shouldn't mention bugs  
> in the implementation. That said, I don't recall it mentioning functions  
> being callable without parens.

It does not say that functions should be callable without parentheses,  
because that mis-feature is deprecated.  Essentially, to someone who is  
new to D, there is no need to mention the historical features of D.  It  
does say that you need to put @property on functions to make them into  
properties.  At least the copy I reviewed said that (I don't have a final  
copy).

-Steve
November 02, 2010
Re: read-only access
Steven Schveighoffer Wrote:

> It does not say that functions should be callable without parentheses,  
> because that mis-feature is deprecated.  Essentially, to someone who is  
> new to D, there is no need to mention the historical features of D.  It  
> does say that you need to put @property on functions to make them into  
> properties.  At least the copy I reviewed said that (I don't have a final  
> copy).
> 
> -Steve

I think part of my distrust is I recall getting an error message to the effect of:

Error: cannot call property function using ()

But I'm not getting any such error now, so I don't know what I was fixing.
November 03, 2010
Re: read-only access
On Tuesday 02 November 2010 15:02:04 Jesse Phillips wrote:
> Steven Schveighoffer Wrote:
> > It does not say that functions should be callable without parentheses,
> > because that mis-feature is deprecated.  Essentially, to someone who is
> > new to D, there is no need to mention the historical features of D.  It
> > does say that you need to put @property on functions to make them into
> > properties.  At least the copy I reviewed said that (I don't have a final
> > copy).
> > 
> > -Steve
> 
> I think part of my distrust is I recall getting an error message to the
> effect of:
> 
> Error: cannot call property function using ()
> 
> But I'm not getting any such error now, so I don't know what I was fixing.

Properties are discussed on page 156 of TDPL. It specifically states that 
property functions _must_ be called without parentheses but says nothing about 
whether you can call other functions without them - presumably because it's 
assumed that the way to call a function is with parens and that being able to 
call non-property functions without parens is not supposed to be in the language 
anymore, so there's no point in mentioning it.

- Jonathan M Davis
November 03, 2010
Re: read-only access
On Tue, 02 Nov 2010 18:02:04 -0400, Jesse Phillips  
<jessekphillips+D@gmail.com> wrote:

> Steven Schveighoffer Wrote:
>
>> It does not say that functions should be callable without parentheses,
>> because that mis-feature is deprecated.  Essentially, to someone who is
>> new to D, there is no need to mention the historical features of D.  It
>> does say that you need to put @property on functions to make them into
>> properties.  At least the copy I reviewed said that (I don't have a  
>> final
>> copy).
>>
>> -Steve
>
> I think part of my distrust is I recall getting an error message to the  
> effect of:
>
> Error: cannot call property function using ()
>
> But I'm not getting any such error now, so I don't know what I was  
> fixing.

IIRC, the compiler has some enforcement but not complete enforcement.

I would not trust the compiler.  The intent is for omission of parentheses  
to be required for, and valid only for @property functions.

-Steve
Top | Discussion index | About this forum | D home