any chance ```sealed fun interface``` will be adde...
# language-proposals
m
any chance
Copy code
sealed fun interface
will be added in the future?
r
Could you elaborate on your use case? That seem like two mutually exclusive concepts. (That is, `fun interface`s are about making it easy for people to provide an anonymous implementation, whereas `sealed interface`s are about stopping people from providing their own implementation.)
m
I was thinking something like this:
Copy code
sealed 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:
Copy code
sealed fun interface BinaryOperation {
    fun operation(val1: UInt, val2: UInt): UInt
    object AND = BinaryOperation(UInt::and)
    object OR = BinaryOperation(UInt::or)
}
In my case, either I bloat the sealed interface AND either change the operation signature to a lambda (which might be unpreferable
Copy code
sealed 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
Copy code
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)
    }
}
p
if your members are monomorphic (which they are, if we're talking about a
sealed fun interface
) why not just use a plain
enum
?
Copy code
enum class BinaryOperation(val operation: (left: UInt, right: UInt) -> UInt) {
    AND(UInt::and),
    OR(UInt::or);
}
m
my example didn't provide this, but this doesn't compile on result2 for instance
Copy code
val 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
1