Fellter
10/15/2026, 8:07 PMinterface Taggable {
fun addTag(tag: Tag)
}
/*
* Example syntax for interfaces with abstract methods
*/
extend String : Taggable {
override fun addTag(tag: Tag) {
// ... implementation
}
}
Or this for marker interfaces:
interface Primitive
/*
* Shorthand syntax for extending multiple types with a "marker" interface (has no methods to override)
*/
extend String, Byte, Short, Int, Long, Float, Double : Primitive
For example, Int would now have these supertypes: Number, ComparableInt, Serializable and PrimitiveYoussef Shoaib [MOD]
10/15/2026, 8:17 PMThe closest of these to Kotlin is the Scala approach. Scala hasdeclarations, which are Rust’sgiven. There’s then theimplmodifier, which is like Rust’s trait bounds.using
Kotlin doesn’t have an easy `given`/`impl` equivalent, but it does have a `using`/trait bound equivalent: context parameters!
In other words, Kotlin doesn’t have trait lookup (so you have to explicitly bring in your traits), but it can propagate traits implicitly to functions that need them.
Youssef Shoaib [MOD]
10/15/2026, 8:26 PM// you want an interface that describes what the type does, not what instances of the type do
interface Taggable<in T> {
fun T.addTag(tag: Tag)
}
// bridge function
context(t: Taggable<T>)
fun <T> T.addTag(tag: Tag) = with(t) { this@addTag.addTag(tag) }
// implementation
object StringTaggable: Taggable<String> {
override fun String.addTag(tag: Tag) {
// ... implementation
}
}
// utility
context(_: Taggable<T>)
fun <T> T.addABunchOfTags() {
addTag(Tag1)
addTag(Tag2)
...
}
// use site
context(StringTaggable) {
"foo".addABunchOfTags()
"bar".addTag(Tag1)
}
and:
interface Primitive<in T>
// implementations
object StringPrimitive: Primitive<String>
object BytePrimitive: Primitive<Byte>
...
// convenience function
inline fun <R> withPrimivites(block: context(Primitive<String>, Primitive<Byte>, ...) () -> R): R = block(StringPrimitive, BytePrimitive, ...)
// utility
context(_: Primitive<T>, _: Primitive<R>)
fun <T, R> doSomethingWithPrimitives(foo: T, bar: R) = ...
// use site
withPrimitives {
doSomethingWithPrimitives("foo", 54.toByte())
}Youssef Shoaib [MOD]
10/16/2026, 6:56 AMinterface Fooin T {
fun T.f()
}
context(impl: FooT)
fun T T.f() = with(impl) { f() } // or `with(impl) { this@f.f() }` to be safer
object FooInt: FooInt {
override fun Int.f() {}
}
context(_: FooT)
fun T g(foo: T) {
foo.f()
}
and only the bridge function sticks out really
Maybe there's a world where the above has nicer syntax sugar:
typeclass Foo { // implicitly has `T`
fun foo() // implicitly has receiver, and generated bridge
}
object FooInt: FooInt {
override fun Int.f() {}
}
fun g(foo: Foo) { // generates an overload or similar that has the type parameter shenanigans
foo.f()
}Youssef Shoaib [MOD]
10/16/2026, 7:57 AMinterface StringExt {
fun splice(...)
}
extend String : StringExt {
override fun splice(...) = ...
}
"Hello world".splice(...)
Looks like extensions with extra steps 🤷🏼Youssef Shoaib [MOD]
10/16/2026, 8:33 AMextend can be (mis)used to end up with extension functions. This isn't just hypothetical; Scala 2's `implicit`s were used as such, too; in Scala 3, they replaced it with (equivalents of) context and extension functions
Haskell has type classes that look a lot more like extend. They run into issues when there are multiple reasonable implementations of a typeclass, and you wish to switch between them.
Their solution is to wrap them in (their equivalent of) `value class`es. `context`s let you choose the local instance you want to use.
If anything, `context`s are more explicit than typeclasses are in Haskell and Scala because you have to provide them locally (i.e. a context must come from a lexically enclosing scope), while those other languages have involved algorithms that assemble such a typeclass, i.e. they replace complex name resolution with typeclass resolution (in Scala's case, they have both!)
I support the core idea of the proposal (adding "interfaces" to types at compile-time), but IMO, context and extension functions would need to be incorporated into such a design because they serve similar purposes.Joffrey
10/16/2026, 8:54 AMCoroutineScope.
With the proposed interface extensions, it's only worse: the fact that you can use a value as another interface is defined in some completely unrelated place (that you probably would have to import), and the import is nowhere to be found in the local lexical scope of the use site. How is this better than contexts, where the context instance has to be in the local scope of the use site?Fellter
10/18/2026, 4:33 PMimport extension my.simple.example.String
or
import extension my.simple.example@kotlin.String
This would already signal to the writer that the type is extended in some way, especially if they import it manually. And, if not, they'd probably be using an IDE, which would somehow present it anyways.
the fact that you can use a value as another interface is defined in some completely unrelated place (that you probably would have to import), and the import is nowhere to be found in the local lexical scope of the use site.Yes, I also agree in this regard. My original intention was mainly to simplify calling functions and passing types as arguments without the need to declare a new function for every type. As @min said, then we would be able to do this:
interface Foo {/* ... */}
extend Int: Foo {/* ... */}
extend String: Foo {/* ... */}
extend Boolean: Foo {/* ... */}
fun f(foo: Foo)
f(1)
f("")
f(true)
without the need to declare three different functions.
I might just be misunderstanding your arguments, but from my perspective, contexts only clutter the call site and make it less readable.Joffrey
10/19/2026, 7:59 AMI might just be misunderstanding your arguments, but from my perspective, contexts only clutter the call site and make it less readable.@Fellter sorry I wasn't really making an argument against this proposal, but merely trying to understand @min’s argument. It seemed to me that the argument about local reasoning is one that would go in favor of contexts instead of in favor of this proposal, so I was confused and tried to explain why I don't get it.
Joffrey
10/19/2026, 8:19 AMpath.exists() reads much better IMO than Paths.exists(path).
By the way, please don't be fooled by the [JetBrains] tag. While I do work at JetBrains, I'm not part of the Kotlin team, so I'm just commenting as a fellow user here 🙂Joffrey
10/19/2026, 8:25 AMI also find it misleading that x.f(y) in a Foo.() -> Unit makes the function appear binary when it takes three runtime arguments.Do you feel the same about
this anywhere? That part might be a matter of habit. Now that said, I'd like to point out that you're no longer talking about extensions in general but more specifically member extensions that have a dispatch receiver and that are exposed outside the type they are declared in. I do share some of your concerns here, and I think contexts are slightly better to express this because a little bit more explicit.
I prefer to call bar.f(x, y) in a (bar: Bar) -> Unit because it spells out all three runtime arguments, and declaring such a function give you a clearer opportunity to consider its namespace.I think it really depends on the specific semantic of each of the parameters. I think having the version where
Foo is a dispatch receiver (or context) is better if:
1. f "acts on" x
2. f could have been a member of x, at least if x had been defined in this specific project
3. `Foo`/`Bar` merely adds context to the call that is not directly related to what's happening (e.g. logger, analytics reporter, diagnostics, some sort of execution context, etc.), or if it brings constraints to the call that are not about the call itself but a wider section of code (e.g. coroutineScope { ... }).Joffrey
10/19/2026, 8:26 AMis.