Is there any way to match just on generic type passed in function?
I'd like to do:
def getValue[T](cursor: Cursor, columnName: String): T = {
val index = cursor.getColumnIndex(columnName)
T match {
case String => cursor.getString(index)
case Int => cursor.getInteger(index)
}
I thought about something like classOf or typeOf, but none of them is acceptable for just types, but objects.
My idea was also to create some object of type T and then check its type, but I think there can be a better solution.
You could use ClassTag.
val string = implicitly[ClassTag[String]]
def getValue[T : ClassTag] =
implicitly[ClassTag[T]] match {
case `string` => "String"
case ClassTag.Int => "Int"
case _ => "Other"
}
Or TypeTag:
import scala.reflect.runtime.universe.{TypeTag, typeOf}
def getValue[T : TypeTag] =
if (typeOf[T] =:= typeOf[String])
"String"
else if (typeOf[T] =:= typeOf[Int])
"Int"
else
"Other"
Usage:
scala> getValue[String]
res0: String = String
scala> getValue[Int]
res1: String = Int
scala> getValue[Long]
res2: String = Other
If you are using 2.9.x you should use Manifest:
import scala.reflect.Manifest
def getValue[T : Manifest] =
if (manifest[T] == manifest[String])
"String"
else if (manifest[T] == manifest[Int])
"Int"
else
"Other"
Related
In scala, a pattern matching for a class has to be conducted in the following way:
val clz: Class[_] = ???
clz match {
case v if clz == classOf[String] =>
// do something
case v if clz == classOf[Int] =>
// do something
//...
}
The boilerplate code v if clz == is really redundant and I'd like to have them removed or reduced, since functions like classOf[String] and classOf[int] can be inlined and used like constant. So how can I do this?
There is some support, mostly in relation to array element types:
scala> import reflect.ClassTag
import reflect.ClassTag
scala> val c: Class[_] = classOf[Int]
c: Class[_] = int
scala> (ClassTag(c): Any) match { case ClassTag.Boolean => "bool" case ClassTag.Int => "int" }
res0: String = int
but the use case is to simplify type tests
scala> def f[A: ClassTag] = ("abc": Any) match { case _: A => "A" case _ => "other" }
f: [A](implicit evidence$1: scala.reflect.ClassTag[A])String
scala> f[Int]
res1: String = other
scala> f[String]
res2: String = A
Maybe one argument for classTag not looking like a stable id is that classOf[C] evaluated in different classloaders don't compare equal.
I've defined a simple generic macro:
object MyMacro {
def readWrite[T](readParse: String => T, label: String, format: T => String): Unit = macro readWriteImpl[T]
def readWriteImpl[T: c.WeakTypeTag](c: Context)(readParse: c.Expr[String => T], label: c.Expr[String], format: c.Expr[T => String]): c.Tree = {
import c.universe._
q"""
def read[WIRE](path: Path, reader: Transceiver[WIRE], isMapKey: Boolean = false): T =
reader.readString(path) match {
case null => null.asInstanceOf[T]
case s => Try( $readParse(s) ) match {
case Success(d) => d
case Failure(u) => throw new ReadMalformedError(path, "Failed to parse "+$label+" from input '"+s+"'", List.empty[String], u)
}
}
def write[WIRE](t: T, writer: Transceiver[WIRE], out: Builder[Any, WIRE]): Unit =
t match {
case null => writer.writeNull(out)
case _ => writer.writeString($format(t), out)
}
"""
}
}
In a separate compilation unit I use it like this:
object DurationTypeAdapterFactory extends TypeAdapter.=:=[Duration] {
MyMacro.readWrite[Duration]((s: String) => Duration.parse(s), "Duration", (t: Duration) => t.toString)
}
When built, the compiler complains it doesn't know about T:
[error] /Users/me/git/ScalaJack/core/src/main/scala/co.blocke.scalajack/typeadapter/TimePrimitives.scala:13:30: not found: type T
[error] MyMacro.readWrite[Duration]((s: String) => Duration.parse(s), "Duration", (t: Duration) => t.toString)
[error]
It doesn't like the 'T' references in my quasiquote, which I kinda understand. How can I represent the T paraeter passed into readWriteImpl inside the quasiquote so that it unpacks properly?
Use the tag to inspect the type or compare it to other types using =:=, or use it in the expansion.
For example,
scala 2.13.0-M5> def fImpl[A: c.WeakTypeTag](c: Context)(a: c.Expr[A]) = { import c._, universe._
| q"null.asInstanceOf[${implicitly[c.WeakTypeTag[A]].tpe}]" }
fImpl: [A](c: scala.reflect.macros.blackbox.Context)(a: c.Expr[A])(implicit evidence$1: c.WeakTypeTag[A])c.universe.Tree
scala 2.13.0-M5> import language.experimental.macros ; def f[A](a: A): A = macro fImpl[A]
import language.experimental.macros
defined term macro f: [A](a: A)A
scala 2.13.0-M5> f(42)
res2: Int = 0
scala 2.13.0-M5> f("")
res3: String = null
Given
a value of type Any
a TypeTag corresponding to the desired type
How can I cast the value
Unfortunately, the following snippet doesn't compile
val v: Any = 123
val tag = typeTag[Int]
val i = v.asInstanceOf[t.tpe]
Use this
import scala.reflect.ClassTag
def getTypedArg[T: ClassTag](any: Any): Option[T] = {
any match {
case t: T => Some(t)
case invalid =>
None
}
}
Usage
scala> getTypedArg[Int](5)
res1: Option[Int] = Some(5)
scala> getTypedArg[Int]("str")
res2: Option[Int] = None
Source: Retrieve class-name from ClassTag
EDIT-1
As asked by #Aki, it can be made to work with TypeTags too, with this hack
import reflect.runtime.universe._
import scala.reflect.ClassTag
def typeToClassTag[T: TypeTag]: ClassTag[T] = {
ClassTag[T]( typeTag[T].mirror.runtimeClass( typeTag[T].tpe ) )
}
def getTypedArg2[T: TypeTag](any: Any): Option[T] = {
implicit val c: ClassTag[T] = typeToClassTag[T]
any match {
case t: T => Some(t)
case invalid =>
None
}
}
Reference: How to get ClassTag form TypeTag, or both at same time?
You could write your own method that does the casting.
(note that this method will never throw ClassCastExceptions)
def cast[A](a: Any, tag: TypeTag[A]): A = a.asInstanceOf[A]
To parse a string to an int we can do theString.toInt, and to a boolean theString.toBoolean. How can I make this generic?
I want to somehow parameterise a function so that I can try to parse the string as either a boolean or int, handle errors and return defaults on error, etc.
I've got this:
def tryParsing[T: TypeTag](value: String)(implicit errorAccumulator: Accumulator[Int]): Option[T] = {
// scala's .toBoolean only permits exactly "true" or "false", not numeric booleans.
val validBooleans = Map(
"true" -> true,
"false" -> false,
"1" -> true,
"0" -> false
)
import scala.reflect.runtime.universe._
// doesn't work. Also, using TypeTag doesn't seem to work.
typeOf[T] match {
case t if t <:< typeOf[Boolean] =>
val result = validBooleans.get(value.asInstanceOf[String].toLowerCase)
if (result.isEmpty) {
logger.warn(s"An error occurred parsing the boolean value `$value`")
errorAccumulator += 1
}
result.asInstanceOf[Option[T]]
case _ =>
Try(value.asInstanceOf[T]) match {
case Success(x) => Some(x: T)
case Failure(e) =>
logger.warn(s"An parsing error occurred: $e")
errorAccumulator += 1
None
}
}
}
I can't match on the type tag. I guess this is because if value was of type T then the typetag would prevent its type from being erased. But here, I want to call this like:
tryParsing[Boolean]("1") // value from variable
How can I match on types or otherwise do what I'm trying to do in scala 2.10?
Use the typeclass pattern:
trait Parser[T] {
def parse(input: String): Option[T]
}
def parse[T](input: String)(implicit parser: Parser[T]): Option[T] =
parser.parse(input)
import util.Try
implicit object IntParser extends Parser[Int] {
def parse(input: String) = Try(input.toInt).toOption
}
implicit object BooleanParser extends Parser[Boolean] {
def parse(input: String) = Try(input.toBoolean).toOption
}
Voila:
scala> parse[Int]("3")
res0: Option[Int] = Some(3)
scala> parse[Int]("zzz")
res1: Option[Int] = None
scala> parse[Boolean]("true")
res2: Option[Boolean] = Some(true)
scala> parse[Boolean]("zzz")
res3: Option[Boolean] = None
I would like to do something like this:
implicit class MyString(s: String) {
def getAs[T]: T = {
T match {
case q if q == classOf[Int] => s.toInt
case q if q == classOf[Boolean] => s.toBoolean
}
}
}
This doesn't compile, of course. How do I write this so it does?
Consider this approach:
object Parse {
def parse[T](f:String => Option[T]) = f
implicit val parseInt = parse(s => Try(s.toInt).toOption)
implicit val parseLong = parse(s => Try(s.toLong).toOption)
implicit val parseDouble = parse(s => Try(s.toDouble).toOption)
implicit val parseBoolean = parse(s => Try(s.toBoolean).toOption)
}
implicit class MyString(s:String) {
def getAs[T]()(implicit run: String => Option[T]): Option[T] = run(s)
}
Usage:
def main(args: Array[String]) {
import Parse._
"true".getAs[Boolean].foreach(println)
"12345".getAs[Int].foreach(println)
}
When use classOf for type match, there maybe will have some issues, example:
scala> classOf[List[Int]] == classOf[List[String]]
res17: Boolean = true
scala> typeOf[List[Int]] =:= typeOf[List[String]]
res18: Boolean = false
classOf only store the class information not with generics type
typeOf will store full type information
TypeTags and Manifests
class MyString(s: String) {
def getAs[T](implicit tag: TypeTag[T]): T = {
tag.tpe match {
case t if t =:= typeOf[Int] => s.toInt.asInstanceOf[T]
}
}
}
scala> new MyString("123")
res2: MyString = MyString#7fca02
scala> res6.getAs[Int]
res3: Int = 123
use TypeType tag to get type info, and call getAs with type information.
This is what I have come up with so far:
import reflect.runtime.universe.TypeTag
import scala.reflection.runtime.universe._
implicit class MyString(s: String) {
def getAs[T : TypeTag]: T = {
typeOf[T] match {
case t if t =:= typeOf[Int] => s.toInt.asInstanceOf[T]
case t if t =:= typeOf[Boolean] => s.toBoolean.asInstanceOf[T]
}
}
}
Running with this in REPL:
scala> "32".getAs[Int]
res25: Int = 32
scala> "32".getAs[Boolean]
java.lang.IllegalArgumentException: For input string: "32"
at scala.collection.immutable.StringLike$class.parseBoolean(StringLike.scala:290)
at scala.collection.immutable.StringLike$class.toBoolean(StringLike.scala:260)
at scala.collection.immutable.StringOps.toBoolean(StringOps.scala:30)
at MyString.getAs(<console>:33)
... 43 elided
scala> "false".getAs[Int]
java.lang.NumberFormatException: For input string: "false"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at scala.collection.immutable.StringLike$class.toInt(StringLike.scala:272)
at scala.collection.immutable.StringOps.toInt(StringOps.scala:30)
at MyString.getAs(<console>:32)
... 43 elided
scala> "false".getAs[Boolean]
res28: Boolean = false
scala> "false".getAs[String]
scala.MatchError: String (of class scala.reflect.internal.Types$AliasNoArgsTypeRef)
at MyString.getAs(<console>:31)
... 43 elided
Better, I think, would be to return an option[T], and catch any issues such as a type T that isn't catered for, or attempting a cast that will fail. I started mucking around with scala's Try (and its .toOption method), but hit some odd errors so haven't gotten any further with that.
EDIT: just using a simple try-catch, we can get:
implicit class MyString(s: String) {
def getAs[T : TypeTag]: Option[T] = try {
typeOf[T] match {
case t if t =:= typeOf[Int] => Some(s.toInt.asInstanceOf[T])
case t if t =:= typeOf[Boolean] => Some(s.toBoolean.asInstanceOf[T])
}
} catch {
case ex: Exception => None
}
}
Resulting in:
scala> "false".getAs[String]
res30: Option[String] = None
scala> "32".getAs[Boolean]
res31: Option[Boolean] = None
scala> "32".getAs[Int]
res32: Option[Int] = Some(32)
scala> "true".getAs[Boolean]
res33: Option[Boolean] = Some(true)
scala> "true".getAs[Int]
res34: Option[Int] = None
Based on the answers given by #chengpohi and #Shadowlands, this is what I came up with.
object ImplicitsStartingWithS {
implicit class MyString(s: String) {
val str = s.trim
import reflect.runtime.universe.TypeTag
import scala.reflect.runtime.universe._
def getAs[T](implicit tag: TypeTag[T]): Option[T] = {
val value = tag.tpe match {
case t if t =:= typeOf[Int] => str.toIntStr.map(_.toInt)
case t if t =:= typeOf[Long] => str.toIntStr.map(_.toLong)
case t if t =:= typeOf[Float] => str.toNumericStr.map(_.toFloat)
case t if t =:= typeOf[Double] => str.toNumericStr.map(_.toDouble)
case _ => None
}
value.asInstanceOf[Option[T]]
}
def toDecimalStr = "^-*\\d+\\.\\d+$".r.findFirstIn(s)
def toIntStr = "^-*\\d+$".r.findFirstIn(s)
def toNumericStr = {
s.toDecimalStr match {
case Some(decimalStr) => Some(decimalStr)
case None => s.toIntStr
}
}
}
}
This avoids exception handling for faster response.