https://kotlinlang.org logo
#announcements
Title
# announcements
e

elect

09/07/2017, 8:02 AM
forgive this post, but if you would like to have bitwise operatos, please vote this issue: https://youtrack.jetbrains.com/issue/KT-1440
b

bamdmux

09/07/2017, 9:18 AM
Seeing the example you posted, why you dont consider writing a small class to handle those flags?
Copy code
class IntFlag(var value: Int = 0) {
    fun set(f: FLAGS) { value = value or f.value }
    fun unset(f: FLAGS) { value = value and f.value.inv() }

    fun flagged() = FLAGS.values().filter { (value and it.value) != 0 }
}

enum class FLAGS(val value: Int) {
    FIRST(1), SECOND(2), THIRD(4), FOURTH(8) //Use whatever tags you need
}
v

voddan

09/07/2017, 9:35 AM
Is there an issue where I can vote AGAINST the bitwise operators? I come from C++, where it is a norm to pack the code with unreadable shifts and masks. It is not worth it.
b

bamdmux

09/07/2017, 9:44 AM
@voddan Cant you vote down on youtrack? Its only up?
e

elect

09/07/2017, 10:11 AM
because of instantiation
I'd need to create a new instance into every sub-function that is called
loc would explode, and the design would get away from the cpp version
b

bamdmux

09/07/2017, 10:28 AM
I dont know your performance constraints, but if you want to avoid instantiation you could use a singleton
Copy code
object FlagHelper {
    val flag = IntFlag()
    fun apply_flags(init: Int = 0, actions: IntFlag.() -> Unit) = with(flag) {
        flag.value = init
        actions()
        value
    }
}
e

elect

09/07/2017, 10:29 AM
immediate gui for 3d realtime graphic
that'd add too much verbosity
atm I am bringing some code down thanks to import renaming
(importing the whole enum isnt an option for me)
b

bamdmux

09/07/2017, 10:34 AM
Ok
2 Views