scala-2.10

Alternative to Scala REPL breakIf in 2.10

青春壹個敷衍的年華 提交于 2019-12-02 17:55:29
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? Perhaps the idea is that you should just work with the ILoop directly? As far as I can tell, it shouldn't be much more complex than: // insert the code below wherever you want a REPL val repl = new

How do I get type parameter from type with type parameter, inside scala macro?

江枫思渺然 提交于 2019-12-02 04:26:47
问题 I have a type (that, in this case, represents return type of a method), and it is of the form List[Int] (for example). I know that the type carries that information, because toString results in the proper value, but how can I extract that type? I tried .typeSymbol , but that loses all type information altogether. 回答1: You can use the TypeRef extractor: import reflect.runtime.universe._ // or in a macro "c.universe._" val tpe = weakTypeOf[List[Int]] // extract type parameters val TypeRef(_,_,

How do I get type parameter from type with type parameter, inside scala macro?

这一生的挚爱 提交于 2019-12-02 02:31:58
I have a type (that, in this case, represents return type of a method), and it is of the form List[Int] (for example). I know that the type carries that information, because toString results in the proper value, but how can I extract that type? I tried .typeSymbol , but that loses all type information altogether. You can use the TypeRef extractor: import reflect.runtime.universe._ // or in a macro "c.universe._" val tpe = weakTypeOf[List[Int]] // extract type parameters val TypeRef(_,_, tps) = tpe // tps has type List[Type] println(tps.head) // Int 来源: https://stackoverflow.com/questions

Scala type inference on an existential type

可紊 提交于 2019-12-01 22:29:39
问题 Consider the following code snippet, which is a reduced version of my original problem: case class RandomVariable[A](values: List[A]) case class Assignment[A](variable: RandomVariable[A], value: A) def enumerateAll(vars: List[RandomVariable[_]], evidence: List[Assignment[_]]): Double = vars match { case variable :: tail => val enumerated = for {value <- variable.values extendedEvidence = evidence :+ Assignment(variable, value) } yield enumerateAll(tail, extendedEvidence) enumerated.sum case

Scala type inference on an existential type

老子叫甜甜 提交于 2019-12-01 22:13:58
Consider the following code snippet, which is a reduced version of my original problem: case class RandomVariable[A](values: List[A]) case class Assignment[A](variable: RandomVariable[A], value: A) def enumerateAll(vars: List[RandomVariable[_]], evidence: List[Assignment[_]]): Double = vars match { case variable :: tail => val enumerated = for {value <- variable.values extendedEvidence = evidence :+ Assignment(variable, value) } yield enumerateAll(tail, extendedEvidence) enumerated.sum case Nil => 1.0 } This fails with the compile-time error that variable was inferred to have type

scala macros: Add function to class

我们两清 提交于 2019-12-01 19:39:11
I'm new to scala macros and I'm using scala 2.10.0-RC3. I want to write a macro that adds a function to a class. Usage example: trait MyTrait { def addF = macro { /*add "def f = 3" to class*/ } } class MyClass extends MyTrait { addF //Adds the "def f" to MyClass } object Main { val t = new MyClass assert(t.f==3) } I need this in the following scenario. My first try didn't use macros but didn't work, because I can't inherit the same trait twice. trait AddF[T] { def f(t: T) { /* ...do sthg ... */ } } class MyClass extends AddF[Int] with AddF[String] With the macro solution I could write class

scala macros: Add function to class

南楼画角 提交于 2019-12-01 19:16:56
问题 I'm new to scala macros and I'm using scala 2.10.0-RC3. I want to write a macro that adds a function to a class. Usage example: trait MyTrait { def addF = macro { /*add "def f = 3" to class*/ } } class MyClass extends MyTrait { addF //Adds the "def f" to MyClass } object Main { val t = new MyClass assert(t.f==3) } I need this in the following scenario. My first try didn't use macros but didn't work, because I can't inherit the same trait twice. trait AddF[T] { def f(t: T) { /* ...do sthg ...

Macros: path dependent type inference confusion

戏子无情 提交于 2019-12-01 08:59:27
I tried to simplify the creation of ASTs, but got a weird error message: case class Box(i: Int) object M { import language.experimental.macros import scala.reflect.makro.Context case class meth(obj: String, method: String)(implicit val c: Context) { import c.universe._ def apply(xs: Tree*) = Apply(Select(Ident(obj), newTermName(method)), xs.toList) } def box(n: Int): Box = macro boxImpl def boxImpl(c: Context)(n: c.Expr[Int]): c.Expr[Box] = { import c.universe._ implicit val cc: c.type = c n.tree match { case arg @ Literal(Constant(_)) => meth("Box", "apply").apply(arg) } } } Error: <console>

How to get constructor arguments in a method using typetags/mirrors?

泄露秘密 提交于 2019-12-01 08:20:11
For Case Class: case class MyClass(param1: String, param2: String) Why does this reflective method: import scala.reflect.runtime.universe._ import scala.reflect.runtime.currentMirror import scala.reflect.runtime.{ universe => ru } def getSettings[T](paramObj: T)(implicit tag: TypeTag[T]) { val m = ru.runtimeMirror(getClass.getClassLoader) val classType = ru.typeOf[T].typeSymbol.asClass val cm = m.reflectClass(classType) val constructor = tag.tpe.declaration(ru.nme.CONSTRUCTOR).asMethod val constructorMethod = cm.reflectConstructor(constructor) val args = constructor.asMethod.paramss.head map {

How to get constructor arguments in a method using typetags/mirrors?

末鹿安然 提交于 2019-12-01 06:37:48
问题 For Case Class: case class MyClass(param1: String, param2: String) Why does this reflective method: import scala.reflect.runtime.universe._ import scala.reflect.runtime.currentMirror import scala.reflect.runtime.{ universe => ru } def getSettings[T](paramObj: T)(implicit tag: TypeTag[T]) { val m = ru.runtimeMirror(getClass.getClassLoader) val classType = ru.typeOf[T].typeSymbol.asClass val cm = m.reflectClass(classType) val constructor = tag.tpe.declaration(ru.nme.CONSTRUCTOR).asMethod val