In the following code, val foo
implements def foo
.
trait A:
def foo: Int
class B(val foo: Int) extends A
However, this does not work for types:
trait A:
type Foo <: Int
class B[Foo <: Int] extends A
Even though the code compiles, the the type parameter Foo
is not associated with the abstract type Foo
in A
.
It also doesn’t work for implicits:
trait A:
given foo: Int
class B(using foo: Int) extends A // compile-error
Any thoughts on this?
rjolly
2
For types you have this not so bad form:
trait A:
type Foo <: Int
class B[Bar <: Int] extends A:
type Foo = Bar
For implicits, why not define it like:
trait A(using foo: Int)
class B(using foo: Int) extends A
For implicits you can do:
trait A:
type Foo <: Int
class B(using foo: Int) extends A:
type Foo = foo.type
// or just:
//type Foo = Int
1 Like
This works:
trait A:
given foo: Int
class B(using val foo: Int) extends A
4 Likes
I guess the equivalent of class B(val foo: Int) extends A
would be:
class B[type Foo <: Int] extends A
4 Likes