scala-2.8

Compile String to AST inside CompilerPlugin?

爱⌒轻易说出口 提交于 2019-12-09 17:46:26
问题 I would like to create a templating plugin and as the first step convert an arbitrary string to it's "compiled" AST representation (as the scala interpreter does, I guess). So a compiler plugin could e.g assign someString to "HELLO WORLD": @StringAnnotation("""("hello world").toString.toUpperCase""") var someString = "" My current first shot plugin does in short: runafter parser create a new representation only compiler and a VirtualFile with the annotation content compile and print unit.body

What is the most succinct Scala way to reverse a Map?

筅森魡賤 提交于 2019-12-09 16:55:35
问题 What is the most succinct Scala way to reverse a Map? The Map may contain non-unique values. EDIT: The reversal of Map[A, B] should give Map[B, Set[A]] (or a MultiMap, that would be even better). 回答1: If you can lose duplicate keys: scala> val map = Map(1->"one", 2->"two", -2->"two") map: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two), (-2,two)) scala> map.map(_ swap) res0: scala.collection.immutable.Map[java.lang.String,Int] = Map((one,1), (two,-2)) If you don't

Covariant Typeparameter in scala needs to be invariant in java interface

依然范特西╮ 提交于 2019-12-09 14:45:43
问题 I've got a trait that looks like this (some further information can be found at this related question by myself although I don't think, it's needed for this question) trait Extractor[-A,+B] { def extract(d:A):B //lots of other things } To use this in an existing java framework I would like this Extractor to either have a function that returns a Comparator[B] (being java.util.Comparator) or even better extend Comparator[A] . Now that poses a problem because Comparator s type parameter is ought

Scala 2.8 TreeMap and custom Ordering

瘦欲@ 提交于 2019-12-09 06:42:07
问题 I'm switching from scala 2.7 and ordered to scala 2.8 and using ordering. It looks quite straight forward but I was wondering could I make it a little less verbose. For example: scala> case class A(i: Int) defined class A scala> object A extends Ordering[A] { def compare(o1: A, o2: A) = o1.i - o2.i} defined module A If I then try to create a TreeMap I get an error scala> new collection.immutable.TreeMap[A, String]() <console>:10: error: could not find implicit value for parameter ordering:

How to access a field's value via reflection (Scala 2.8)

依然范特西╮ 提交于 2019-12-09 05:19:00
问题 Consider the following code: class Foo(var name: String = "bar") Now i try to get the value and the correct type of it via reflection: val foo = new Foo val field = foo.getClass.getDeclaredField("name") field.setAccessible(true) //This is where it doesn't work val value = field.get(????) I tried things like field.get(foo), but that just returns an java.lang.Object but no String. Basically I need the correct type, because I want to invoke a method on it (e. g. toCharArray). What is the

Scala program exiting before the execution and completion of all Scala Actor messages being sent. How to stop this?

谁说胖子不能爱 提交于 2019-12-08 04:53:08
问题 I am sending my Scala Actor its messages from a for loop. The scala actor is receiving the messages and getting to the job of processing them. The actors are processing cpu and disk intensive tasks such as unzipping and storing files. I deduced that the Actor part is working fine by putting in a delay Thread.sleep(200) in my message passing code in the for loop. for ( val e <- entries ) { MyActor ! new MyJob(e) Thread.sleep(100) } Now, my problem is that the program exits with a code 0 as

Scala 2.8: how to initialize child class

前提是你 提交于 2019-12-08 02:23:37
问题 Consider the following code: abstract class X { def a:Unit a } class Y extends X { var s:String = "Hello" def a:Unit = println ("String is "+s) } This gives the following output: scala> new Y String is null res6: Y = Y@18aeabe How can I get the parent class X to wait for s to be initialized when calling a 回答1: The parent's fields and the parent constructor are always initialized and run before the children's field and constructor. It means that the call to a happens before your var s is set

What is a good library for JSON serialization for Scala 2.8.1 for use in Eclipse

蓝咒 提交于 2019-12-07 17:27:05
问题 I have looked at https://github.com/debasishg/sjson and using EGit I was not able to import this code for Scala 2.8.1. Ideally, this seems to be the best library that should work, but when I loaded master it is currently empty. I am trying to use Jersey (http://jersey.java.net) to build a REST service in Scala, but the JSON serialization is where I am stuck, as I would prefer to use something written in Scala. Unfortunately there isn't anything for Scala at http://json.org/. So, how do I

Scala for-comprehension returning an ordered map

左心房为你撑大大i 提交于 2019-12-07 14:26:50
问题 How can I use a for-comprehension that returns something I can assign to an ordered Map? This is a simplification of the code I have: class Bar class Foo(val name: String, val bar: Bar) val myList: java.util.List[Foo] = ... val result: ListMap[String, Bar] = for { foo <- myList } yield (foo.name, foo.bar) I need to make sure my result is an ordered Map, in the order tuples are returned from the for-comprehension. With the above, I get the error: error: type mismatch; found : scala.collection

HowTo get the class of _ :Any

帅比萌擦擦* 提交于 2019-12-07 11:28:29
问题 I've wrapped a Message and would like to log which message I've wrapped. val any :Any = msg.wrappedMsg var result :Class[_] = null The only solution I could find is matching everything: result = any match { case x:AnyRef => x.getClass case _:Double => classOf[Double] case _:Float => classOf[Float] case _:Long => classOf[Long] case _:Int => classOf[Int] case _:Short => classOf[Short] case _:Byte => classOf[Byte] case _:Unit => classOf[Unit] case _:Boolean=> classOf[Boolean] case _:Char =>