View mode: basic / threaded / horizontal-split · Log in · Help
January 29, 2013
What am I missin with const?
Hi,

Just started in D a week back after years of C/C++ head bashing 
and have to say language is fantastic.

I have a small misunderstanding regarding const...well ok, a 
fundamental misunderstanding by the looks of it:)

class A
{

ulong[] values;
...
const ulong[] getValue() const {
    return this.values;
}

}

But this fails to compile with the following message:
Error: cannot implicitly convert expression (this.values) of type 
const(ulong[]) to ulong[]

I just do not see it in the code. The function is const, so 
this.values is const, and I am just returning that as a const 
ulong[]. Am I not???


I don't get it? Any help explaining why this does not compile 
would be appreciated!

Thanks,
Stewart
January 29, 2013
Re: What am I missin with const?
Sorry all, I just need to read the docs more carefully and do 
more homework! Especially the section "Immutable Member 
Functions" under Const and Immutable.


...couldn't be more obvious :-)

Cheers,
Stewart
January 29, 2013
Re: What am I missin with const?
On Tuesday, 29 January 2013 at 05:36:31 UTC, estew wrote:
> Hi,
>
> Just started in D a week back after years of C/C++ head bashing 
> and have to say language is fantastic.
>
> I have a small misunderstanding regarding const...well ok, a 
> fundamental misunderstanding by the looks of it:)
>
> class A
> {
>
> ulong[] values;
> ...
> const ulong[] getValue() const {
>     return this.values;
> }
>
> }
>
> But this fails to compile with the following message:
> Error: cannot implicitly convert expression (this.values) of 
> type const(ulong[]) to ulong[]
>
> I just do not see it in the code. The function is const, so 
> this.values is const, and I am just returning that as a const 
> ulong[]. Am I not???
>
>
> I don't get it? Any help explaining why this does not compile 
> would be appreciated!
>

Hi,

This is a very common syntax trap where too many people fall. 
When you do const ulong[] getValue() const both const are 
qualifying this, and none the return value. So the error.

The correct declaration is :
const(ulong)[] getValue() const

or

const(ulong[]) getValue() const

I wish this syntax will be fixed, but I have no hope. We just 
will see the problem popping again and again I guess.
January 29, 2013
Re: What am I missin with const?
deadalnix:

> I wish this syntax will be fixed, but I have no hope. We just 
> will see the problem popping again and again I guess.

I asked for that fix, but Walter closed down that request in 
Bugzilla. I still think he was wrong.

I now open a smaller request that can't fix the problem fully:
http://d.puremagic.com/issues/show_bug.cgi?id=9422

Bye,
bearophile
January 29, 2013
Re: What am I missin with const?
Could you link to the issue Walter closed down from issue 9422?

Kenji Hara


2013/1/29 bearophile <bearophileHUGS@lycos.com>

> deadalnix:
>
>
>  I wish this syntax will be fixed, but I have no hope. We just will see
>> the problem popping again and again I guess.
>>
>
> I asked for that fix, but Walter closed down that request in Bugzilla. I
> still think he was wrong.
>
> I now open a smaller request that can't fix the problem fully:
> http://d.puremagic.com/issues/**show_bug.cgi?id=9422<http://d.puremagic.com/issues/show_bug.cgi?id=9422>
>
> Bye,
> bearophile
>
January 29, 2013
Re: What am I missin with const?
On Tuesday, 29 January 2013 at 09:10:28 UTC, bearophile wrote:
> deadalnix:
>
>> I wish this syntax will be fixed, but I have no hope. We just 
>> will see the problem popping again and again I guess.
>
> I asked for that fix, but Walter closed down that request in 
> Bugzilla. I still think he was wrong.
>
> I now open a smaller request that can't fix the problem fully:
> http://d.puremagic.com/issues/show_bug.cgi?id=9422
>

Well I think the error message you propose isn't possible as it 
would cause many problems in generic code.

const applied to the rturn type should be the way to go. It is 
basically what everybody except at first when looking at the 
code. But it is probably too late to change that.
January 29, 2013
Re: What am I missin with const?
On 01/29/2013 10:38 AM, deadalnix wrote:
> On Tuesday, 29 January 2013 at 09:10:28 UTC, bearophile wrote:
>> deadalnix:
>>
>>> I wish this syntax will be fixed, but I have no hope. We just will
>>> see the problem popping again and again I guess.
>>
>> I asked for that fix, but Walter closed down that request in Bugzilla.
>> I still think he was wrong.
>>
>> I now open a smaller request that can't fix the problem fully:
>> http://d.puremagic.com/issues/show_bug.cgi?id=9422
>>
>
> Well I think the error message you propose isn't possible as it would
> cause many problems in generic code.
>

How?

> const applied to the rturn type should be the way to go. It is basically
> what everybody except at first when looking at the code. But it is
> probably too late to change that.

The current behaviour emerges because the parser treats

const void foo(){}

exactly like

const{
    void foo(){}
}
January 29, 2013
Re: What am I missin with const?
kenji hara:

> Could you link to the issue Walter closed down from issue 9422?

I have taken part of that discussion thread, but the bug report 
was created by Simen Kjaeraas:

http://d.puremagic.com/issues/show_bug.cgi?id=4070

(Maybe in Bugzilla there is another similar closed issue, because 
I vaguely remember Jonathan commenting on it.)

Bye,
bearophile
January 29, 2013
Re: What am I missin with const?
deadalnix:

> Well I think the error message you propose isn't possible as it 
> would cause many problems in generic code.

Please, add one or two of such troubled cases in the issue thread:

http://d.puremagic.com/issues/show_bug.cgi?id=9422

Bye,
bearophile
January 29, 2013
Re: What am I missin with const?
On Tuesday, 29 January 2013 at 09:50:37 UTC, bearophile wrote:
> deadalnix:
>
>> Well I think the error message you propose isn't possible as 
>> it would cause many problems in generic code.
>
> Please, add one or two of such troubled cases in the issue 
> thread:
>
> http://d.puremagic.com/issues/show_bug.cgi?id=9422
>
> Bye,
> bearophile

For what it's worth, I think 9422 is better than 4070. You rarely 
see const on the return type without also putting it on the 
function too, so this should catch *most* accidental usage.

It also still allows the syntax for those that enjoy qualifying 
everything on the previous line:
//----
pure @property @safe const
int foo()
{
    ......
}
//----
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home