Would it be possible for the Kotlin compiler to al...
# compiler
k
Would it be possible for the Kotlin compiler to allow interface implementation by extension? For example, something like:
Copy code
// An interface in code I define. 
interface Thing { ... }

// A type from a third party library. 
class SomeThirdPartyType

// An implementation of Thing based on the public API of SomeThirdPartyType that I own. 
extension SomeThirdPartyType : Thing { ... }
A really naive implementation of this could generate some wrapper like
SomeThirdPartyType$ThingImplWrapper
that, whenever an instance of
SomeThirdPartyType
is referenced as a Thing, it is boxed by the generated wrapper. Are there unsuspecting ways which something like this might actually be terrible?
y
This has been proposed before as a formal KEEP, and was rejected in favour of context parameters.
👍 1
k
I haven't had a chance to play with context parameters yet. How does it solve this issue?
I read the portion of the KEEP that seems relevant and I'm not entirely understanding how it'd solve the OP https://github.com/Kotlin/KEEP/blob/context-parameters/proposals/context-parameters.md#context-oriented-dispatch--externally-implemented-interface--type-classes
y
You'd write
interface Thing<T>
and make every operation an extension on T, then you'd take in a Thing context everywhere you want those operations. This is like type classes in other languages
k
Oooh I see, so the interface would look something like this:
Copy code
interface Thing<T> {
  fun T.doThing()
}
If that's the case, then I don't think this covers all scenarios but still a nice thing to have.