DIP proposal: Enum parameters

Quirin Schroll qs.il.paperinik at gmail.com
Tue Sep 27 13:42:55 UTC 2022


On Tuesday, 27 September 2022 at 11:52:03 UTC, bauss wrote:
> I am really confused about how it differs from what we have now 
> with templates.

Calling syntax: “The difference between enum parameters `and` 
template value parameters is only in calling syntax: 
`f!ct_value(args)` versus `f(ct_value, args)`” And that 
difference matters for meta-programming and operator rewrites.

> Like how is this:
>
> ```d
> auto opSlice()(enum size_t l, enum size_t u) => slice!(l, u);
> ```
>
> An improvement over:
>
> ```d
> auto opSlice(size_t l, size_t u)() => slice!(l, u);
> ```
>
> Maybe I am not entirely grasping this concept or something.

**TL;DR:** Because the former one works for an operator rewrite 
while the latter does not, and changing that in a consistent way 
is very likely a breaking change. ― *end TL;DR*

You can call both explicitly, i.e. `tup.opSlice!(0, 2)` will 
perfectly compile, but that’s not why we defined `opSlice`. The 
syntax `tup[l .. u]` rewrites to the first one of those that 
compiles:
1. `tup.opIndex(tup.opSlice!0(l, u))`
2. `tup.opSlice(l, u)`

What is not in the list is `tup.opSlice!(l, u)()` which currently 
would have to be for it to work, but probably cannot without 
breaking code.

It’s a bit long, but if you take a look at [Operator Overloading 
§ Array Indexing and Slicing Operators 
Overloading](https://dlang.org/spec/operatoroverloading.html#array-ops), you’ll notice that the only template parameters involved are supplying `opDollar` and `opSlice` (the one that [rewrites the `l..u` pseudo-subexpression](https://dlang.org/spec/operatoroverloading.html#slice) when indexing) with the position in the argument list they appeared in.

-----

In 2020, I drafted a DIP for [Compile-time Indexing 
Operators](https://github.com/Bolpat/DIPs/blob/StaticIndexingOperators/DIPs/DIP-1NN1-QFS.md), which is a different feature with a wild range of consequences if thought to its conclusion, among which is spreading the indexable sequence and overload spreading in and of itself. In its *Alternative* section, [§ Compile-time Function Parameters](https://github.com/Bolpat/DIPs/blob/StaticIndexingOperators/DIPs/DIP-1NN1-QFS.md#compile-time-function-parameters), I’ve already outlined the `enum` parameters DIP and that a large portion of static indexing would be superseded by `enum` parameters.

Note that the draft is older and when writing something, I rather 
put it in than leave it out as after review, stuff can always be 
deleted, but a thought never put down may be gone forever.


More information about the Digitalmars-d mailing list