scala-2.10

Is it possible to generate Apply from WeakTypeTag inside a scala macro?

女生的网名这么多〃 提交于 2019-12-30 18:59:50
问题 I have a WeakTypeTag of some type in my macro, and I want to generate code as follows: macroCreate[SomeObject] // => SomeObject(1) The definition of a macro will be something like this: def macroCreate[A] = macro _macroCreate[A] def _macroCreate[A](c: Context)(implicit wtt: c.WeakTypeTag[A]) = { c.Expr(Apply(Select(???, newTermName("apply")), List(c.literal(1).tree))) } The problem is, how do I get Select for the given type? I can use a workaround of converting the type to string, splitting

Runtime resolution of type arguments using scala 2.10 reflection

有些话、适合烂在心里 提交于 2019-12-30 06:23:52
问题 Given a type declaration, I am able to resolve the type argument. scala> reflect.runtime.universe.typeOf[List[Int]] match {case x:TypeRef => x.args} res10: List[reflect.runtime.universe.Type] = List(Int) For a runtime value, The same method doesn't work. scala> reflect.runtime.currentMirror.reflect(List(42)).symbol.toType match {case x:TypeRef => x.args} res11: List[reflect.runtime.universe.Type] = List(B) Is there a way to overcome the type erasure for reflected values? 回答1: An example based

What does “reflective access of structural type member method should be enabled…” warning mean in Scala?

隐身守侯 提交于 2019-12-30 05:52:47
问题 After switching to Scala 2.10 I get tons of warnings: reflective access of structural type member method ... should be enabled by making the implicit value language.reflectiveCalls visible What does it mean? 回答1: The warning actually tells where to look in the documentation for an explanation: Test.scala:9: warning: reflective access of structural type member method y should be enabled by making the implicit value language.reflectiveCalls visible. This can be achieved by adding the import

How are lazy val class variables implemented in Scala 2.10?

邮差的信 提交于 2019-12-28 05:58:30
问题 This answer to What's the (hidden) cost of Scala's lazy val? shows how they were implemented in Scala 2.7. But as the comments say, this must have changed since then, so I'm curious, what's the current (2.10) implementation of class lazy val variables? 回答1: Compiled this with scala 2.10.2: class Foo { lazy val bar = math.pow(5, 3) } Then decompiled the result with JD-GUI: import scala.math.package.; import scala.reflect.ScalaSignature; @ScalaSignature(bytes="\006\001e1A!\001\002\001\013\t

Scala generic method - No ClassTag available for T

六月ゝ 毕业季﹏ 提交于 2019-12-28 01:51:11
问题 I'm relatively new to Scala and am trying to define a generic object method. However, when I refer to the parameterized type within the method I am getting "No ClassTag available for T". Here is a contrived example that illustrates the problem. scala> def foo[T](count: Int, value: T): Array[T] = Array.fill[T](count)(value) <console>:7: error: No ClassTag available for T def foo[T](count: Int, value: T): Array[T] = Array.fill[T](count)(value) ^ Thanks in advance for help in understanding what

How to Sum a part of a list in RDD

自闭症网瘾萝莉.ら 提交于 2019-12-25 18:34:28
问题 I have an RDD, and I would like to sum a part of the list. (key, element2 + element3) (1, List(2.0, 3.0, 4.0, 5.0)), (2, List(1.0, -1.0, -2.0, -3.0)) output should look like this, (1, 7.0), (2, -3.0) Thanks 回答1: You can map and indexing on the second part: yourRddOfTuples.map(tuple => {val list = tuple._2; list(1) + list(2)}) Update after your comment, convert it to Vector : yourRddOfTuples.map(tuple => {val vs = tuple._2.toVector; vs(1) + vs(2)}) Or if you do not want to use conversions:

Spark sorting of delimited data

纵饮孤独 提交于 2019-12-24 04:33:53
问题 I am new to Spark. Can you give any idea what is the problem with below code: val rawData="""USA | E001 | ABC DE | 19850607 | IT | $100 UK | E005 | CHAN CL | 19870512 | OP | $200 USA | E003 | XYZ AB | 19890101 | IT | $250 USA | E002 | XYZ AB | 19890705 | IT | $200""" val sc = ... val data= rawData.split("\n") val rdd= sc.parallelize(data) val data1=rdd.flatMap(line=> line.split(" | ")) val data2 = data1.map(arr => (arr(2), arr.mkString(""))).sortByKey(false) data2.saveAsTextFile("./sample

Reflection: Show bytecode type signature of static types

早过忘川 提交于 2019-12-24 03:21:11
问题 Is it possible to show the full type signature (with erased parameterized types) saved in the bytecode with the new Reflection library? For example the type Any => Unit should be displayed as "scala.Function1<java.lang.Object,scala.runtime.BoxedUnit>" because that's the type stored in the bytecode. It is possible to show this type with javap . First one needs to compile some code with scalac : object X { def m(f: Any => Unit) = f } The command javap -c -s -l -verbose X$ shows: ... const #25 =

Reflection: Show bytecode type signature of static types

两盒软妹~` 提交于 2019-12-24 03:20:04
问题 Is it possible to show the full type signature (with erased parameterized types) saved in the bytecode with the new Reflection library? For example the type Any => Unit should be displayed as "scala.Function1<java.lang.Object,scala.runtime.BoxedUnit>" because that's the type stored in the bytecode. It is possible to show this type with javap . First one needs to compile some code with scalac : object X { def m(f: Any => Unit) = f } The command javap -c -s -l -verbose X$ shows: ... const #25 =

Understanding Scala -> syntax

喜夏-厌秋 提交于 2019-12-24 03:19:10
问题 I am getting a taste of Scala through the artima "Programming in Scala" book. While presenting the Map traits, the authors go to some lengths to describe the -> syntax as a method that can be applied to any type to get a tuple. And indeed: scala> (2->"two") res1: (Int, String) = (2,two) scala> (2,"two") res2: (Int, String) = (2,two) scala> (2->"two") == (2, "two") res3: Boolean = true But those are not equivalent: scala> Map(1->"one") + (2->"two") res4: scala.collection.immutable.Map[Int