www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - const/immutable member functions

reply Trass3r <un known.com> writes:
class F
{
const Foo bar();
}

Isn't this ambiguous? "returns a const Foo object" vs. "is a const
member function that returns a Foo object"?
Jan 24 2011
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Trass3r <un known.com> wrote:

 class F
 {
 const Foo bar();
 }

 Isn't this ambiguous? "returns a const Foo object" vs. "is a const
 member function that returns a Foo object"?

Only to humans. const applies to everything after it, unless there are parentheses. In this case, 'everything' is Foo bar(); I do agree it is ambiguous though, and should be disallowed, or at very least, discouraged. -- Simen
Jan 24 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday 24 January 2011 05:56:49 Trass3r wrote:
 class F
 {
 const Foo bar();
 }
 
 Isn't this ambiguous? "returns a const Foo object" vs. "is a const
 member function that returns a Foo object"?

When using const or immutable in a function signature, it _always_ applies to the function, unless you use parens to say otherwise. const Foo bar(); //const function Foo bar() const; //const function immutable Foo bar(); //immutable function Foo bar() immutable; //immutable function const(Foo) bar(); //mutable function with const return value const(Foo) bar() const; //const function with const return value immutable(Foo) bar(); //mutable function with immutable return value immutable(Foo) bor() immutable; //immutable function with immutable return value And, of course, you could mix up const and immutable to have const functions with immutable return values and vice versa. Personally, I don't like it. In fact, most people don't, but that's the way it is. I always put const and immutable or the right-hand side of the function when I want the function to be const or immutable, and I wish that that were required, but it isn't. It's the way it is because it's consistent with all of the other function modifiers: property, nothrow, public, static, etc. In fact, ddoc always put them all in front of the function signature, even if you put them after. So, if you want the return value to be const or immutable, use parens. Otherwise it's the function. If you want both to be const and/or immutable, then you need to mark them both that way. - Jonathan M Davis
Jan 24 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday 24 January 2011 06:02:13 Simen kjaeraas wrote:
 Trass3r <un known.com> wrote:
 class F
 {
 const Foo bar();
 }
 
 Isn't this ambiguous? "returns a const Foo object" vs. "is a const
 member function that returns a Foo object"?

Only to humans. const applies to everything after it, unless there are parentheses. In this case, 'everything' is Foo bar();

Not quite right. The return value is _not_ const in this case. It's only the function which is affected. Try it and you'll see. The _only_ time that a return value is const or immutable is if you use parens to mark it that way. - Jonathan M Davis
Jan 24 2011
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 24 Jan 2011 09:20:17 -0500, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

 On Monday 24 January 2011 05:56:49 Trass3r wrote:
 class F
 {
 const Foo bar();
 }

 Isn't this ambiguous? "returns a const Foo object" vs. "is a const
 member function that returns a Foo object"?

When using const or immutable in a function signature, it _always_ applies to the function, unless you use parens to say otherwise. const Foo bar(); //const function Foo bar() const; //const function immutable Foo bar(); //immutable function Foo bar() immutable; //immutable function const(Foo) bar(); //mutable function with const return value const(Foo) bar() const; //const function with const return value immutable(Foo) bar(); //mutable function with immutable return value immutable(Foo) bor() immutable; //immutable function with immutable return value

You forget my favorite: const const Foo bar(); // const function returning const Foo. -Steve
Jan 24 2011
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday 24 January 2011 06:45:27 Andrej Mitrovic wrote:
 Perhaps it would be less ambiguous if we turned const/immutable for
 functions into annotations:
 
  const Foo bar(); //const function
  immutable Foo bar(); //immutable function
  immutable const(Foo) bar(); //immutable function with const return value
  const const(Foo) bar(); //const function with const return value
 immutable(Foo) bar(); //mutable function with immutable return value
  immutable immutable(Foo) bor(); //immutable function with immutable
 return value
 
 But I'm not a big fan of " ", it clutters up source code.

Well, any change to the situation would break TDPL, I believe, so we're pretty much stuck. If Walter didn't think it was bad enough to fix by now, he won't fix it post-TDPL. - Jonathan M Davis
Jan 24 2011
prev sibling next sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
Simen kjaeraas wrote:
 Trass3r <un known.com> wrote:
 
class F
{
const Foo bar();
}

Isn't this ambiguous? "returns a const Foo object" vs. "is a const
member function that returns a Foo object"?

