PhobosWatch: manifest => enum

Christopher Wright dhasenan at gmail.com
Mon Dec 31 19:55:57 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<--------------------

You basically want this to fail:
---
enum int foo = 5;

template Something (alias arg) {
    // ...
}

Something!(foo);
---

Since it's the equivalent of:
---
template Something (alias arg) {
    // ...
}

Something!(5);
---

And that would be a substitution failure, hopefully with a special error 
message telling you it's a compile-time constant so you can't use alias 
with it. The workaround being, assign it and then pass it.

I don't know of a situation where it would be advantageous to use an 
alias template parameter where a compile-time constant would make sense.



More information about the Digitalmars-d mailing list