Assistance with generic type parameter

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Assistance with generic type parameter

Colin Bester
I was hoping someone could assist and point me in the right direction as I am going in circles with this and know I am missing some finer point.

I am modeling data structures using case classes (the data model) and object singleton as helper object.

I am looking to create a List() of these helper objects (extending the MetaModel trait) and require type checking of list. 
Best I have been able to do is shown in line 17 val list:List[MetaModel[_]]  and ideally I would like to include the Model in generic type, something along lines of List[MetaModel[Model[_]]] but at this point the wheels fall off the bus and am in +/-M hell.

I've been trying to take a step back in hopes of seeing where I am going wrong but to no avail and am hoping that someone could/will point me in the right direction on how to specify generics for List of MetaModel of Models. 

trait MetaModel[M <: Model[M]] {
 
def speak:String
}

trait
Model[A <: Model[A]] {
 
self: A =>
}

object A1 extends MetaModel[Model1] { def speak="Model1, A1"}
object B1 extends MetaModel[Model1] { def speak="Model1, B1"}
object A2 extends MetaModel[Model2] { def speak="Model2, A2"}

case class Model1(a: Int) extends Model[Model1]
case class Model2(a: Int, b: String) extends Model[Model2]

object MyTest extends App {
  val list
:List[MetaModel[_]] = List(A1, A2, B1) //Looking to specify type for list.
 
  list
.foreach { x => println(x.speak) }
}


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Assistance with generic type parameter

Jon Pretty-4
Hi Colin,

I maybe haven't fully understood your problem, but if you're using F-bounded polymorphism (i.e. the type parameter `[M <: Model[M]]`), then `M` probably wants to be covariant, and you will want to use `M` in the covariant positions in the definition and `Model[M]` in the contravariant positions.

You can then use an existential to define the type of the list, like this:

  val list: List[MetaModel[T] forSome { type T <: Model[T] }] = List(A1, A2, B1)

It's ugly.

Hope that helps,
Jon

On 22 January 2015 at 21:21, Colin Bester <[hidden email]> wrote:
I was hoping someone could assist and point me in the right direction as I am going in circles with this and know I am missing some finer point.

I am modeling data structures using case classes (the data model) and object singleton as helper object.

I am looking to create a List() of these helper objects (extending the MetaModel trait) and require type checking of list. 
Best I have been able to do is shown in line 17 val list:List[MetaModel[_]]  and ideally I would like to include the Model in generic type, something along lines of List[MetaModel[Model[_]]] but at this point the wheels fall off the bus and am in +/-M hell.

I've been trying to take a step back in hopes of seeing where I am going wrong but to no avail and am hoping that someone could/will point me in the right direction on how to specify generics for List of MetaModel of Models. 

trait MetaModel[M <: Model[M]] {
 
def speak:String
}

trait
Model[A <: Model[A]] {
 
self: A =>
}

object A1 extends MetaModel[Model1] { def speak="Model1, A1"}
object B1 extends MetaModel[Model1] { def speak="Model1, B1"}
object A2 extends MetaModel[Model2] { def speak="Model2, A2"}

case class Model1(a: Int) extends Model[Model1]
case class Model2(a: Int, b: String) extends Model[Model2]

object MyTest extends App {
  val list
:List[MetaModel[_]] = List(A1, A2, B1) //Looking to specify type for list.
 
  list
.foreach { x => println(x.speak) }
}


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
Jon Pretty | @propensive

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Assistance with generic type parameter

Jon Pretty-4
When I tried it, I came up against what appears to be a limitation in type inference, so you will need to specify the type on the `List` when it's instantiated, unfortunately.

Cheers,
Jon

On 22 January 2015 at 23:02, Jon Pretty <[hidden email]> wrote:
Hi Colin,

I maybe haven't fully understood your problem, but if you're using F-bounded polymorphism (i.e. the type parameter `[M <: Model[M]]`), then `M` probably wants to be covariant, and you will want to use `M` in the covariant positions in the definition and `Model[M]` in the contravariant positions.

You can then use an existential to define the type of the list, like this:

  val list: List[MetaModel[T] forSome { type T <: Model[T] }] = List(A1, A2, B1)

It's ugly.

Hope that helps,
Jon

On 22 January 2015 at 21:21, Colin Bester <[hidden email]> wrote:
I was hoping someone could assist and point me in the right direction as I am going in circles with this and know I am missing some finer point.

I am modeling data structures using case classes (the data model) and object singleton as helper object.

I am looking to create a List() of these helper objects (extending the MetaModel trait) and require type checking of list. 
Best I have been able to do is shown in line 17 val list:List[MetaModel[_]]  and ideally I would like to include the Model in generic type, something along lines of List[MetaModel[Model[_]]] but at this point the wheels fall off the bus and am in +/-M hell.

I've been trying to take a step back in hopes of seeing where I am going wrong but to no avail and am hoping that someone could/will point me in the right direction on how to specify generics for List of MetaModel of Models. 

trait MetaModel[M <: Model[M]] {
 
def speak:String
}

trait
Model[A <: Model[A]] {
 
self: A =>
}

object A1 extends MetaModel[Model1] { def speak="Model1, A1"}
object B1 extends MetaModel[Model1] { def speak="Model1, B1"}
object A2 extends MetaModel[Model2] { def speak="Model2, A2"}

case class Model1(a: Int) extends Model[Model1]
case class Model2(a: Int, b: String) extends Model[Model2]

object MyTest extends App {
  val list
:List[MetaModel[_]] = List(A1, A2, B1) //Looking to specify type for list.
 
  list
.foreach { x => println(x.speak) }
}


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
Jon Pretty | @propensive



--
Jon Pretty | @propensive

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.