Only to humans. const applies to everything after it, unless there are parentheses. In this case, 'everything' is Foo bar(); I do agree it is ambiguous though, and should be disallowed, or at very least, discouraged.

Very true. Preferred style is to write Foo bar() const; Jens
Jan 24 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday 24 January 2011 06:27:34 Steven Schveighoffer wrote:
 On Mon, 24 Jan 2011 09:20:17 -0500, Jonathan M Davis <jmdavisProg gmx.com>
 
 wrote:
 On Monday 24 January 2011 05:56:49 Trass3r wrote:
 class F
 {
 const Foo bar();
 }
 
 Isn't this ambiguous? "returns a const Foo object" vs. "is a const
 member function that returns a Foo object"?

When using const or immutable in a function signature, it _always_ applies to the function, unless you use parens to say otherwise. const Foo bar(); //const function Foo bar() const; //const function immutable Foo bar(); //immutable function Foo bar() immutable; //immutable function const(Foo) bar(); //mutable function with const return value const(Foo) bar() const; //const function with const return value immutable(Foo) bar(); //mutable function with immutable return value immutable(Foo) bor() immutable; //immutable function with immutable return value

You forget my favorite: const const Foo bar(); // const function returning const Foo.

That works? I would have thought that it still would have required the parens. Regardless, const and immutable on functions is not the cleanest corner of D. It's fairly easily understood and mostly doesn't result in bugs because marking the wrong thing as const usually results in a compilation error somewhere, but still, it would be nice if const and immutable had to go on the right-hand side like they do in C++. - Jonathan M Davis
Jan 24 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Perhaps it would be less ambiguous if we turned const/immutable for
functions into annotations:

 const Foo bar(); //const function
 immutable Foo bar(); //immutable function
 immutable const(Foo) bar(); //immutable function with const return value
 const const(Foo) bar(); //const function with const return value
immutable(Foo) bar(); //mutable function with immutable return value
 immutable immutable(Foo) bor(); //immutable function with immutable
return value

But I'm not a big fan of " ", it clutters up source code.
Jan 24 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 24 Jan 2011 09:39:16 -0500, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

 On Monday 24 January 2011 06:27:34 Steven Schveighoffer wrote:
 On Mon, 24 Jan 2011 09:20:17 -0500, Jonathan M Davis  
 <jmdavisProg gmx.com>

 wrote:
 On Monday 24 January 2011 05:56:49 Trass3r wrote:
 class F
 {
 const Foo bar();
 }

 Isn't this ambiguous? "returns a const Foo object" vs. "is a const
 member function that returns a Foo object"?

When using const or immutable in a function signature, it _always_ applies to the function, unless you use parens to say otherwise. const Foo bar(); //const function Foo bar() const; //const function immutable Foo bar(); //immutable function Foo bar() immutable; //immutable function const(Foo) bar(); //mutable function with const return value const(Foo) bar() const; //const function with const return value immutable(Foo) bar(); //mutable function with immutable return value immutable(Foo) bor() immutable; //immutable function with immutable return value

You forget my favorite: const const Foo bar(); // const function returning const Foo.

That works? I would have thought that it still would have required the parens.

I could have sworn it does, but on testing (which I probably should have done), it fails (even back on 2.033) with: redundant storage class const Sorry for the noise, I still find the const const(Foo) bar() very confusing to read. -Steve
Jan 24 2011
prev sibling next sibling parent Kagamin <spam here.lot> writes:
Trass3r Wrote:

 class F
 {
 const Foo bar();
 }
 
 Isn't this ambiguous? "returns a const Foo object" vs. "is a const
 member function that returns a Foo object"?

The const qualifier applies to the member being declared - the function in this case. Usually transitivity rules come into play, but they're not defined for functions.
Jan 24 2011
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Jonathan M Davis <jmdavisProg gmx.com> wrote:

 Only to humans. const applies to everything after it, unless there
 are parentheses. In this case, 'everything' is Foo bar();

Not quite right. The return value is _not_ const in this case. It's only the function which is affected. Try it and you'll see. The _only_ time that a return value is const or immutable is if you use parens to mark it that way.

I could probably have worded that more clearly. *clears throat* const applies to one, and exactly one, thing after it, matched greedily. So yes, Foo bar() is the 'everything'. Foo and Foo bar() would be two things. -- Simen
Jan 24 2011