I maintain a project (https://chisel.eecs.berkeley.edu/) where our users commonly use anonymous classes as in the following pattern:
Example 1
Implementing trait field with anonymous subclass:
abstract class Foo
trait Bar {
def foo: Foo
}
object Test extends App with Bar {
val foo = new Foo {
val x = 3
}
println(foo.x)
}
- Works in Scala 2.12 with scalac option
-Xsource:2.11
- Fails in Scala 2.12
This is my main use case but there is a similar one:
Example 2
Merely having a field that is an anonymous subclass:
abstract class Foo
object Test extends App {
val foo = new Foo {
val x = 3
}
println(foo.x)
}
- Works in Scala 2.12
- Fails in Dotty
The above examples can also be found on my Github repo: https://github.com/jackkoenig/scala-anon-class-type-inference