DIP proposal: Enum parameters

Quirin Schroll qs.il.paperinik at gmail.com
Wed Sep 28 19:42:19 UTC 2022


On Wednesday, 28 September 2022 at 15:36:27 UTC, Guillaume Piolat 
wrote:
> On Wednesday, 28 September 2022 at 15:21:54 UTC, Guillaume 
> Piolat wrote:
>>
>> Huh? I think this is already possible with 
>> http://p0nce.github.io/d-idioms/#Is-this-available-at-compile-time-or-runtime?
>
> and, may I say, I don't know of a single instance of anyone 
> doing that.

TL;DR: Doesn’t work for non-trivial run-time expressions.

You can do this on a *type* rather easily. Handling values is 
annoying. The DIP explains it in the Alternatives section: You 
cannot take run-time values as a template value parameter. You 
can bind run-time variables with template alias parameters, but 
then you have to ensure it’s indeed a value (with `if 
(is(typeof(x)))` usually) and not a type or some other symbol. 
Still, you cannot put `n+1` into it (unless it can be 
constant-folded).

Bonus: In D, you *have* to use the `alias` parameter even if you 
want to bind a value as a template value parameter and just infer 
its type. You have to make sure it’s a value and it’s a 
compile-time constant. (C++20 has template auto parameters for 
this.) ``void f(auto x)()`` does not parse; ``void f(T x, T)()`` 
doesn’t work, but ``void f(T)(enum T x)`` is intended to infer 
`T` given `f(value)`. ``void f(T, T x)()`` works, but it must be 
called `f!(int, 2)`.


More information about the Digitalmars-d mailing list