www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - const property don't wont return const reference to value

reply "Zhenya" <zheny list.ru> writes:
Hi!
Explain me please what's wrong with this code:

module main;

struct foo
{
	int m_bar;
	 property const ref int bar() const
	{
		return m_bar;
	}
}

void main()
{
}

Error: cast(int)this.m_bar is not an lvalue
Dec 23 2012
parent reply Rainer Schuetze <r.sagitario gmx.de> writes:
On 23.12.2012 14:20, Zhenya wrote:
       property const ref int bar() const
The first const does not bind to the return type, but to the whole declaration, so it does the same as the const at the end. You should use property ref const(int) bar() const
Dec 23 2012
next sibling parent "Zhenya" <zheny list.ru> writes:
On Sunday, 23 December 2012 at 13:37:33 UTC, Rainer Schuetze 
wrote:
 On 23.12.2012 14:20, Zhenya wrote:
      property const ref int bar() const
The first const does not bind to the return type, but to the whole declaration, so it does the same as the const at the end. You should use property ref const(int) bar() const
Thank you very much!
Dec 23 2012
prev sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Sunday, 23 December 2012 at 13:37:33 UTC, Rainer Schuetze 
wrote:
 On 23.12.2012 14:20, Zhenya wrote:
      property const ref int bar() const
The first const does not bind to the return type, but to the whole declaration, so it does the same as the const at the end. You should use property ref const(int) bar() const
Shouldn't we fix this? Most of the Newcomer are confused with the fact, that there are two ways to define a function/method as const and that they have to write const(ReturnType) and not const ReturnType.
Dec 23 2012
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Namespace:

 Shouldn't we fix this? Most of the Newcomer are confused with 
 the fact,
Take a look in bugzilla. I opened an enhancement request to fix this situation, but Walter has closed it down. The rationale is to keep uniform the way D manages tags like const, immutable, etc. I don't agree with him in this case: I agree that uniformity is good, but breaking symmetry is acceptable when it just produces an error and when it helps avoid a common mistake. If you have strong desires about fixing this, then I suggest you to open a new thread about it in the main D newsgroup. I have already fought this battle and I lost. (And I think this time Jonathan was on my side). Bye, bearophile
Dec 23 2012
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Sunday, 23 December 2012 at 14:55:30 UTC, bearophile wrote:
 Namespace:

 Shouldn't we fix this? Most of the Newcomer are confused with 
 the fact,
Take a look in bugzilla. I opened an enhancement request to fix this situation, but Walter has closed it down. The rationale is to keep uniform the way D manages tags like const, immutable, etc. I don't agree with him in this case: I agree that uniformity is good, but breaking symmetry is acceptable when it just produces an error and when it helps avoid a common mistake. If you have strong desires about fixing this, then I suggest you to open a new thread about it in the main D newsgroup. I have already fought this battle and I lost. (And I think this time Jonathan was on my side). Bye, bearophile
I have to write "const (ReturnType)" and "ref ReturnType" ... very symetric. But I think that Walter thinks that this is the best idea, and of course better than that what C++ and other languages do​​. So what should I waste my time with "war" with my limited english knowledge ? ;) But thank you, that you tried it. Maybe this is something for Romulus... :)
Dec 23 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Namespace:

 So what should I waste my time with "war" with my limited 
 english knowledge ? ;)
 But thank you, that you tried it.
Your English improves if you want it, and you exercise yourself. And such "wars" are not always useless, because while Walter is very experienced and usually strong willed, he isn't always right. In several situations we/I have eventually changed his mind for the better. Bye, bearophile
Dec 23 2012
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Sunday, 23 December 2012 at 15:05:16 UTC, Namespace wrote:
 On Sunday, 23 December 2012 at 14:55:30 UTC, bearophile wrote:
 Namespace:

 Shouldn't we fix this? Most of the Newcomer are confused with 
 the fact,
Take a look in bugzilla. I opened an enhancement request to fix this situation, but Walter has closed it down. The rationale is to keep uniform the way D manages tags like const, immutable, etc. I don't agree with him in this case: I agree that uniformity is good, but breaking symmetry is acceptable when it just produces an error and when it helps avoid a common mistake. If you have strong desires about fixing this, then I suggest you to open a new thread about it in the main D newsgroup. I have already fought this battle and I lost. (And I think this time Jonathan was on my side). Bye, bearophile
I have to write "const (ReturnType)" and "ref ReturnType" ... very symetric. But I think that Walter thinks that this is the best idea, and of course better than that what C++ and other languages do​​. So what should I waste my time with "war" with my limited english knowledge ? ;) But thank you, that you tried it. Maybe this is something for Romulus... :)
"ref" is a function qualifier, not a type qualifier, so you could even write it as: "const(ReturnType) foo() const ref;" So technically, it is symetric. I for one don't think this is a huge problem. There is *some* confusion for those comming from C++, but they (we) have to learn D is not C++. Besides, I like the liberty of putting all qualifiers on the line before the types, including const. If anything "const" *itself* is VERY confusing for new commers. WAY more than the syntax used to qualify a function with it.
Dec 24 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, December 24, 2012 14:32:55 monarch_dodra wrote:
 "ref" is a function qualifier, not a type qualifier, so you could
 even write it as:
 "const(ReturnType) foo() const ref;"
 
 So technically, it is symetric.
