Michael de Kaste
12/07/2021, 8:57 PMsealed fun interface
will be added in the future?Ruckus
12/07/2021, 9:02 PMMichael de Kaste
12/07/2021, 9:17 PMsealed fun interface BinaryOperation{
fun operation(val1: UInt, val2: UInt) : UInt
object AND: BinaryOperation by BinaryOperation(UInt::and)
object OR: BinaryOperation by BinaryOperation(UInt::or)
}
which compiles bare the sealed
part.
Or then rather simplified to something like:Michael de Kaste
12/07/2021, 9:17 PMsealed fun interface BinaryOperation {
fun operation(val1: UInt, val2: UInt): UInt
object AND = BinaryOperation(UInt::and)
object OR = BinaryOperation(UInt::or)
}
Michael de Kaste
12/07/2021, 9:22 PMsealed interface BinaryOperation {
val operation: (val1: UInt, val2: UInt) -> UInt
object AND : BinaryOperation {
override val operation = UInt::and
}
object OR : BinaryOperation {
override val operation = UInt::or
}
}
or bloat it even more by
sealed interface BinaryOperation {
fun operation(val1: UInt, val2: UInt): UInt
object AND: BinaryOperation {
override fun operation(val1: UInt, val2: UInt) = val1.and(val2)
}
object OR: BinaryOperation {
override fun operation(val1: UInt, val2: UInt) = val1.or(val2)
}
}
Paul Griffith
12/07/2021, 9:26 PMsealed fun interface
) why not just use a plain enum
?Paul Griffith
12/07/2021, 9:26 PMenum class BinaryOperation(val operation: (left: UInt, right: UInt) -> UInt) {
AND(UInt::and),
OR(UInt::or);
}
Michael de Kaste
12/07/2021, 9:34 PMval result1 = BinaryOperation.AND.test
val result2 = BinaryOperation2.AND.test
fun interface BinaryOperation {
fun operation(val1: UInt, val2: UInt): UInt
object AND : BinaryOperation by BinaryOperation(UInt::and) { val test: Int = 3 }
object OR : BinaryOperation by BinaryOperation(UInt::or)
}
enum class BinaryOperation2(val operation: (left: UInt, right: UInt) -> UInt) {
AND(UInt::and){ val test = 3 },
OR(UInt::or);
}
other than that you are absolutely right, my example has a better usecase in enums