scala

nested Annotation List in Scala

Help, how do i do stuff like the following in Scala? import org.hibernate.validator.constraints.ScriptAssert @ScriptAssert.List({ @ScriptAssert(script = "...", lang = "javascript"), @ScriptAssert(script = "...", lang = "javascript")}) ...

Scala for-comprehension returning an ordered map

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 mak...

How to ensure that type parameters are different in Scala?

With the following definition it's possible to ensure the concrete type parameters are equal: trait WithEqual[T1 >: T2 <: T2, T2] So the line type A = WithEqual[Int, Int] will be legal. Now my question is: How to achieve exactly the opposite? Thus, the following line should not compile: type B = WithUnequal[Int, Int] ...

Is it possible to run AKKA on Java 1.5?

Is it possible to run AKKA on Java 1.5? I'm getting java.lang.UnsupportedClassVersionError: Bad version number in .class file for se.scalablesolutions.akka.actor.LocalActorRef. AKKA version is 0.9.1 There's noting said about the JVM version limitations at http://doc.akkasource.org/getting-started ...

Scala initialization behaviour

Please look at the following code. trait MyTrait { val myVal : String } class MyClass extends MyTrait { val myVal = "Value" } class MyClass2(val myVal: String) extends MyTrait Why does the initialization order differ in case of MyClass and MyClass2? The constructor of MyClass will be as MyClass() { MyTrait$class.$init$(this); ...

Setting target JVM in SBT

How can I set target JVM version in SBT? In Maven (with maven-scala-plugin) it can be done as follows: <plugin> ... <configuration> <scalaVersion>${scala.version}</scalaVersion> <args> <arg>-target:jvm-1.5</arg> </args> </configuration> </plugin> ...

How efficient is Scala's compiler at reusing known function results?

I'm working on a Scala-based analytics system (http://www.hiringthing.com), and I'm finding that I'm often asking myself the following question. Given a "pure" function with no side effects, if I hit that function twice with the same inputs, can I expect the compiler to reuse the value generated from the first run, or will it go through ...

Scala Map implementation keeping entries in insertion order?

In Java, I use LinkedHashMap for this purpose. The documentation of Java's LinkedHashMap is very clear that it has "predictable iteration order" and I need the same in Scala. Scala has ListMap and LinkedHashMap, but the documentation on what they do exactly is poor. Question: is Scala's LinkedHashMap or ListMap the implementation to us...

Can I use the Scala lift-json library to parse a JSON into a Map?

Is there a way to use the lift-json library's JObject class to act like a Map? For example: val json = """ { "_id" : { "$oid" : "4ca63596ae65a71dd376938e"} , "foo" : "bar" , "size" : 5} """ val record = JsonParser.parse(json) record: net.liftweb.json.JsonAST.JValue = JObject(List(JField(_id,JObject(List(JField($oid,JString(4ca63596ae6...

How to populate java.util.HashMap on the fly from Scala code?

I am unit testing java code from ScalaTest and would like to populate a java.util.HashMap within the same statement it gets declared. Is it possible to do this in Scala? ...

Scala: Generic implicit converters?

I would like to define a generic implicit converter that works for all subtypes of type T. For example: abstract class Price[A] { def price(a: Any): Int } trait Car case class Prius(year: Int) extends Car trait Food case class FriedChicken() extends Food object Def { implicit def carToPrice[A <: Car](car: A): Price[A] = new Price[...

Scala advantages after Java having closures

With closures being added to Java, what is Scala's advantage over Java as a language choice? Can someone elaborate on any advantages? ...

Scala - High heap usage when performed XML.loadFile on large number of files in local scope

I am trying to create an object tree from large number of xmls. However, when I run the following code on about 2000 xml files(ranging from 100KB to 200MB) (note that I have commented out the code that creates object tree), I get a large memory footprint of 8-9GB. I expect memory footprint to be minimum in the following example because t...

scala - creating objects without making constructor parameter a member of the object

I have class Address(elem: scala.xml.Elem){ val attr1 = (elem \ "attr1") text ... } I do not want elem to be a member of Address to keep the the footprint to minimum as I create few millions of such objects in memory. What is the scala way to achieve this? Thanks. ...

Is Scala for me? (C# developer with focus on functional / OOP style)

I want to learn a new programming language and develop for the Android platform. I'm a fulltime C# / F# - developer and I also use C# in the most functional way possible (because I like this paradigm far better than the old skool style "lets iterate and describe to the barkeeper how to make the cocktail", to quote Microsoft). However, ...

How can I pass a Scala object reference around in Java?

I want to return from a Java method a reference to a Scala object. How can I do that? My Scala objects are like this: trait Environment object LocalEnvironment extends Environment {...} object ServerEnvironment extends Environment {...} ... and I want my Java method to be like this: Environment getEnvironment() { return LocalEnvir...

scalacheck, scalatest, specs - which one has the lower barrier to entry?

Between the following test frameworks which one is the easiest to use and learn? scalacheck ScalaTest specs I'm using 2.8.0 and sbt for building, so working nicely with it is a factor. I'm not necessarily looking at the most full featured. As best I can tell my criteria are in order: allow unit testing for Scala code painless set-u...

Advantages of subtyping over typeclasses

What are the advantages of OOP subtyping over typeclasses, if any? In other words, now that we have typeclasses, is there any reason to still use OOP subtyping? PS: I am a Scala programmer. ...

How can I get around this limitation of overload resolution in Scala?

Hi, all, In Scala, the interaction of overloading and implicit argument resolution seem to make it impossible to make the following code usable. trait Bijection[A, B] extends Function1[A, B] with Unapply[A, B] { self => def apply(a: A): B def unapply(b: B): A } sealed trait Unapply[A, B] { def unapply(b: B): A } object Bijectio...

New to 2.8 collections. What would this signature look like? Similar to scalaz sequence.

I found a blog post today that mention's scalaz's sequence function. Couldn't you do something as simple as: if (l contains None) None else l If so, what would this function signature look like? contains is in SeqLike, right? Also, from the blog post I thought sequence was going to be something similar to map, but one that would br...