scala

logging in scala

In Java, the standard idiom for logging is to create a static variable for a logger object and use that in the various methods. In Scala, it looks like the idiom is to create a Logging trait with a logger member and mixin the trait in concrete classes. This means that each time an object is created it calls the logging framework to get ...

An efficient technique to replace an occurence in a sequence with mutable or immutable state

I am searching for an efficient a technique to find a sequence of Op occurences in a Seq[Op]. Once an occurence is found, I want to replace the occurence with a defined replacement and run the same search again until the list stops changing. Scenario: I have three types of Op case classes. Pop() extends Op, Push() extends Op and Nop()...

How to expose a constructor variable(sic!) as read-only?

Hi StackOverflow! I have this rather simple question about Scala. Given that i have to following class definition: class Foo(var bar: Int) The code which is able to construct an instance of Foo must be able to pass the initial value for bar. But if I define bar as var the User is also able to change its value at runtime which is not ...

match tuple with null

I don't understand why the following case doesn't match. Null should be an instance of Any, but it doesn't match. Can someone explain what is going on? val x = (2, null) x match { case (i:Int, v:Any) => println("got tuple %s: %s".format(i, v)) case _ => println("catch all") } prints catch all Thanks. ...

Scala: How can I implement a clone method on a superclass, and use it in a subclass?

I might be approaching this the wrong way, but I'd like to have an object like this: class MyDataStructure { def myClone = { val clone = new MyDataStructure // do stuff to make clone the same as this ... clone } } class MyDataStructureExtended(val foo: String) extends MyDataStructure Then: val data = MyDataStruct...

How do I handle unicode user input in Scala safely (esp XML entities)

On my website I have a form that takes in some textual user input. All works fine for "normal" characters. However when unicode characters are input... well, the plot thickens. User inputs something like やっぱ死にかけてる This comes in to the server as text containing XML entity refs やっぱ死にかけ&...

Is this correct way to handle RESTful like URL in Lift Framework?

If I have a URL like http://localhost/Test/edit/{id} and I would like the {id} transform to a parameter instead of URL path part. Is it best way to do it by create a menu with RewriteRequest? Because I found it a little boilerplate if I have lot URL pattern like this. val menu = Menu(new Loc[Unit] { override def name = "Test" ...

Scala: Suggestion for an idea for a hands on session

I want to give a Scala presentation and I want to do it by taking an application and evolve it from something which uses java idioms to something that uses the power of scala (traits, pattern matching, implicit convertions, functional programming). I'm especially interested in something that demonstrates a design change, rather than sy...

scala: mixins depending on type of arguments

I have a set of classes of models, and a set of algorithms that can be run on the models. Not all classes of models can perform all algorithms. I want model classes to be able to declare what algorithms they can perform. The algorithms a model can perform may depend on its arguments. Example: Say I have two algorithms, MCMC, and Importa...

scala: traits and abstract methods override

I have a base abstract class (trait). It has an abstract method meth(). It is extended and implemented by several derived classes. I want to create a trait that can be mixed into the derived classes so that it implements meth() and then calls the derived class's meth() Something like: trait Foo { def foo() } trait M extends Foo { ...

Scala: Pattern matching when one of two items meets some condition

I'm often writing code that compares two objects and produces a value based on whether they are the same, or different, based on how they are different. So I might write: val result = (v1,v2) match { case (Some(value1), Some(value2)) => "a" case (Some(value), None)) => "b" case (None, Some(value)) => "b" case _ = > "c" } Thos...

Scala semantics of equals/hashCode for case classes with traits

I am a newcomer to Scala. In 2.7.7, the following code abstract class C case class CC() extends C trait T val c1 = CC() val c2 = new CC() with T println(c1.hashCode == c2.hashCode,c1 equals c2) prints (false,true) whereas I would have expected (false,false) What am I missing? Thanks in advance. ...

Generating javadoc for project involving scala modules

If I have a project which has some of its modules in scala (ie java modules, scala modules side by side) - how have people solved combining scaladoc generated HTML with javadoc generated to provide a single view of the documentation for the project? (this could be using maven, or ant, more a general question). Any thoughts and experi...

Common Programming Mistakes for Lift Developers to Avoid

In the spirit of Common programming mistakes for Scala developers to avoid and family. What are the best practices and common mistakes to avoid when developing for the lift / liftweb framework? More specifically, what are the issues that affect Lift development, as distinct from generic Scala development? ...

Pattern matching with multiline XML case

I must be doing some stupid mistake. I have a server that returns the XML <a><b>123</b></a> and now I would like to match against that XML. So I write something like xml match { case <a><b>{_}</b></a> => true } This works as long as I do not have to deal with multi-line XML literals. So the important thing is that the server sends m...

Scala and Lift and SessionVar that loses it contents

I have SessionVar like this in Lift: object MyObject { object myVar extends SessionVar[Box[MyObject]](Empty) } Then I set value once for myVar: MyObject.myVar(Full(value)) My problem is that after some time (1-5 mins) myVar will lose its value, that is, it will have value Empty again. This is not due inactivity since I am click...

Deploying simple scala app(non-webapp) managed with maven

I am new to the java/scala stack in general. So far I have a relatively simple scala application (not a webapp) setup and built build with maven2, and I'd like to be able to deploy the output to one or more production server (ubuntu on EC2, but that shouldn't matter I assume)? My main questions are: 1) What's the best way to get all the...

Scala: binary incompatibility between releases

Why is Scala binary incompatible between different releases? ...

Recursive XML in scala

Hello Everyone, I am trying to parse this document in scala: <?xml version="1.0"?> <model> <joint name="pelvis"> <joint name="lleg"> <joint name="lfoot"/> </joint> <joint name="rleg"> <joint name="rfoot"/> </joint> </joint> </model> I want...

Derived Scala case class with same member variables as base

Is there a nicer way of doing this? scala> case class A(x : Int) defined class A scala> case class B(override val x : Int, y : Int) extends A(x) defined class B I'm extending A with B and adding an extra member variable. It would be nice not to have to write override val before the x. ...