1

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.

1 Answer 1

4

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 } 
Sign up to request clarification or add additional context in comments.

2 Comments

(commenting for the record:)this also works with inheritance: case class ChildClass(arg:ChildClass#aType) extends ParentClass.
Yes, as the child class inherits both type and regular members of the parent.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.