template? mixin? template mixins? for modifying a struct setup

Chris Katko ckatko at gmail.com
Fri May 20 00:12:44 UTC 2022


On Thursday, 19 May 2022 at 10:35:30 UTC, ag0aep6g wrote:
> On 19.05.22 12:15, Chris Katko wrote:
>> given
>> ```D
>> struct COLOR
>> {
>> float r, g, b, a; // a is alpha (opposite of transparency)
>> }
>> 
>> auto red   = COLOR(1,0,0,1);
>> auto green = COLOR(0,1,0,1);
>> auto blue  = COLOR(0,0,1,1);
>> auto white = COLOR(1,1,1,1);
>> //etc
>> ```
>> 
>> is there a way to do:
>> ```D
>> auto myColor = GREY!(0.5);
>> // where GREY!(0.5) becomes COLOR(0.5, 0.5, 0.5, 1.0);
>> ```
>
> What's wrong with a simple plain function?
>
> COLOR grey(float rgb)
> {
>     return COLOR(rgb, rgb, rgb, 1);
> }
> auto myColor = grey(0.5);

Yeah that occurred to me as I was falling asleep. Though, do I 
have to a specify

```D
static auto myColor = grey(0.5);
```
to ensure it's done at compile time? It's not the end of the 
world, but ideally, these are static / hardcoded values that can 
be used thousands of times a second.


More information about the Digitalmars-d-learn mailing list