Colors in Raylib

Mike Parker aldacron at gmail.com
Mon Feb 28 12:18:37 UTC 2022


On Monday, 28 February 2022 at 11:48:59 UTC, Salih Dincer wrote:
> Hi All,
>
> Is there a namespace I should implement in Raylib? For example, 
> I cannot compile without writing Colors at the beginning of the 
> colors: ```Colors.GRAY```
>
> SDB at 79

Assuming you mean the raylib-d binding, it implements the values 
as a named enum, so the `Colors` namespace is required.

https://dlang.org/spec/enum.html#named_enums

If you have a situation where you need to type it multiple times 
in consecutive code lines, you can use `with`:

```d
with(Colors) {

}
```

Then you can drop the namespace and just used the values. Very 
useful for switches:

```d
with(Colors) switch(myColor) {

}
```

You can also generate aliases, so that e.g., `LIGHTGRAY` is 
equivalent to `Colors.LIGHTGRAY`). Just throw this template 
function in an appropriate module:

```d
enum expandEnum(EnumType, string fqnEnumType = EnumType.stringof) 
= (){
     string expandEnum;
     foreach(m;__traits(allMembers, EnumType)) {
         expandEnum ~= "alias " ~ m ~ " = " ~ fqnEnumType ~ "." ~ 
m ~ ";";
     }
     return expandEnum;
}();
```

Then you can mixin aliases for any named enum members you'd like:

```d
mixin(expandEnum!Colors);
```


More information about the Digitalmars-d-learn mailing list