4 ms·
This is pretty standard practice, in C# too. However, it's still somewhat prone to typos. If you don't care about backward compatibility (i.e. the flags are us
by Benjamin_Dobell 10y ago
This is pretty standard practice, in C# too. However, it's still somewhat prone to typos.
If you don't care about backward compatibility (i.e. the flags are used internally and never written anywhere), then a better method is actually to grab the value one line above and bit shift it by one. That way you can insert values in the middle and you won't have to change every single line (just two).
- Pxtl 10y agoYeah, but still there's possibility of failure there as you could paste in and forget to update the one below. Honestly, flags should be a type. IIRC, the C# designers have admitted this was a mistake on their parts to continue C's ambiguity between flag enums and choice enums.
- haar 10y agoCrystal has a Flag attribute for enums (Flag enums): http://crystal-lang.org/docs/syntax_and_semantics/enum.html http://crystal-lang.org/docs/syntax_and_semantics/enum.html
- dom96 10y agoNim supports sets which IMO is a much better solution: http://nim-lang.org/docs/manual.html#types-set-type http://nim-lang.org/docs/manual.html#types-set-type
- PeCaN 10y agoTaken from Pascal and other Wirthian languages. Nim actually looks vaguely Wirthish in some areas. Interesting.
- lobster_johnson 10y agoNim's author was originally a Turbo Pascal/Delphi programmer. A lot of Nim and its conventions (things like prefixing type names with T and members with F) come directly from Borland's Pascal dialect.
- Pxtl 10y agoAh, so a Set keyed with an Enum is functionally C-style "flags" then?
- dom96 10y agoPretty much. Here is an example: type EGlassRNState = enum EGlassRNState_Initial, EGlassRNState_Weakened, EGlassRNState_Shattering, EGlassRNState_Shattered, EGlassRNState_ActiveFrags proc hasGlassShattered(mState: set[EGlassRNState]): bool = EGlassRNState_Shattered in mState proc hasActiveFragments(mState: set[EGlassRNState]): bool = EGlassRNState_ActiveFrags in mState assert hasActiveFragments({EGlassRNState_Shattered, EGlassRNState_ActiveFrags})