PhobosWatch: manifest => enum
Bill Baxter
dnewsgroup at billbaxter.com
Mon Dec 31 02:29:00 PST 2007
Jérôme M. Berger wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Walter Bright wrote:
>> Jérôme M. Berger wrote:
>>> Walter Bright wrote:
>>>> Yes, that could be done, but we're still stymied by the problem that we
>>>> are unable to declare a constant of type 'int', only 'const(int)'.
>>> I don't see any situation in which we would need a constant of type
>>> "int" instead of "const (int)" or "invariant (int)". After all, if
>>> it is a *constant*, it should be either "const" or "invariant", no?
>> Consider the following:
>>
>> const int X = 3;
>> auto i = X;
>> i = 4; // error, i is const
>>
>> Essentially, it would make type inference far less useful.
>
> OTOH, having something that's a constant but has type "int" instead
> of "const (int)" or "invariant (int)" could cause problems with
> generic programming. For example with something like this:
>
> - -------------------->8====================
>
> static if (is (typeof (x) : int)
> x = 42;
> else static if ((is (typeof (x) : const (int))
> || (is (typeof (x) : invariant (int)))
> processImmutableInts();
> else
> static assert (0, "Can only handle ints");
>
> ====================8<--------------------
>
> If "x" is in fact a manifest constant, this code will think it is
> mutable and try to assign to it but the compiler will then refuse
> the assignment...
Thanks for mentioning that. That's what I was thinking too. Walter
said something about how literals are plain types, and it works fine
because you can't assign to them since they aren't lvalues. But it
seems like for generic programming it would be more useful if literals
did get typed as const. Unless there's some other obvious way to test
for lvalueness.
--bb
More information about the Digitalmars-d
mailing list