Frist Draft (in this forum): Enum Parameters

Quirin Schroll qs.il.paperinik at gmail.com
Fri Aug 2 20:31:31 UTC 2024


On Friday, 2 August 2024 at 03:49:22 UTC, IchorDev wrote:
> On Wednesday, 31 July 2024 at 22:55:36 UTC, Quirin Schroll 
> wrote:
>> I truly wonder how. In a function template, you can’t define 
>> an `enum` locally that has the same name as a parameter. And 
>> if there’s an eponymous `enum` constant outside the function’s 
>> scope, the name of the argument refers to the argument (unless 
>> you use `with`). It says that it checks for enum *membership,* 
>> but the most relevant special case would be for simple `enum` 
>> constants, which are technically a shorthand for defining a 
>> member of an unnamed `enum`, so those are implicitly included.
>
> It could still happen in string mixins, and generally we want a 
> trait to mean one specific thing.
>
>> It’s not like it would be an error to use `isEnum` on it, it’d 
>> just return `false` instead, which IMO is just worse.
>
> Silently doing the wrong thing instead of giving an error 
> message IS worse—this is D, not JavaScript!
>
> It sounds like you intend the DIP to mean `isEnum` checks for 
> manifest constant declarations? This is really not clear in the 
> text, it sounds like you want it to check whether 
> `is(typeof(symbol) == enum)`. You could probably clarify this a 
> little better.
>
> So, does `isEnum` yield `true` for template value parameters? 
> If not, I think it’s too specific. Why not generalise it to all 
> manifest constants and call it `isManifestConstant` (or just 
> `isManifest`) instead? This could benefit meta-programming and 
> potential future language features.

I'll try to reword the DIP.

Checking if a type or an expression's type is an enemy type is 
quite easy as of now. Checking if some expression is a 
compile-time constant is not as trivial, but it's not exactly 
hard either. Checking if a symbol is a manifest constant, as far 
as I know, isn't possible. Are template value parameters manifest 
constants? Good question. They're compile-time constants, but not 
every compile-time constant is a manifest constant (e.g. `static 
immutable` values aren't), so maybe the answer is no. Literals 
aren't manifest constants either. There are many forms of 
compile-time constants.

Yes, this isn't JavaScript, but again, `isRef` doesn't check if 
it's a `ref` parameter, it checks if a symbol is a reference, and 
with the upcoming 
[v2.111](https://dlang.org/changelog/pending.html#dmd.reflocal), 
that will include local variables, possibly more. `isEnum` checks 
if something is declared using `enum`, in particular after 
inference from arguments.

The only thing that `isRef` has to contend with is that it 
doesn't detect `ref` returning functions and one could argue it 
should. But that's beside the point as Enum Parameters doesn't 
propose enum returns, whatever that would even mean.


More information about the dip.development mailing list