So for Swift we can create new types or pass to a method as a parameters using the &
operator.
Example Swift Code:
protocol Fooable {}
protocol Barable {}
// the new protocol
typealias FooBarable = Fooable & Barable
// method parameter
func doSomethingFor(object: Fooable & Barable) { ... }
Is there a way to do this in Kotlin's Interfaces?
Please check the below code:
interface A{
}
interface B{
}
fun <T> check(variable: T) where T : A, T: B{
print("Hello");
}
the above gives you compile time error if you try to pass a variable which doesn't confirm to both of them
From the function side you'd be able to handle it with generic functions using a where
-clause:
fun <T> foo(obj: T) where T: Fooable, T: Barable {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With