On 24 jun, 12:28, Eugene Burmako <
eugene.burm...@epfl.ch> wrote:
> In the example above, TypeTag, as a regular context bound, will be stored
> in a synthetic implicit evidence. The value that will be passed into that
> implicit parameter will be a factory that is capable of creating the exact
> type that was there during compilation.
>
Yes indeed, and ErasureTag and ConcreteTypeTag give at least a compile
time error about it, but TypeTag not.
Probably the implicit evidences are created for TypeTag(Int, Double,
String) and for ClassTag(Int, Double) but they aren't working
and for ClassTag the @specialized is not working to work around box/
unboxed jvm types.
package reifiedpattern
import reflect._
class TestClassTag[@specialized(Int,Double) A:ClassTag]{
def m(x: Any) = x match { case _:A => println("Found match " + x +
"(" + x.getClass.getName + ", " + classTag[A] + ")")
case _ => println("No match: " +
x.getClass.getName + ", " + classTag[A])
}
}
class TestErasureTag[@specialized(Int,Double) A:ErasureTag]{
def m(x: Any) = x match { case _:A => println("Found match " + x +
"(" + x.getClass.getName)
case _ => println("No match: " +
x.getClass.getName)
}
}
class TestTypeTag[@specialized(Int,Double) A:TypeTag]{
def m(x: Any) = x match { case _:A => println("Found match " + x +
"(" + x.getClass.getName)
case _ => println("No match: " +
x.getClass.getName)
}
}
class TestConcreteTypeTag[@specialized(Int,Double) A:ConcreteTypeTag]{
def m(x: Any) = x match { case _:A => println("Found match " + x +
"(" + x.getClass.getName)
case _ => println("No match: " +
x.getClass.getName)
}
}
object Main extends App {
println("Testing ClassTag")
val testClassTag = new TestClassTag[Int]
testClassTag.m(1)
testClassTag.m(2.0)
testClassTag.m("test")
val testClassTag2 = new TestClassTag[Double]
testClassTag2.m(1)
testClassTag2.m(2.0)
testClassTag2.m("test")
val testClassTag3 = new TestClassTag[String]
testClassTag3.m(1)
testClassTag3.m(2.0)
testClassTag3.m("test")
println("Testing ErasureTag")
val testErasureTag = new TestErasureTag[Int]
testErasureTag.m(1)
testErasureTag.m(2.0)
testErasureTag.m("test")
val testErasureTag2 = new TestErasureTag[Double]
testErasureTag2.m(1)
testErasureTag2.m(2.0)
testErasureTag2.m("test")
val testErasureTag3 = new TestErasureTag[String]
testErasureTag3.m(1)
testErasureTag3.m(2.0)
testErasureTag3.m("test")
println("Testing TypeTag")
val testTypeTag = new TestTypeTag[Int]
testTypeTag.m(1)
testTypeTag.m(2.0)
testTypeTag.m("test")
val testTypeTag2 = new TestTypeTag[Double]
testTypeTag2.m(1)
testTypeTag2.m(2.0)
testTypeTag2.m("test")
val testTypeTag3 = new TestTypeTag[String]
testTypeTag3.m(1)
testTypeTag3.m(2.0)
testTypeTag3.m("test")
println("Testing ConcreteTypeTag")
val testConcreteTypeTag = new TestConcreteTypeTag[Int]
testConcreteTypeTag.m(1)
testConcreteTypeTag.m(2.0)
testConcreteTypeTag.m("test")
val testConcreteTypeTag2 = new TestConcreteTypeTag[Double]
testConcreteTypeTag2.m(1)
testConcreteTypeTag2.m(2.0)
testConcreteTypeTag2.m("test")
val testConcreteTypeTag3 = new TestConcreteTypeTag[String]
testConcreteTypeTag3.m(1)
testConcreteTypeTag3.m(2.0)
testConcreteTypeTag3.m("test")
}
/*
C:\scala-2.10.0-M4\myexamples2>scalac -unchecked reifiedpattern.scala
reifiedpattern.scala:12: warning: abstract type A in type pattern A is
unchecked
since it is eliminated by erasure
def m(x: Any) = x match { case _:A => println("Found match " + x +
"(" + x.get
Class.getName)
^
reifiedpattern.scala:20: warning: abstract type A in type pattern A is
unchecked
since it is eliminated by erasure
def m(x: Any) = x match { case _:A => println("Found match " + x +
"(" + x.get
Class.getName)
^
reifiedpattern.scala:28: warning: abstract type A in type pattern A is
unchecked
since it is eliminated by erasure
def m(x: Any) = x match { case _:A => println("Found match " + x +
"(" + x.get
Class.getName)
^
reifiedpattern.scala:53: error: could not find implicit value for
evidence param
eter of type scala.reflect.ErasureTag[Int]
val testErasureTag = new TestErasureTag[Int]
^
reifiedpattern.scala:58: error: could not find implicit value for
evidence param
eter of type scala.reflect.ErasureTag[Double]
val testErasureTag2 = new TestErasureTag[Double]
^
reifiedpattern.scala:63: error: could not find implicit value for
evidence param
eter of type scala.reflect.ErasureTag[String]
val testErasureTag3 = new TestErasureTag[String]
^
reifiedpattern.scala:88: error: could not find implicit value for
evidence param
eter of type scala.reflect.ConcreteTypeTag[Int]
val testConcreteTypeTag = new TestConcreteTypeTag[Int]
^
reifiedpattern.scala:93: error: could not find implicit value for
evidence param
eter of type scala.reflect.ConcreteTypeTag[Double]
val testConcreteTypeTag2 = new TestConcreteTypeTag[Double]
^
reifiedpattern.scala:98: error: could not find implicit value for
evidence param
eter of type scala.reflect.ConcreteTypeTag[String]
val testConcreteTypeTag3 = new TestConcreteTypeTag[String]
^
three warnings found
6 errors found
*/