enums and version/static if/"inheritance"
IchorDev
zxinsworld at gmail.com
Tue Jul 30 23:26:58 UTC 2024
I’ve recently made a API for doing this in BindBC-Common. You can
see it on the latest commit of my repository
[here](https://github.com/BindBC/bindbc-common/blob/f9b30591e05238fcda80bb6b070acacd1f77f018/source/bindbc/common/codegen.d#L500). This is not tagged as a version yet, but you can download it & use `dub add-local` until I tag it.
It’s designed for making language bindings (mostly to C) so
there’s a convenient way to have a D version of the enum (e.g.
`Plant.flower`) and a C version (e.g. `PLANT_FLOWER`) with the
same code. And you can turn them on and off easily so that people
who don’t like one of the styles are still happy.
Usage is like…
```d
import bindbc.common;
mixin(makeEnumBindFns(cStyle: true, dStyle: true)); //this
defines `makeEnumBind`
//makes `enum Plant: ulong`:
mixin(makeEnumBind(q{Plant}, q{ulong}, members: (){
EnumMember[] ret = [
{{q{flower}, q{PLANT_FLOWER}}, q{1}},
{{q{bush}, q{PLANT_BUSH}}, q{2}},
];
if(condition){
EnumMember[] add = [
{{q{herb}, q{PLANT_HERB}}, q{4}},
];
ret ~= add;
}
return ret;
}()));
```
Please note that some parameters and fields are **required** to
be referred to by name. Read the documentation comments for
`makeEnumBind`, `EnumMember`, and `EnumIden` to avoid misusing
them.
More information about the Digitalmars-d
mailing list