New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
color: Replace color components union with property functions #406
base: master
Are you sure you want to change the base?
Conversation
I’d like to hear your opinion on this, @adamdruppe. My motivation behind this change is to get arsd to act more or less like a drop-in replacement and make it more convenient to use. |
// Unlike an anonymous union, this works with CTFE as well. | ||
@safe pure nothrow @nogc { | ||
// individual rgb components | ||
pragma(inline, true) ref inout(ubyte) r() inout scope return { return components[0]; } /// red |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i doubt the pragma is worth it, but since you return ref
here anyway, no need for a setter; the ref return can be assigned.
As long as this compiles on my oldest supported compiler i think im ok with it, i gotta find where i used these but i don't think anything would break here, it should work fine everywhere and compile to the same thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve added the setters specifically after something from arsd would no longer compile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not sure how to reproduce this, however
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve commented out the setters.
29f3b8a
to
4a04a65
Compare
I’ve updated this patch. |
Well, it removes an anonymous union. Could be an advantage. |
@adamdruppe |
i kinda forgot about this one lol lemme look at it again |
well looking it back over it looks ok enough. i can go ahead and merge if you wanna |
Any suggestions to improve it? |
In its current form, this is a breaking change suitable for arsd 12 or a later major release.
getter+setter vs. union
Color.components
is turned intoubyte[4]
.Unlike an anonymous union, this works with CTFE as well.
r
,g
,b
,a
andasUint
are turned into getter + setter functions.A sufficiently good optimizing compiler will generate the same code for these as it would previously have with the union. (Note that this cannot be confirmed with DMD’s built-in profiler as it’s implementation will poison any getter/setter functions.)
alias this
The alias this stems from my codebase which did use it to simplify certain implementation details.
I’m not sure whether it’s a good thing or even worth it.