``` val foo : SomeInterface = inputVariable val ba...
# getting-started
d
Copy code
val foo : SomeInterface = inputVariable
val bar = if (foo is implA) (foo as implA) else (foo as implB)
Here
bar
will be of type SomeInterface. Is it possible to somehow use implementation specific stuff available on both implementations but not on the interface, so
bar.fieldOnlyOnImplementations
?
d
No, that is not possible, since the methods are different, even though they might share a name and signature. What you want is called duck typing and not supported in Kotlin. If you want to access the common properties/methods, introduce an interface for them.
👍 1
u
no you can only have it typed to common type, i.e. Any in this case -- or make 2 functions, each taking its type and call it a day
h
Wouldn't
when
be helpful here?
Copy code
when (foo) {
    is implA -> implA.fieldOnlyOnImplementation

    is implB -> implB.fieldOnlyOnImplementation
}
Using smart cast would help to use implementation specific methods here