scala-2.10

Why does TypeTag not work for return types?

百般思念 提交于 2019-12-10 18:57:01
问题 It seems that TypeTags only work for type parameters that are used in the parameters of the called method, and not the return type: scala> :paste // Entering paste mode (ctrl-D to finish) import scala.reflect.runtime.universe._ object Test { def withParam[T: TypeTag](v: T): T = { println(typeOf[T]) 0.asInstanceOf[T] } def justReturn[T: TypeTag](): T = { println(typeOf[T]) 0.asInstanceOf[T] } } // Exiting paste mode, now interpreting. import scala.reflect.runtime.universe._ defined module Test

what is magic of Scala Array.apply

允我心安 提交于 2019-12-10 18:49:53
问题 From array.scala of scala-2.10.4, The Array is defined as final class Array[T](_length: Int) extends java.io.Serializable with java.lang.Cloneable { /** The length of the array */ def length: Int = throw new Error() def apply(i: Int): T = throw new Error() def update(i: Int, x: T) { throw new Error() } override def clone(): Array[T] = throw new Error() } Please note, the apply method will throw an exception! And for the accompany object Arrry, I find the following codes: def apply[T: ClassTag

Can Scala 2.10 reflection emulate this Javassist functionality?

最后都变了- 提交于 2019-12-10 18:16:26
问题 I would like to know if it is possible to rewrite this function using Scala-2.10 reflection instead of Javassist: def adaptClass(name1: String, name2: String) : Class[_] = { import javassist._ val cls = ClassPool.getDefault().getAndRename(name1, name2) val field = CtField.make("private static final long serialVersionUID = 1L;", cls)) cls.addField(field, cls)) cls.toClass() } I am most interested in the part that adds a SerialVersionUID field to the new class, since that part of the above code

Is there anyway we can give two conditions in Scalatest using ShouldMatchers

自作多情 提交于 2019-12-10 11:15:29
问题 How can I do something like this? Check for two conditions while testing // b is Option[Array[Int]] b should be ('empty) || b.get should be ('empty) I want to do it using ShouldMatchers instead of assert, since ShouldMatchers is part of scalatest. 回答1: You should be able to do val b: Option[Array[Int]] = ??? b should (be ('empty) or be (Some(Array.empty[Int])) See this section of the scalatest manual: Logical Expressions 来源: https://stackoverflow.com/questions/27964790/is-there-anyway-we-can

Add custom compile time checks to Scala

五迷三道 提交于 2019-12-10 10:44:06
问题 Suppose I have the following Scala code: sealed trait Foo sealed trait Bar object Foo1 extends Foo object Foo2 extends Foo object Foo3 extends Foo object Bar1 extends Bar object Bar2 extends Bar object Bar3 extends Bar case class Hello(foo:Foo, bar:Bar) val a = Hello(Foo1, Bar2) // allowed val b = Hello(Foo2, Bar2) // suppose not allowed I need to catch at compile time if any incompatible combination is applied to Hello . Suppose only the following combinations are allowed: (Foo1, Bar1) ,

Checking for varargs type ascription in Scala macros

久未见 提交于 2019-12-10 03:26:38
问题 Suppose I have this macro: import language.experimental.macros import scala.reflect.macros.Context object FooExample { def foo[A](xs: A*): Int = macro foo_impl[A] def foo_impl[A](c: Context)(xs: c.Expr[A]*) = c.literal(xs.size) } This works as expected with "real" varargs: scala> FooExample.foo(1, 2, 3) res0: Int = 3 But the behavior with a sequence ascribed to the varargs type is confusing to me (in Scala 2.10.0-RC3): scala> FooExample.foo(List(1, 2, 3): _*) res1: Int = 1 And to show that

How to distinguish compiler-inferred implicit conversion from explicitly invoked one?

99封情书 提交于 2019-12-10 03:12:27
问题 Let's imagine passing these two equivalent expressions to a Scala macro: with compiler-inferred implicit conversion: 1+"foo" with explicitly invoked implicit conversion: any2stringadd(1)+"foo" Is there a way to distinguish between these two inside the macro? 回答1: First of all, the 1 + "foo" case is going to be tricky because there isn't actually any implicit conversion happening there: Int itself really, truly does have this + method (unfortunately). So you're out of luck if that's your use

Is it possible to write a scala macro whose returntype depends on argument?

无人久伴 提交于 2019-12-09 23:22:14
问题 For a DSL I would like to be able to do something like: object Creator { def create[T](s :String) :Foo[T] = macro createImpl[T] def createImpl[T](c :Context)(s :c.Expr[String]) : c.Expr[Foo[T]] = { reify(new Foo[Any]()) } } My problem is to replace the Any in reify with something that will return the correctly parametrized version. (above I use a string argument, but in the final version I plan to use the companion object of class T as a marker to know the argument-type of a Function1[T,Unit]

String interpolation and macro: how to get the StringContext and expression locations

与世无争的帅哥 提交于 2019-12-09 08:18:42
问题 I'm trying to implement a custom string interpolation method with a macro and I need some guidance on using the API. Here is what I want to do: /** expected * LocatedPieces(List(("\nHello ", Place("world"), Position()), ("\nHow are you, ", Name("Eric"), Position(...))) */ val locatedPieces: LocatedPieces = s2""" Hello $place How are you, $name """ val place: Piece = Place("world") val name: Piece = Name("Eric") trait Piece case class Place(p: String) extends Piece case class Name(n: String)

Polymorphic instantiation in Scala using TypeTag and ClassTag

旧巷老猫 提交于 2019-12-09 06:27:40
问题 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