scala

scala reflection: getDeclaringTrait ?

When I research a new library, I sometimes find it hard to locate the implementation of a method. In Java, Metho#getDeclaringClass provides the class that declared a given method. So by iterating over Class#getMethods, I can find for each method, the class that declared it. In Scala, traits are converted to Java interfaces and a clas...

can't extend two traits that have a method with the same signature?

Why is the error below? How to workaround it? EDIT: I assumed that since A and B compile to (interface,class) pairs, it's a matter of choosing the right static method call to implement when compiling C. I would expect the priority to be according to order. scala> trait A {def hi = println("A")} defined trait Ascala> trait A {def hi ...

erlang and scala, do they run on apache?

erlang and scala, do they run on apache? ...

Match multiple cases classes in scala

I'm doing matching against some case classes and would like to handle two of the cases in the same way. Something like this: abstract class Foo case class A extends Foo case class B(s:String) extends Foo case class C(s:String) extends Foo def matcher(l: Foo): String = { l match { case A() => "A" case B(sb) | C(sc) => "B" ...

Getting the correct scala actor sender reference when sending from an different class

Within my actor I have to create a class which sends a message to another actor. The other actor should reply back to actor A class A extends Actor { val b = new B b.start val i = new DefaultHandler() { override def fun(a: String) = { b ! payload } } someotherclass.registerHandler(i) def act = { loop {...

Regex.MatchData returning null: why not Option[String]?

Is there any particular reason why Regex.MatchData.group(i: Int): java.lang.String returns null rather than Option[String]? Is there a "Scala Way" to handle nulls in Scala? ...

What to learn after PHP? Scala or Clojure?

I have a heavy web dev background with PHP. My reasons for learning a functional programming languages are: to improve my programming skills. It was heavily suggested that learning a FPL helps. this has high priority because I want to be better and better. learn a general purpose programming language to solve tasks like scripting (OS s...

Can First-class functions in Scala be a concern for allocating a large PermGen Space in JVM?

Regarding first-class functions in Scala, it is written in the book Programming by Scala: A function literal is compiled into a class that when instantiated at run-time is a function value. When there will be many first-class functions used in a program, will this affect the JVM's PermGen space? because instead of simple functi...

Default value for generic data structure

I would like to write a SparseVector[T] class where T can be a double, an int or a boolean. The class will not be backed by an array (because I want a sparse data structure) but I have seen that when I build an empty array of an AnyVal type, the elements are initialized to the default value. For instance: scala> new Array[Int](10) re...

Unexpected Scala default values behavior

Why do the default values here behave differently when assigned explicitly to a val, versus printed directly? package blevins.example class SimpleWrap[T] { var t: T = _ def get = t } object App extends Application { val swb = new SimpleWrap[Boolean] val b = swb.get println("b: " + b) // b: false println("swb.get: " + swb....

How do you define a type for a function in Scala?

I'm hoping there is a way to define a type for a function in Scala. For example, say I want a function that takes two Ints and returns a Boolean, I could define a function that uses that like this: def checkInts(f: (Int,Int) => Boolean) = { // do stuff } Is there a way to define the type of f? Then I could do something like: def c...

What is the best book for learning Scala for an experienced programmer?

I've become very interested in Scala and see that there are several books out there at this point. Can anyone with experience with some subset of them recommend a book for a reasonably experienced OO programmer? The list I see is: Programming in Scala - Odersky, Spoon, Venners Beginning Scala - Pollak Programming Scala - Subramaniam Pr...

How do you uninstall the Scala Eclipse plugin?

I tried to install the Eclipse plugin advertised in the Scala advertised at http://www.scala-lang.org/node/94. I installed it onto my version of the SpringSource Tool Suite 2.2.1 thinking everything would be fine. I'm still using Mac OS 10.4, and thus have not upgraded to Java 6. Lo, and behold, I was wrong. I can't even restart STS an...

Static testing for Scala

There are some nice libraries for testing in Scala (Specs, ScalaTest, ScalaCheck). However, with Scala's powerful type system, important parts of an API being developed in Scala are expressed statically, usually in the form of some undesirable or disallowed behavior being prevented by the compiler. So, what is the best way to test whet...

Scala Concurrency slowing down

Heya, I'm doing to preface this with the fact I'm a relative Java/Scala newbie so I wouldn't rule out that there is something obvious I'm not doing. I've got a Scala application which connects via Hibernate to a MySQL database. The Application is designed to process a large amount of data, about 2,750,000 records so I've tried to optim...

Scala 2.8 and Lift status

Whats the status of Lift working with Scala 2.8? I'm finding fragments of conversations about it on the web. I've been trying tweaking the pom.xml but I'm getting errors from the Lift side of things. ...

Deep copy of 2D array in Scala?

How do I do a deep copy of a 2D array in Scala? For example val a = Array[Array[Int]](2,3) a(1,0) = 12 I want val b to copy values of a but without pointing to the same array. ...

Scala: Whats the best way to do numeric operations in generic classes?

In Scala, I'd like to be able to write generic classes which use operators like >, /, * etc, but I don't see how to constrain T such that this will work. I looked into constraining T with Ordered[T], but that doesn't seem to work since only RichXXX (e.g. RichInt) extend it, not Int etc. I also saw Numeric[T], is this only available in ...

How to do fast prefix string matching in Scala

I'm using some Java code to do fast prefix lookups, using java.util.TreeSet, could I be using scala's TreeSet instead? Or a different solution? /** A class that uses a TreeSet to do fast prefix matching */ class PrefixMatcher { private val _set = new java.util.TreeSet[String] def add(s: String) = _set.add(s) def findMatches(pre...

Possible to make a producer/consumer obj in Scala using actors 'threadless' (no receive...)?

So I want to write some network code that appears to be blocking, without actually blocking a thread. I'm going to send some data out on the wire, and have a 'queue' of responses that will come back over the network. I wrote up a very simple proof of concept, inspired by the producer/consumer example on the actor tutorial found here: ...