Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing an abstract type from a parameterized type

Tags:

scala

Given this example:

abstract class Base { type Value }
case object Foo extends Base { type Value = String }

case class Bar[A <: Base](a: A, value: A#Value)

val x = Bar(Foo, "It's a Foo") // Won't compile with found String required ?#Value

I would like to have the type constraint for value to be the same as the type for A. I.e. String since I'm passing in Foo. I know I can model this differently, but it would be nice to know if it's at all possible?

like image 568
chrsan Avatar asked Dec 22 '25 14:12

chrsan


1 Answers

As a starting point this compiles and provides some of the constraint you require.

abstract class Base { type Value }
case object Foo extends Base { type Value = String }

case class Bar[A <: Base, V <: A#Value](a: A, value: V)

val x = Bar(Foo, "It's a Foo")
like image 90
Don Mackenzie Avatar answered Dec 24 '25 06:12

Don Mackenzie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!