now, if we could have also bitwise operators it'd ...
# random
e
now, if we could have also bitwise operators it'd be the best metal
e
I have something similar but shorter:
has
,
hasnt
,
or
,
and
,
wo
(without)
I couldnt come up yet with a better alternative
the dual representation in C/C++ between enum and int is really enviable
I wish we could have something similar in Kotlin
g
But why do you need enum in this case instead of just object with constants?
e
I also explored the
EnumSet
possibility, but that's not an option for me. Too many code border, instantiations and style difference from original C port
because I want to infer also type safety
g
but why do you need int and all those flags than? Just use set of enums. And use standard methods of set, instead of converting to/from enum
Of course set requires more memory than single int, but also will be easier to use and much more type-safe
e
what do you have in mind, Andrey?
I usually have
enum class WindowFlag
and
typealias WindowFlags = Int
to help understanding on which enum set an int mask is based from
you meant
EnumSet
, didn't you?
g
Why do you use int instead of enums for preferences? Because of performance?
EnumSet for example, yeah, it’s slightly more efficient than just general usage Set
e
because I have a massive usage of enums
g
I didn’t get your point
So if you have a lot of enums, use them as enums, not as int representation
e
and that would require too many
EnumSet
instantiations and style difference compare to the C code
the enums allow me to also use the int counterpart wherever I need that and there is no additional instantiation overhead
and it closer to the C version I do the port from
g
Why style should be the same as for C? why
window.flags += ShowBorders
is worse than
window.flags |= ShowBorders.i
wherever I need that
Enum already has
ordinal
property which is int representation of enum
e
That's an easy example, but when I tried that there were some complicate passages where I had to completely change the original line style (and this is also quite error prone)
I can take advantage of
ordinal
only when the corresponding enum is supposed to start from 0 and increase contiguously. Unfortunately this is not always the case
so I have an additional
.i
field for that
g
Yeah, maybe in terms of porting is make some sense, you just want to port with minimal effort
e
exactly
maybe inline classes would bring some new possibilities
g
But my point, that probably for pure Kotlin code without this attempt to keep style for another language, I would rather use Enum directly
e
I'm really looking forward for those
yeah, definitely
g
Unfortunately, as I understand, inline enum classes will not be available on first implementation. You can check KEEP and maybe add your cases
Also, interesting paragraph from EnumSet docs:
Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based “bit flags.” Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.
👍 1
So I would really take a look into it. Int is definitely more efficient, but one more object probably wouldn’t be a problem, especially so efficient implementation
Also EnumSet handles enums for more than 64 options automatically
e
you are so tempting, Andrey ^^
ah, another issue with
EnumSet
is that on C, when you pass
flag
you pass a copy, on JVM I'd pass the reference to the class. That was another big error prone point
I'd have to create a copy everytime I pass it to another function -> more instantiations
g
Yes, instantiation of one object with one int field, of course if it’s real problem than you on the stage when for optimization reasons you should do your code ugly But I would really concider this option. It’s actually the same price as for boxing Int, also one object with Int field, and probably even more efficient than creation of single string. And because this is probably public API, maybe make sense to make it even more user friendly and type safe and pay price of a couple objects per component
e
Anyway Andrey, looking at the inline proposal one use cases is "inline enum classes", https://github.com/Kotlin/KEEP/blob/master/proposals/inline-classes.md
this is exactly also my use case! I cant wait
g
Yes, I know, I saw this in proposal, but enums mentioned only as "use case", no details about implementation, but would be happy to see this as part of the first implementation