What am I missin with const?

deadalnix deadalnix at gmail.com
Mon Jan 28 23:37:32 PST 2013


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.


More information about the Digitalmars-d mailing list