phobos/changelog/bitflags-property-opdispatch.dd
2018-02-24 14:55:42 +01:00

49 lines
1.5 KiB
Text

`std.typecons.BitFlags` now supports opDispatch-based property access
$(REF BitFlags, std.typecons) was extended so that enum members can be set and tested directly on the
`BitFlags` instead of having to `&` with the underlying enum.
-------
enum Features
{
fast = 1 << 0,
size = 1 << 1,
}
void run(BitFlags!Features features)
{
// get
if (features.fast && !features.size) {} // new new new
if ((features & Features.fast) && !(features & Features.size)) {} // old old old
// set
features.fast = true; // new new new
features.fast = false; // new new new
features.fast |= Features.fast; // old old old
features.fast &= ~Features.fast; // old old old
}
-------
This also works for unsafe `BitFlags` where the property get access tests for an exact
match of all bits of the unsafe `BitFlags` combination.
Analogously, the property set access clears or sets all bits of the unsafe `BitFlags` combination.
-------
enum Features
{
fast = 1 << 0,
size = 1 << 1,
combined = fast | size,
}
void run(BitFlags!(Features, Yes.unsafe) features)
{
// get
if (features.combined) {} // new new new
if ((features & Features.combined) == BitFlags!(Features, Yes.unsafe)(Features.combined)) {} // old old old
// set
features.combined = true; // new new new
features.combined = false; // new new new
features.combined |= Features.combined; // old old old
features.combined &= ~Features.combined; // old old old
}
-------