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.
t
Sorry to necro on this one, let me know if I should post a new thread instead. I'm also looking for something like this. I'd like to automatically "implement" a bunch of methods for types that implement a simple interface (just one method, in fact). Like the OP, I've read that KEEP (and been really liking using context parameters), however I'm not quite understanding how they can be used to implement type-classes/traits. I mean, I suppose I see a way, but it would require declaring some an object implementing the interface and then having to use
with(ImplementorObjectForTypeFoo)
every time I want to use it. That's way more verbose than it should be, syntax-wise, and much heavier in the final JAR as well. Is there no better way to achieve it?
My specific use-case looks like:
Copy code
interface ToFoo {
  fun toFoo(): Foo
}

// then
fun T : ToFoo T.bar() = toFoo().let { ... }  // whatever logic
// and more methods based on toFoo
How can I "implement"
Bar : ToFoo
? or equivalent
If it was my type, I'd naturally just do
Copy code
class Bar : ToFoo {
  override fun toFoo() = ...
}
y
It is verbose, but I don't understand what you mean by heavier in the JAR? It'll ultimate just be a singleton object, and you simply look it up and call a method on it. I don't think that's so heavy as to be prohibitive
t
If you do it for a lot of things, it's certainly heavier than it really needs to be. And the verbosity is quite annoying for the number of things I'd like to do. I suppose I could write a plugin/annotation processor to do some of the writing for me, but that's another can of worms.