const challenge

Oskar Linde oskar.lindeREM at OVEgmail.com
Fri Feb 1 13:33:07 PST 2008


Sean Kelly wrote:

> Quite frankly, I'm
> shocked that the const features were considered complete with something
> like this in place.  One of these days I really need to spend some time
> with 2.0.  I'm starting to wonder if no one has any issues with it
> simply because no one's using it.

I can't help but agree. The very first thing I tried when D 2.009 was 
released was:

struct Vector { int x,y; }
invariant Vector up = {0,1};
void main() {
	Vector y = up;
}

which didn't work because:
	Error: no property 'opCall' for type 'Vector'
??? No idea what that means. Is it trying to implicitly call a 
non-existant struct static opCall "constructor"?

and changing the main body to:

	Vector y;
	y = up;

Yields:
	Error: cannot implicitly convert expression (up) of type 
invariant(Vector) to Vector

Declaring constants using the "const" keyword is of course(?) wrong, 
and: (I use the function call to illustrate the point, as just 
initializing a variable gives the weird opCall error)

const Vector c = {1,1};
void foo(invariant Vector v) {}
void main() { foo(c); }

Yields:
	Error: cannot implicitly convert expression (c) of type const(Vector) 
to invariant(Vector)

And changing that to a "manifest constant":

enum Vector c = {1,1};

Yields:
	Error: cannot implicitly convert expression (c) of type Vector to 
invariant(Vector)

So, we have three different ways of declaring constants that are not 
only incompatible with each other, but also incompatible with 
non-constants. (This is what prompted me to post the "orthogonal const 
proposal")

In my book, there are only two kinds of plain values. Constant and 
variable. Why D needs 4 kinds is beyond me. Plain constant values should 
always be implicitly convertible to non-constants. (Plain values are 
values that contain no references to other data).

Those examples only deal with plain values. Not a single reference is 
included. If this design is final and D2 can't even handle values 
properly I'm afraid there is not much attraction in D2 for me...

There are other issues with D2 const. Some I feel are quite serious and 
others mainly aesthetic, but I can't be bothered to write about them 
now. I don't even know why I bothered writing all this down. It is not 
like I have any illusions about it making any difference what so ever.

In the meantime, D1 works well and remains a great language that is a 
pleasure to use. ;)

-- 
Oskar



More information about the Digitalmars-d mailing list