scala-2.10

Scala 2.10 TypeTag usage

醉酒当歌 提交于 2019-12-03 10:29:56
问题 I'm digging new scala reflection api and can't figure out why the following snippet doesn't work as expected. Given hierarchy (tried to simplify as much as I can): import scala.reflect.runtime.universe._ trait TF[A] { implicit def t: TypeTag[A] def f[T <: A: TypeTag]: PartialFunction[Any, A] = { case msg: T if typeOf[T] =:= typeOf[A] => msg } } class TFilter[T: TypeTag] extends TF[T] { def t = typeTag[T] } case class Foo(x: Int) I expect method f to filter objects of given type. So the

Polymorphic instantiation in Scala using TypeTag and ClassTag

馋奶兔 提交于 2019-12-03 07:58:35
In Scala 2.9 one could implement polymorphic instantiation as def newInstance[T](implicit m: Manifest[T]) = m.erasure.newInstance.asInstanceOf[T] but as of 2.10 Manifest is being replaced with TypeTag , and it is not clear to me how to achieve something similar with TypeTag . I would prefer if the TypeTag version preserved all available type information. I know that the above only works for traits/classes that do not require constructor args, and ven then it does not always work, but it works well enough for what I need. If I can do better the new reflection APIs that would be great. TypeTag

Map the Exception of a failed Future

☆樱花仙子☆ 提交于 2019-12-03 06:34:37
问题 What's the cleanest way to map the Exception of a failed Future in scala? Say I have: import scala.concurrent._ import scala.concurrent.ExecutionContext.Implicits.global val f = Future { if(math.random < 0.5) 1 else throw new Exception("Oh no") } If the Future succeeds with 1 , I'd like to keep that, however if it fails I would like to change the Exception to a different Exception . The best I could come up with is transform, however that requires me to make a needless function for the

Is it possible to convert a TypeTag to a Manifest?

爱⌒轻易说出口 提交于 2019-12-03 06:02:55
Our library uses TypeTags, but now we need to interact with another library which requires Manifests. Is there any simple way to create a Manifest from a TypeTag? gourlaysama's anwer uses Class[_], thus type arguments are being erased. I've come up with an implementation that preserves the type arguments here: How to maintain type parameter during TypeTag to Manifest conversion? Here's the code: def toManifest[T:TypeTag]: Manifest[T] = { val t = typeTag[T] val mirror = t.mirror def toManifestRec(t: Type): Manifest[_] = { val clazz = ClassTag[T](mirror.runtimeClass(t)).runtimeClass if (t

How can I reuse definition (AST) subtrees in a macro?

筅森魡賤 提交于 2019-12-03 05:56:47
I am working in a Scala embedded DSL and macros are becoming a main tool for achieving my purposes. I am getting an error while trying to reuse a subtree from the incoming macro expression into the resulting one. The situation is quite complex, but (I hope) I have simplified it for its understanding. Suppose we have this code: val y = transform { val x = 3 x } println(y) // prints 3 where 'transform' is the involved macro. Although it could seem it does absolutely nothing, it is really transforming the shown block into this expression: 3 match { case x => x } It is done with this macro

How to get more information about 'feature' flag warning?

对着背影说爱祢 提交于 2019-12-03 05:34:37
When compiling an application with Play2, sometimes these kind of message appears on my terminal : [info] Compiling 1 Scala source to ~/target/scala-2.10/classes... [warn] there were 1 feature warnings; re-run with -feature for details [warn] one warning found [success] Compiled in 1s How can I get more information about those warning? It must be an option of sbt but I have no idea where to search... om-nom-nom To see the exact message you need to add "feature" flag in your sbt build definition file: scalacOptions ++= Seq("-feature") Why? Scala 2.10 not only introduced new features, but also

Alternative to Scala REPL breakIf in 2.10

不羁的心 提交于 2019-12-03 05:33:55
问题 I was reading here about using the breakIf method in the REPL code for interactive debugging, but then I found this post saying that break and breakIf were removed from ILoop in Scala 2.10. Unfortunately, that post doesn't explain why the code was removed. I'm assuming that these functions were removed because there's a better way of doing this. If that's the case, could someone please enlighten me? 回答1: Perhaps the idea is that you should just work with the ILoop directly? As far as I can

How to use Type calculated in Scala Macro in a reify clause?

坚强是说给别人听的谎言 提交于 2019-12-03 05:11:53
问题 I've been working with Scala Macros and have the following code in the macro: val fieldMemberType = fieldMember.typeSignatureIn(objectType) match { case NullaryMethodType(tpe) => tpe case _ => doesntCompile(s"$propertyName isn't a field, it must be another thing") } reify{ new TypeBuilder() { type fieldType = fieldMemberType.type } } As you can see, I've managed to get a c.universe.Type fieldMemberType . This represents the type of certain field in the object. Once I get that, I want to

How to disambiguate links to methods in scaladoc?

☆樱花仙子☆ 提交于 2019-12-03 01:11:32
I'm documenting a Scala class with overloaded methods . How can I distinguish them when referring to them in scaladoc comments? For example, if I have /** * The most important method is [[Doc.foo]]. */ object Doc { def foo[A]: A = throw new UnsupportedOperationException; def foo[A,B >: A](x: A): B = x; } and run sbt doc I get Doc.scala:1: warning: The link target "Doc.foo" is ambiguous. Several (possibly overloaded) members fit the target: method foo[A,B>:A](x:A):B in object Doc [chosen] method foo[A]:Nothing in object Doc Using foo[A,B >: A] etc. to the link doesn't work. The following seems

Scala 2.10 TypeTag usage

前提是你 提交于 2019-12-02 23:54:06
I'm digging new scala reflection api and can't figure out why the following snippet doesn't work as expected. Given hierarchy (tried to simplify as much as I can): import scala.reflect.runtime.universe._ trait TF[A] { implicit def t: TypeTag[A] def f[T <: A: TypeTag]: PartialFunction[Any, A] = { case msg: T if typeOf[T] =:= typeOf[A] => msg } } class TFilter[T: TypeTag] extends TF[T] { def t = typeTag[T] } case class Foo(x: Int) I expect method f to filter objects of given type. So the following snippet should return Seq[Foo] val messages = Seq(1, "hello", Foo(1)) val tFilter = new TFilter[Foo