const challenge
Sean Kelly
sean at f4.ca
Fri Feb 1 08:17:10 PST 2008
Janice Caron wrote:
> On Feb 1, 2008 4:55 AM, Kris <foo at bar.com> wrote:
>> Steve - some D2 questions for ya:
>>
>> Taking an example related to the
>> discussion, and if I understand correctly, the variations on Unique!(T) are
>> little more than attempts to hide a cast() operation under a sugary coating,
>> right?
>
> Yes.
>
>
>> A need for such sugar in the language syntax would surely tend to
>> indicate a notable flaw?
>
> It indicates a difficulty, yes. If something is invariant(T), then it
> /may/ be in a read-only segment, and so modifying it is undefined.
> That's fair enough. The thing is though that one can create a buffer
> full of stuff on the heap or the stack and then cast it to invariant.
> (That's what assumeUnique() does - it's more or less template sugar
> for cast(invariant)). Now you can pass all those objects to functions
> that take invariants, no problem. (At least, no problem if you don't
> mind explicit casts. Some people object to them).
>
> Unfortunately, it does have a negative consequence, which is
> exemplified in concatenation. To get a mutable buffer when
> concatenating, you have to do something like this:
>
> string s;
> char[] t;
> char[] buffer = cast(char[])("hello " ~ s ~ cast(invariant)t);
>
> This is known to be safe. Unfortunately, the D specification says that
> casting away invariance is undefined, so strictly speaking, one
> shouldn't do it. Perhaps, in the future, Andrei may come up with some
> clever library template to hide the cast, but it will still be there,
> under the hood.
That's ridiculous. Is there an implicit cast here as well:
int x = 1 + 2;
And does the compiler complain about this:
int y = 3 + x;
This second example is exactly the same as your example above:
char[] buffer = "hello " ~ t;
Requiring casts here seems like a blatant flaw in the current design.
I'm aware of the counter-example Andrei posted, but that doesn't change
the fact that the above should have always worked. 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.
Sean
More information about the Digitalmars-d
mailing list