3

Is it possible to have a manifest defined based on another manifest in Scala?

I've pretty much resigned myself to the belief that this is not possible because the Scala Manifest information was not intended to be used dynamically.

Here's the problem. I have a function that can return more than one type of object (String, Int, List[Int], List[List[String]], etc.) To support these multiple types, the return type is set to Any, but due to type erasure the information about the types supported in Lists, Maps, etc is lost. In an attempt to recover some of the details, I return a Manifest along with the return type.

However, the returned information may then be placed in another list or map and that is then returned from another function. I want to update the manifest to include the fact that the type is now a List or Map of the previous type as defined by the manifest.

Here's some example code

def returnWithManifest[T: Manifest](x: T) = (x, manifest[T])

// May return String, Int, List[Int], List[List[String]], ...
def contrivedExample(t: String): (Any, Manifest[_]) = t match {
  case "String" => returnWithManifest("test")
  case "Int" => returnWithManifest(1)
  case "Boolean" => returnWithManifest(true)
  case "List[Int]" => returnWithManifest(List(1,2,3))
  case "List[List[String]]" => 
    returnWithManifest(List(List("a","b"),List("c","d")))
  case _ => returnWithManifest(None)
}

scala> val v1 = contrivedExample("List[Int]")
v1: (Any, Manifest[_]) = (List(1, 2, 3),scala.collection.immutable.List[Int])

scala> val x = v1._1
x: Any = List(1, 2, 3)

scala> val m = v1._2
m: scala.reflect.Manifest[_] = scala.collection.immutable.List[Int]

scala> val v2 = List(x)
v2: List[Any] = List(List(1, 2, 3))

From the manifest of 'v1' I know that v1 is of type List[Int] so when I create 'v2' I should have all the information I need to create a manifest identifying that the type is List[List[Int]], but instead I only have List[Any] to work with. Perhaps syntax like the following:

val v2: m = List(x)
val v2 = List[m](x)

I realize it looks like I'm trying to define a type dynamically, but in reality the information is metadata related to type erasure of statically known types. I guess if this can be solved, then type erasure can be solved. However, at the very least I thought I should be able to do something like:

scala> val m2 = m.wrapInList()
m2: scala.reflect.Manifest[_] = 
      scala.collection.immutable.List[scala.collection.immutable.List[Int]]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike
  • 245
  • 2
  • 11

3 Answers3

5

Edit: Adriaan Moors is right pointing out this works:

def makeListManifest[T: Manifest] = manifest[List[T]]

You'll just have to call it explicitly, passing it the manifest you have obtained.


My old answer:

huynhjl is partially right: currently this won't work automatically. We'd need the compiler to be smart enough to compile this:

def makeListManifest[T](m: Manifest[T]) = manifest[List[T]]

without any extra implicit parameter. While it certainly looks feasible (all the needed info is here), it isn't implemented yet (2.9.0.1), as I believe manifests are now either inserted locally if the compiler has all the static type info it needs or looked up in the implicit scope, but not generated from other (possibly implicitly available) manifests.

What you can do, however, is construct that manifest yourself with the methods on the companion object:

scala> import reflect.Manifest
scala> Manifest.classType(classOf[List[_]], manifest[Int])
res0: scala.reflect.Manifest[List[_]] = scala.collection.immutable.List[Int]

So you can implement makeListManifest yourself:

scala> def makeListManifest[T](m: Manifest[T]) = Manifest.classType(classOf[List[_]], m)            
makeListManifest: [T](m: scala.reflect.Manifest[T])scala.reflect.Manifest[List[_]]

Note that although the right manifest will be returned, the static return type of makeListManifest is only Manifest[List[_]]. But you could safely cast to Manifest[List[T]] here.

Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
  • I think you simply forgot the implicit modifier in your argument list. This works for me: `def makeListManifest[T: Manifest] = manifest[List[T]]` – Adriaan Moors Jul 04 '11 at 10:33
5

Note that Scala's type system allows you to do better than returning Any. There are several ways to define type unions (a.k.a. "disjunctive types"). See e.g.

Of course you can also have your own ADT for the return type, which is IMHO the cleanest solution:

trait ReturnValue   
case class ReturnInt(value: Int) extends ReturnValue
case class ReturnString(value: String) extends ReturnValue
case class ReturnIntList(value: List[Int]) extends ReturnValue
...

[Edit]

Going with the definition from the second link, we could write:

def contrivedExample(t: String): String or List[Int] or List[List[String]] = t match {
  case "String" => "test"
  case "List[Int]" => List(1,2,3)
  case "List[List[String]]" => List(List("a","b"),List("c","d"))
}

Now we can retrieve verbosely but safely the type:

def otherContrivedExample(t: String or List[Int] or List[List[String]]) = t match {  
  case DisjointType(Some(DisjointType(Some(s),_)), _) => println("processing String: " + s)  
  case DisjointType(Some(DisjointType(_,Some(s))), _) => println("processing List[String]: " + s)  
  case DisjointType(_,Some(s)) => println("processing List[List[Int]]: head=" + s.head)  
}

val x =  contrivedExample("List[List[String]]")
otherContrivedExample(x)
//--> processing List[List[Int]]: head=List(a, b)

As you can see that the matched variable s has the right type, despite we didn't mention it. I'm pretty sure the extraction process could be simplified by using implicit magic and / or special extractors.

Community
  • 1
  • 1
Landei
  • 54,104
  • 13
  • 100
  • 195
  • I'd be interested if you could elaborate a bit more on how you'd use what is described in one of your links to implement a union type as a return type for a method. – Jean-Philippe Pellet Jul 04 '11 at 08:22
  • Thanks a lot! Too bad the pattern-matching part is so verbose. – Jean-Philippe Pellet Jul 04 '11 at 10:34
  • This is really great. The articles on implementing union types are incredible as is the solution. However, the actual nature of the problem I have is recursive. I may know the types when I start processing (where union will help a lot), but runtime logic will determine whether a list or map of these types are created and to what depth. That can't be defined with a union statically unless i predefine a preset number of combinations, but it can be tracked in a manifest... – Mike Jul 05 '11 at 06:03
1

I think because your function returns Manifest[_], the compiler has lost the necessary information to recover the type. If m was of type Manifest[List[Int]] that would be a different story.

huynhjl
  • 41,520
  • 14
  • 105
  • 158