I'm pretty sure that ref is nonsensical in that example. What would ref on a function even mean? It could be used on a function pointer, but then you'd have to have the function keyword in there. It makes no sense on a function prototype like you have there. If that compiles, it's because dmd allows you to put all kinds of modifiers on symbols where the modifier has no effect and arguably shouldn't be legal.
 I for one don't think this is a huge problem. There is *some*
 confusion for those comming from C++, but they (we) have to learn
 D is not C++. Besides, I like the liberty of putting all
 qualifiers on the line before the types, including const.
 
 If anything "const" *itself* is VERY confusing for new commers.
 WAY more than the syntax used to qualify a function with it.
You're likely to be told to move the const to the right if one of us notices it on the left in a pull request. I believe that quite a few of us consider it to be bad practice to put it on the left. - Jonathan M Davis
Dec 24 2012
parent reply "Monarch Dodra" <monarchdodra gmail.com> writes:
On Monday, 24 December 2012 at 15:52:01 UTC, Jonathan M Davis 
wrote:
 On Monday, December 24, 2012 14:32:55 monarch_dodra wrote:
 "ref" is a function qualifier, not a type qualifier, so you 
 could
 even write it as:
 "const(ReturnType) foo() const ref;"
 
 So technically, it is symetric.
I'm pretty sure that ref is nonsensical in that example. What would ref on a function even mean? It could be used on a function pointer, but then you'd have to have the function keyword in there. It makes no sense on a function prototype like you have there. If that compiles, it's because dmd allows you to put all kinds of modifiers on symbols where the modifier has no effect and arguably shouldn't be legal.
Wait, never mind that. I thought that was legal. I was mentioning the fact that if you want to know if a function returns ref, you have check that function's attributes: http://dlang.org/phobos/std_traits.html#FunctionAttribute
 I for one don't think this is a huge problem. There is *some*
 confusion for those comming from C++, but they (we) have to 
 learn
 D is not C++. Besides, I like the liberty of putting all
 qualifiers on the line before the types, including const.
 
 If anything "const" *itself* is VERY confusing for new commers.
 WAY more than the syntax used to qualify a function with it.
You're likely to be told to move the const to the right if one of us notices it on the left in a pull request. I believe that quite a few of us consider it to be bad practice to put it on the left. - Jonathan M Davis
That's the way the ddoc is generated anyways: Everything on the left.
Dec 24 2012
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, December 24, 2012 17:25:05 Monarch Dodra wrote:
 That's the way the ddoc is generated anyways: Everything on the
 left.
Which should probably be fixed IMHO. Regardless, in Phobos or druntime, if const is found on the left, it gets moved to the right. - Jonathan M Davis
Dec 24 2012
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, December 23, 2012 15:41:09 Namespace wrote:
 On Sunday, 23 December 2012 at 13:37:33 UTC, Rainer Schuetze
 
 wrote:
 On 23.12.2012 14:20, Zhenya wrote:
      property const ref int bar() const
The first const does not bind to the return type, but to the whole declaration, so it does the same as the const at the end. You should use property ref const(int) bar() const
Shouldn't we fix this? Most of the Newcomer are confused with the fact, that there are two ways to define a function/method as const and that they have to write const(ReturnType) and not const ReturnType.
No one has been able to convince Walter. He thinks that the consistency of allowing function attributes on both sides trumps fixing the problems caused by having const or immutable on the left. - Jonathan M Davis
Dec 23 2012
parent reply "Namespace" <rswhite4 googlemail.com> writes:
 No one has been able to convince Walter. He thinks that the 
 consistency of
 allowing function attributes on both sides trumps fixing the 
 problems caused by
 having const or immutable on the left.

 - Jonathan M Davis
It is more confusing than anything else but as I said I would waste my time with convince Walter. What is your opinion?
Dec 23 2012
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, December 23, 2012 21:34:25 Namespace wrote:
 No one has been able to convince Walter. He thinks that the
 consistency of
 allowing function attributes on both sides trumps fixing the
 problems caused by
 having const or immutable on the left.
 
 - Jonathan M Davis
It is more confusing than anything else but as I said I would waste my time with convince Walter. What is your opinion?
On which? Whether it should be changed or whether it's worth your time trying to convince Walter? I'd definitely argue that putting const and immutable on the left should be made illegal. The consistency is not worth the problems that it causes. But I doubt that there's much hope of convincing Walter at this point. The fact that newbies screw it up all the time clearly isn't enough to convince him, or he would have relented in the past. And that's really the only argument AFAIK, since if you know what you're doing, it's trivial to avoid the problem. - Jonathan M Davis
Dec 23 2012