case-class

How is this case class match pattern working?

I've just seen this case class in the Scala actors package: case class ! [a](ch: Channel[a], msg: a) And in the JavaDoc it describes usage in the following form: receive { case Chan1 ! msg1 => ... case Chan2 ! msg2 => ... } Why is this not: receive { case !(Chan1, msg1) => ... case !(Chan2, msg2) => ... } Is the bang ope...

Case class to map in Scala

Does anyone know if there is a nice way I can convert a Scala case class instance, e.g. case class MyClass(param1: String, param2: String) val x = MyClass("hello", "world") Into a mapping of some kind, e.g. getCCParams(x) returns "param1" -> "hi", "param2" -> "3" Which works for any case class, not just predefined ones. I've found ...

Symbols or Case Classes for sending messages to Scala Actors?

In the Scala actor examples I have seen where a parameterless message is sent to an actor (such as this), case classes (or case objects) have been created and then used as messages. Symbols work just as well and look a bit neater and, after reading a book on Erlang, seem more natural. I assume that symbol equality would work for remote a...

Case classes vs Enumerations in Scala

I was wondering if there are any best practice guidelines on when to use case classes vs extending Enumeration in Scala. They seem to offer some of the same benefits. ...

Matching sub-classes of case classes in Scala

Why does this fail to compile (or work?): case class A(x: Int) class B extends A(5) (new B) match { case A(_) => println("found A") case _ => println("something else happened?") } The compiler error is: constructor cannot be instantiated to expected type; found : blevins.example.App.A required: blevins.example.Ap...

Reflection on a Scala case class

I'm trying to write a trait (in Scala 2.8) that can be mixed in to a case class, allowing its fields to be inspected at runtime, for a particular debugging purpose. I want to get them back in the order that they were declared in the source file, and I'd like to omit any other fields inside the case class. For example: trait CaseClassRef...

Why were the case classes without a parameter list deprecated?

Why were the case classes without a parameter list deprecated from Scala? And why does compiler suggest to use () as parameter list instead? EDIT : Someone please answer my second question... :| ...

Overload constructor for Scala's Case Classes?

In Scala 2.8 is there a way to overload constructors of a case class? If yes, please put a snippet to explain, if not, please explain why? ...

How do I "get" a Scala case object from Java?

I created a hierarchy of case objects in Scala that looks like the following: package my.awesome.package sealed abstract class PresetShapeType(val displayName: String) case object AccelerationSensor extends PresetShapeType("Acceleration Sensor") case object DisplacementSensor extends PresetShapeType("Displacement Sensor") case object ...

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

Naming case classes in Scala.

I tend to have this redundant naming in case classes: abstract class MyTree case class MyTreeNode (...) case class MyTreeLeaf (...) Isn't it possible to define Node and Leaf inside of MyTree? What are best practices here? ...

A case class, inheriting from a class, is having issues being used as constructor parameter

I have this case class define: class Protocol(protocol:String) object Protocol { def apply(protocol:String) :Protocol = { protocol.toUpperCase match { case "HTTP" => Http() case "HTTPS" => Https() case "Ftp" => Ftp() case "Mail" =>Mail() case other => new Protocol(other) } } } cas...

Matching case classes in scala: ~(a,b) match{case a~b=>...}

I have a case class case class ~[a,b](_1:a, _2:b) When I want to do pattetn matching new ~("a", 25) match{ case "a" ~ 25 => } I can use it this way because "a" ~ 25 and ~("a", 25) are equivalent. But if I want to match new ~("a", new ~("b", 25)) by {case "a" ~ "b" ~ 25 => } troubles begin. I understand that this statements aren't...

Appending a label to immutable case classes in Scala

Im trying to create a parser for a small language with commands including labels and goto: ... lazy val cmds = opt("{")~>rep(cmd<~opt(";"))<~opt("}") ^^ {...} lazy val cmd = ("if"~boolexpr~"then"~cmds~"else"~cmds ^^ { case _~b~_~c1~_~c2 => IFCMD(boolexpr,c1 | ident ~":="~numericLit ^^ {case i1~_~v => ASSIGN(i1,v) } | "goto" ~>ide...

Implicit conversion to instantiate a sealed class

I have this inheritance sealed abstract class MyValue case class MyString(s:String) extends MyValue case class MyBoolean(b:Boolean) extends MyValue case class MyR(m1:MyValue, m2:MyValue) extends MyValue case class MyU(m1:MyValue, m2:MyValue) extends MyValue /* ... */ and implicit def string2myString(s:String) = MyString(s) implicit ...

Is it correct to define all classes as cases in Scala just to have all their arguments made properties automatically?

I'm beginning Scala. Am I correct understanding that I should define a class as a case class if I'd like it's arguments to be exposed as properties? Does not it introduce any side effects? ...

case class and traits

I want create a special calculator. I think that case class is a good idea for operations: sealed class Expr case class add(op1:Int, op2:Int) extends Expr case class sub(op1:Int, op2:Int) extends Expr case class mul(op1:Int, op2:Int) extends Expr case class div(op1:Int, op2:Int) extends Expr case class sqrt(op:Int) extends Expr case cla...

How to test arguments to a case class constructor?

I'd like to test the arguments to my case class constructor and throw an exception if they fail certain tests. The compiler complained when I tried to write my own apply method (Multiple 'apply' methods. I suppose I could make it a non-case class, and do the apply/unapply constructor field stuff myself, but I had hoped not to. Thanks ...

Bidirectional reference with case classes

Is it possible to implement a bi-directional tree in a case class. This seems like it should be easy, but I'm getting stumped case class Node(name:String, parent:Option[Node], children:List[Node]) I want to add a child (and get a new root) -- something like def addChild(n:String):Node = { Node(name, parent, Node(n, Some(this), Nil...

Is constructor use allowed with case classes?

I have a case class (let's name it Stuff) that I want to be able to create anonymous subclasses of at run time by extending a trait (call it Marker). Here's a snippet of a REPL session that illustrates what I'm trying to do: scala> trait Marker defined trait Marker scala> case class Stuff(i: Int) defined class Stuff scala> val a = Stu...