On Wed, May 9, 2012 at 1:07 PM, Adriaan Moors <
adriaa...@epfl.ch> wrote:
>> Suppose instead you allowed Extractor patterns to have explicit
>> type arguments,
>
> yes, that's something I'd like to add during the 2.10.x cycle (both type and
> value arguments)
Cool :-)
>> The point being that we can make the dynamic type test implemented in
>> Typeable[T].cast a lot more precise than just,
>>
>> if (erasure.isAssignableFrom(x.getClass))
>> Some(x.asInstanceOf[T])
>> else None
>>
>> One possible implementation can be found in shapeless here,
>>
>>
https://github.com/milessabin/shapeless/blob/master/core/src/main/scala/shapeless/typeable.scala
>
> as far as I can see, these implicits construct Typeable instances from
> other Typeables, which is neat
> (but how is it different from how we materialize ClassTags in the compiler?
> I think it also takes into account type tags we have lying around)
>
> your cast seems to be the same as what I wrote
Well, not quite. Unless I misunderstood, you have the equivalent of,
def cast[T : ClassTag](x : Any) : Option[T] = {
val tag = implicitly[ClassTag[T]]
if(tag.erasure.isAssignableFrom(x.getClass))
Some(x.asInstanceOf[T])
else None
}
That's problematic in a lot of cases, eg.,
scala> cast[Int](23 : Any)
res9: Option[Int] = None
scala> val oops = cast[Option[String]](Option(23) : Any)
oops: Option[Option[String]] = Some(Some(23))
scala> oops.get.get
java.lang.ClassCastException: java.lang.Integer cannot be cast to
java.lang.String
On the other hand, the Typeable type class in shapeless is better behaved,
scala> import shapeless._ ; import Typeable._
import shapeless._
import Typeable._
scala> (23 : Any).cast[Int]
res0: Option[Int] = Some(23)
scala> (Option(23) : Any).cast[Option[String]]
res1: Option[Option[String]] = None
> in other words, could you explain how my proposal should be adapted to
> accommodate your use case?
The main thing is to have witnesses for element types pulled in recursively.
> it seems to me they would work together quite naturally
I do hope so :-)