I don’t think Clojure is a good tool for disovering Java semantics at all. IMO it’s just as bad as Jython or JRuby in this regard. You can implement very exotic semantics on JVM if you don’t care about performance, bytecode size and compatibility with other languages, but Scala wants to be performant and compatible. Often Scala classes can be extended normally in Java, Kotlin, etc code (i.e. it’s not hard to make a class in Scala that will be extensible in Java and Kotlin).
trait Bar {
def m(p:String):Unit
}
class Foo extends Bar {
val f: (p:String) => Unit =
println(“function”)
def m(s:String): Unit =
println(“function”)
val fn = m _
}
Above code is semantically equivalent to this:
trait Bar {
def m(p:String):Unit
}
class Foo extends Bar {
val f = new (String => Unit) { // or (equivalently): new Function1[String, Unit] {
def apply(p: String): Unit =
println("function")
}
def m(s:String): Unit =
println(“function”)
val fn = new (String => Unit) { // or (equivalently): new Function1[String, Unit] {
def apply(s: String): Unit =
m(s)
}
}
It was desugared this way in Scala 2.11 and earlier. Scala 2.12 uses low-level Java 8 constructs for creating lightweight lambdas, but they have the same semantics as anonymous inner classes shown above.
There’s a little catch in desugaring, though. this
in sugar form refers to outer class, while this
in desugared form refers to function itself. The same difference is present in Java 8 language (in lambda vs anonymous class syntax). Anyway, that doesn’t matter in our discussion because outer - inner relationship is completely different from parent - child relationship.