https://kotlinlang.org logo
Title
c

Charlie Tapping

02/17/2023, 2:43 PM
Does anyone know why Given an interface like:
interface SynkAdapter<T : Any> : IDResolver<T>, MapEncoder<T>
An impl like this does not count as the aggregated type (SynkAdapter)
class FooSynkAdapter(
    private val fooResolver: IDResolver<Foo> = FooResolver(),
    private val fooMapEncoder: MapEncoder<Foo> = FooMapEncoder()
) : IDResolver<Foo> by fooResolver, MapEncoder<Foo> by fooMapEncoder
You have to do this??
class FooSynkAdapter(
    private val fooResolver: IDResolver<Foo> = FooResolver(),
    private val fooMapEncoder: MapEncoder<Foo> = FooMapEncoder()
) : SynkAdapter<Foo>, IDResolver<Foo> by fooResolver, MapEncoder<Foo> by fooMapEncoder
Surely if an interface is just a union of two other interfaces then implementing those interfaces should make it compatible? I’m sure there must be a good reason for having to be explicit?
s

Sam

02/17/2023, 2:46 PM
Kotlin just isn’t duck-typed 🤷. To be a thing, you have to actually be the thing.
v

Vampire

02/17/2023, 2:47 PM
Just that it has the same methods does not mean it should be the same. Imho it is good that you need to be explicit, as Kotlin is not duck-typed.
c

Charlie Tapping

02/17/2023, 2:51 PM
Well I get why duck typing can be a bit too flexible, but in this case it seems to be quite different. Its not simply that my impl contains a function which has a signature which happens to match
I’m explicitly implementing the two interfaces which make up the composite
feels like we could use a new keywork
union interface
s

Sam

02/17/2023, 2:52 PM
You want an intersection, not a union
Technically, you can make one, with generics
c

Charlie Tapping

02/17/2023, 2:52 PM
or that yes 😛
s

Sam

02/17/2023, 2:53 PM
fun <A, B> doSomethingWith(a: A) where A: IDResolver<B>, A: MapEncoder<B>
It’s not possible to denote the intersection type outside of a generic constraint, though 😞
c

Charlie Tapping

02/17/2023, 2:59 PM
Well I’ll write the extra interface for now 😄, isn’t there work being done on union types? maybe this will extend to intersection types?
s

Sam

02/17/2023, 3:00 PM
Yes, the ticket does cover both intersections and unions 😍
v

Vampire

02/17/2023, 3:20 PM
While I wouldn't expect that it would automagically have the
SnykAdapter
type with that change either 🙂
s

Sam

02/17/2023, 3:24 PM
You could get close though, with a type alias:
typealias SynkAdapter<T> = IDResolver<T> & MapEncoder<T>
Just that
SynkAdapter
wouldn’t be an actual type