Why the following is not allowed? (2.12):
type Foo <: {def foo(): Unit}
type Bar <: {def bar(): Unit} with Foo
Looks natural to me, Bar should have both foo() and bar().
Why the following is not allowed? (2.12):
type Foo <: {def foo(): Unit}
type Bar <: {def bar(): Unit} with Foo
Looks natural to me, Bar should have both foo() and bar().
On
It's not allowed by the spec, which calls the { ...} part a refinement which may both have a with clause. The other way around it's fine though.
That's somewhat tautological of course as it doesn't say why it's not allowed by the spec. It appears there is no deep understanding here, just that that's not a way you're allowed to write a type. You should be able to express the same intent in another way.
type Foo <: {def foo(): Unit}istype Foo <: AnyRef{def foo(): Unit}.So while
type Bar <: {def bar(): Unit} with Foois not parsable, withAnyRefand different order it worksAlso with brackets (and direct order) it works
Actually
type Bar <: {def bar(): Unit} with Foois against the spec whiletype Bar <: ({def bar(): Unit}) with Foosatisfies the spec because{def bar(): Unit}is not aSimpleTypebut({def bar(): Unit})is aSimpleType.https://scala-lang.org/files/archive/spec/2.13/03-types.html#compound-types