4

Quite simply, is there any way to marry constructor arguments and abstract types? For example, something I would like to do

class A(t: Seq[T]) { type T } 

2 Answers 2

6

The members of the class aren't in scope in the parameter declaration of the constructor.

This is as close as you can get:

scala> trait T { type T; val a: T } defined trait T scala> def A[X](x: X) = new T { type T = X; val a = x } A: [X](x: X)Object with T{type T = X} scala> A[Int](0) res0: Object with T{type T = Int} = $anon$1@3bd29ee4 scala> A[String](0) <console>:10: error: type mismatch; found : Int(0) required: String A[String](0) ^ scala> class AA[X](val a: X) extends T { type T = X } defined class AA scala> new AA[Int](0) res5: AA[Int] = AA@1b3d4787 scala> new AA[String](0) <console>:10: error: type mismatch;  found   : Int(0)  required: String              new AA[String](0)                             ^ 
Sign up to request clarification or add additional context in comments.

3 Comments

I am not sure about this type theory..can you please explain more about how this achieves the goals of the OP?
He wanted to have a constructor argument of the same type as a type member. class AA comes pretty close to that.
Thanks for explaining, class AA looks very interesting indeed!
2

Does this not suit your needs?

class A[T](ts: Seq[T]) 

1 Comment

It is what I am trying to move away from. This is how my current implementation works, but I would like to think of Parameterized Types as being useful for something I need to make a lot of generic instances of (eg: List[String], List[Int], List[NyanCat], etc etc) rather than something that will generally have less than 10 use cases (eg: TV[NTSC], TV[PAL], TV[Digital]).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.