scala

Scala wont pattern match with java.lang.String and Case Class

Hello fellow Scala Programmers I have been working with Scala for some month now, however I have a problem with some properly basic stuff, I am hoping you will help my out with it. case class PersonClass(name: String, age: Int) object CaseTester { def main(args:Array[String]) { val string = "hej" string match { case e:String ...

In Scala, how does one write a class with a constructor, not all of whose arguments are class members ?

I want to write class whose constructor takes two parameters, but the arguments are not actually members of the class. e.g. class P(V1:Int, V2:Int) { val set = Set(V1, V2) } Having constructed the 'set', I don't actually care about V1 and V2. Is there a way of expressing this in Scala ? ...

How can I easily get a Scala case class's name?

Given: case class FirstCC { def name: String = ... // something that will give "FirstCC" } case class SecondCC extends FirstCC val one = FirstCC() val two = SecondCC() How can I get "FirstCC" from one.name and "SecondCC" from two.name? ...

Scala: Can't catch exception thrown inside a closure

Disclaimer: absolute novice in Scala :( I have the following defined: def tryAndReport(body: Unit) : Unit = { try { body } catch { case e: MySpecificException => doSomethingUseful } } I call it like this: tryAndReport{ someCodeThatThrowsMySpecificException() } While the call to someCodeThatThrowsMySpecificException...

How can I take any function as input for my Scala wrapper method?

Let's say I want to make a little wrapper along the lines of: def wrapper(f: (Any) => Any): Any = { println("Executing now") val res = f println("Execution finished") res } wrapper { println("2") } Does this make sense? My wrapper method is obviously wrong, but I think the spirit of what I want to do is possible. Am I right...

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

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

Is classOf[] in Scala 2.8 different from 2.7?

I have an interface from Java public class IJava { ... public java.lang.Class getType(); ... } It is inherited in Scala class CScala { def getType() = classOf[Foo] } It worked in Scala 2.7.7. But in 2.8.0.RC1, I get type mismatch; found : java.lang.Class[Foo](classOf[Foo]) required: java.lang.Class How do I get...

Error after installing scala plugin of netbeans

I installed the scala plugin on my netbeans and followed the instruction of this page: http://wiki.netbeans.org/Scala68v1#Scala_Plugins_for_NetBeans_6.8_v1.x_.28RC2.29 but after it completed correctly step by step, when I make an empty project (Hello world!), the project has an error! The empty project is here: package scalaapplicati...

Very dumb question about IntelliJ IDEA

I'm a long-time Eclipse user and I just now decided to try IntelliJ IDEA 9 (free edition) for Scala. A couple of dumb questions: How can I tell if a file I've modified has been saved? How can I tell if I file I've saved has been checked into CVS? I feel incredibly "exposed" to some sort of imminent danger when I don't see the famili...

How Can I Check an Object to See its Type and Return A Casted Object

I have method to which I pass an object. In this method I check it's type and depending on the type I do something with it and return a Long. I have tried every which way I can think of to do this and I always get several compiler errors telling me it expects a certain object but gets another. Can someone please explain to me what I a...

Can you do Logic Programming in Scala?

I read somewhere that Pattern Matching like that supported by the match/case feature in Scala was actually borrowed from Logic languages like Prolog. Can you use Scala to elegantly solve problems like the Connected Graph problem? e.g. https://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_15.html ...

Scala traits and implicit conversion confusion

The following lines work when I enter them by hand on the Scala REPL (2.7.7): trait myTrait { override def toString = "something" } implicit def myTraitToString(input: myTrait): String = input.toString object myObject extends myTrait val s: String = myObject However, if I try to compile file with it I get the following error: [erro...

lift `??` construct

Hi, I have browsed lift's MegaProtoUser and encountered this construction: ??("Last Name"). Can anyone explain, what that means? Thanks for answering ...

Scala: is it possible to override default case class constructor?

Just wondering if this is possible. What I would actually like to do is check and possibly modify one of the arguments before it is stored as a val. Alternatively, I could use an overload and make the default constructor private. In which case I would also like to make private the default factory constructor in the companion object, how...

Is there an easy (idiomatic) way to convert a java.lang.reflect.Method to a Scala function?

Can I retrieve a Method via reflection, somehow combine it with a target object, and return it as something that looks like a function in Scala (i.e. you can call it using parenthesis)? The argument list is variable. It doesn't have to be a "first-class" function (I've updated the question), just a syntactic-looking function call, e.g. f...

how to define a structural type that refers to itself?

I want to create a method sum that I can call on different types, specifically sum(1,2). def sum[A](a1: A, a2: A) = a1 + a2 This fails because the compiler can't tell if A has a method '+' I tried to define a structural type: type Addable = {def +(a: Addable)} This fails because of an illegal cyclic reference How can I achieve thi...

Can I create a collection in Scala that uses different equals/hashCode/compare implementations?

I'm looking for as simple way to create an identity set. I just want to be able to keep track of whether or not I've "seen" a particular object while traversing a graph. I can't use a regular Set because Set uses "==" (the equals method in Scala) to compare elements. What I want is a Set that uses "eq." Is there any way to create a S...

GAE JCache NumberFormatException, will I need to write Java to avoid?

The code below produces a NumberFormatException in this line: val cache = cf.createCache(Collections.emptyMap()) Do you see any errors? Will I need to write a Java version to avoid this, or is there a Scala way? ... import java.util.Collections import net.sf.jsr107cache._ object QueryGenerator extends ServerResource { private val ...

What are all the instances of syntactic sugar in Scala?

I decided to create this question to have a single source for all things syntactic sugar in Scala. I feel these details are some of the things most frustrating to starting users and are hard to search for since most/all of them are purely symbols and are thus hard to search for without knowing the name of the concept. TODO: implicit...

Any way to access the type of a Scala Option declaration at runtime using reflection?

So, I have a Scala class that looks like this: class TestClass { var value: Option[Int] = None } and I'm tackling a problem where I have a String value and I want to coerce it into that Option[Int] at runtime using reflection. So, in another piece of code (that knows nothing about TestClass) I have some code like this: def setField...