Frist Draft (in this forum): Enum Parameters

Timon Gehr timon.gehr at gmx.ch
Mon Apr 29 13:39:57 UTC 2024


On 4/25/24 19:56, Quirin Schroll wrote:
> https://github.com/Bolpat/DIPs/blob/2bfef0b05e99c5448b416078da2e743482e3193d/DIPs/1NNN-QFS.md
> 
> This supersedes my idea of static indexing. The static indexing DIP 
> draft even said that if some way to pass values as compile-time 
> constants to functions, that would supersede it.
> 
> **Abstract**
> On function templates, allow `enum` to be used as a function parameter 
> storage class and a member function attribute. Arguments binding to 
> `enum` parameters must be compile-time constants, as if template value 
> parameters. With `auto enum`, “compile-time-ness” is determined from 
> argument (cf. `auto ref`) and queried via a trait.
> 

Nice, thanks! However, I think the technical part will need some more 
elaboration.

> if candidates contain enum parameters, constant folding must be attempted for them, i.e. a candidate can only be excluded when an enum parameter is bound to an argument for which constant folding failed.

I am not sure what you mean by constant folding. Do you mean CTFE has to 
be attempted? What are possible reasons for it to fail? E.g., if it 
throws an exception, will it match a runtime parameter instead?

> In the function body (including contracts and constraints), an enum parameter’s value is a compile-time constant as if it were template value parameter.

But it is not, so probably you have to specify some sort of lowering 
and/or name mangling strategy. Also, is there a way to manually 
instantiate the template?

The idea I had (that I am not yet fully satisfied with) to bypass all of 
this was to require specific values for `enum` parameters. Then you'd do:

```d
auto opSlice(size_t l, size_t u)(enum : l, enum : u) => slice!(l, u);
```

(Maybe there is better syntax. Mine is inspired from template 
specializations.)

This way, you can manually instantiate the templates if you need to, and 
you also do not add a new category of symbol that requires updating the 
way D mangles names.




More information about the dip.development mailing list