List of classes with types

28 views
Skip to first unread message

Konstantine Kougios

unread,
Nov 2, 2011, 4:20:46 PM11/2/11
to scala...@googlegroups.com
Hi,

    I've a class Entity[T]. So Entity can be i.e. Entity[String] or Entity[Int] or Entity[MyClass].

Now I want to store these in a List:

List(Entity[String],Entity[Int],....)

So far the only way I found to do this is to have a List[Entity[_]], but this is problematic:

val l:
List[Entity[_]] = ...

l.map { entity=>
    // entity is an Entity[_] where as I need it to be an Entity[T] inside this closure
}

Is there a refactoring I can do to better handle this?

Thanks,

Kostas

Nick H

unread,
Nov 2, 2011, 4:39:56 PM11/2/11
to scala...@googlegroups.com
You can't directly pattern match on entity because the type of Entity is unknown at compile time, so you can't check if an object is of type Entity[String]. You can get Scala to provide a Manifest class to accompany the object, which will contain more information. It's a bit fiddly to use, though. If you could give a more concrete example of what you hope to do within map(), there might be a different way around it that's neater.

Nick H

unread,
Nov 2, 2011, 4:40:27 PM11/2/11
to scala...@googlegroups.com
the type of Entity is unknown at compile time

I mean it's unknown at runtime. 

Brian Maso

unread,
Nov 2, 2011, 5:02:29 PM11/2/11
to scala-user
(including scala-user in my reply)

---------- Forwarded message ----------
From: Brian Maso <brian....@gmail.com>
Date: Wed, Nov 2, 2011 at 2:02 PM
Subject: Re: [scala-user] List of classes with types
To: Konstantine Kougios <kostas....@googlemail.com>


Essentially what you are doing is using List as a Tuple of arbitrary arity. If you would really like to preserve the type info of the collection at runtime you should take a look at HList and KList, which Mark Harrah has implemented in his github project "up". Using these, you can create "heterogenous lists" -- lists which preserve each element's type information.

You'll not be getting a good heterogenous list "map" implementation, though. If you think about it, application of a single mapping function to different elements of a heterogenous list doesn't make a lot of sense. The map's body would have to be a big switch statement, where each case would pattern match on the element's type. That is, you'd have to squeeze each of the HList elements of types T1, T2, T3, etc. in to the input parameter of the same map function -- you'd lose the type information, since the type of the input parameter would have a lower-bound equal to the least upper bound of the types T1, T2, T3, etc. So even if you preserved type information in an HList, you'd have to throw it away specifically during the invocation of the "map" function.

Brian Maso

Eduardo Pareja Tobes

unread,
Nov 2, 2011, 5:21:47 PM11/2/11
to Brian Maso, scala-user
you can, if what you want to do is available at the Entity level; you need a KList with Entity as the type constructor, and define a natural transformation Entity ~> Id

--

Eduardo Pareja Tobes
Reply all
Reply to author
Forward
0 new messages