enums and version/static if/"inheritance"
IchorDev
zxinsworld at gmail.com
Wed Jul 31 03:15:30 UTC 2024
On Wednesday, 31 July 2024 at 00:50:59 UTC, Witold Baryluk wrote:
> All nice and dandy. I know how to write mixins, there are many
> ways to do it.
I am just providing an existing solution that is tailored to
language bindings and should see extensive use in the near future
and will therefore be less likely to have bugs in the long run.
> I am just surprised language feature of `version` and `static
> if` that work so easily in many other places, cannot be used
> for anything useful in enums and enums only.
Judging by the replies to [my DIP ideas
thread](https://forum.dlang.org/thread/gkvhaaqrnbizhacpikde@forum.dlang.org), I think it’s partly an issue with allowing a declaration in the middle of one type of comma-separated list when it is not permitted in all others:
```d
int[] x = [1,2,3, version(X) 4 else 0];
void fn(int a, version(X) int b, int c){}
```
The options are really:
1. We make a way of declaring enums with statements instead of a
list, much like in Swift, which would also allow things like enum
member functions; or
2. We allow for conditional compilation declarations in all comma
separated lists. I don’t see much use of this for function
parameters outside of language bindings, but with array literals
this could alleviate having to laboriously concatenate many items
together, and with associative array literals this would be
**very** useful since you can’t just concatenate them easily.
I’d love to see if there’s any interest in either of those
possibilities.
> Usually one would not want to define an enum that have
> different values and enum members, as this can mess other code.
> But then it is pretty normal when interfacing with other
> languages and systems, or even doing things like changing some
> default values depending on a version.
Yes, it’s mostly needed for making language bindings, so that
means that most people don’t understand why you’d even want the
feature; but as someone who creates a lot of language bindings, I
absolutely see the utility.
> I do like idea of the struct with static enum members. It might
> just work, without CTFEs and mixins. I will give it a try.
It’s a clever workaround but won’t work the way people want with
a lot of enum-based meta-programming (e.g. `std.conv.to`). You
should also `@disable this();`.
More information about the Digitalmars-d
mailing list