Rust like Self type, Kotlin seems going to add it

Rust: Paths - The Rust Reference
Kotlin: KEEP for Self types by maxim092001 · Pull Request #356 · Kotlin/KEEP · GitHub

I think this kind of type very helpful to reduce the boilerplate.

I believe there was some thinking about this, but this leads to some ambiguity in cases like the following:
(Using This instead of Self)

trait A:
  def v: Array[This]

abstract class B() extends A

class C() extends B:
  def v = Array() // type of v is Array[A], Array[B] or Array[C] ?

Since Array is invariant, each possibility leads to different behaviour

I hope this illustrates how in more complicated examples, This becomes really hard to both specify and keep track of in your head

2 Likes

See Introduce `This` type to replace self types · Issue #7374 · lampepfl/dotty · GitHub and Fix #7374: Introduce This Types by odersky · Pull Request #7376 · lampepfl/dotty · GitHub for a draft implementation. It did not make it into Scala 3.0, because it was not deemed essential. But I still like to come back to this at some time.

7 Likes