User1291
User1291

Reputation: 8229

Scala - case classes inheriting types

Is there a way to have a case class accept types defined in a trait that it mixes in? When I try what I would do with ordinary classes, it fails:

trait myTypes{
    type aType = Array[String]
}

abstract class ParentClass extends myTypes{
    //no issue here
    val a:aType = Array.fill[String](7)("Hello")
}

//error: not found: type aType
case class ChildClass(arg:aType) extends ParentClass

//error: not found: type aType
case class ChildClass2(arg:aType) extends myTypes

Not sure why Scala would choose to behave this way, but I would appreciate some help in circumventing this annoying error.

Upvotes: 1

Views: 76

Answers (1)

J Cracknell
J Cracknell

Reputation: 3547

This happens because the type alias is out of scope:

// error: not found: type Bar
class Foo(val bar: Bar) { type Bar = String }

Instead try:

class Foo(val bar: Foo#Bar) { type Bar = String }

Upvotes: 4

Related Questions