scala

Type class pattern in Scala doesn't consider inheritance?

I am designing an API using type classes in some cases however I have encountered a problem with implicit resolution. As shown below, if there is an implicit object for type A but an object of type B extends A is passed to the method, then an implicit object cannot be found. Is there a way to make this work or do callers have to put im...

why scala can't infer the type of method parameters

I am wondering why scala can't infer the type of method parameters.I can see that in haskel (which also has type inference) can do the same. Then why not for scala ? ...

Functional Programming: Does a list only contain unique items?

I'm having an unsorted list and want to know, whether all items in it are unique. My naive approach would be val l = List(1,2,3,4,3) def isUniqueList(l: List[Int]) = (new HashSet()++l).size == l.size Basically, I'm checking whether a Set containing all elements of the list has the same size (since an item appearing twice in the original...

Clojure stripMargin

Scala offers a method called stripMargin that removes the left-hand part of a multiline string up to a specified delimiter (default: "|"). Here is an example: """|Foo |Bar""".stripMargin returns the string Foo Bar Is there a similar function in Clojure? If not, how would you implement it (most functionally)? Thanks. UPDATE: Th...

Scala equivalent of `??` operator in C#

Possible Duplicate: How to write a proper null-safe coalescing operator in scala? What is the Scala equivalent of ?? operator in C#? example: string result = value1 ?? value2 ?? value3 ?? String.Empty; ...

Scala implicit conversion problem

I am struggling with a Scala implicit conversion problem. The following code snippet illustrates my problem : import org.junit.{ Test, Before, After }; class ImplicitsTest { implicit def toStringWrapper(str: String) = new StringWrapper(str); @Test def test(){ val res1: Predicate = "str" startsWith "other"; } ...

How can I keep -Xcheckinit from interfering with the deserialization of Scala objects?

When using the -Xcheckinit compiler option and implementing my own readObject method in a serializable class, I can't call any accessor functions on fields declared in the body of my class from the readObject method. Fields declared as constructor arguments are ok. When I do try to access a field declared in the class body, I get a scala...

How to check that an array contains a particular value in Scala 2.8?

I've got an array A of D unique (int, int) tuples. I need to know if the array contains (X, Y) value. Am I to implement a search algorithm myself or there is a standard function for this in Scala 2.8? I've looked at documentation but couldn't find anything of such there. ...

Is there a do-until (postcondition) loop in Scala 2.8?

An algorithm of mine could be better readable if I could use a postcondition (do-until) loop instead of precondition (while) loop. Is there such a feature in Scala 2.8? ...

Why to use empty parentheses in Scala if we can just use no parentheses to define a function which does not need any arguments?

As far as I understand, in Scala we can define a function with no parameters either by using empty parentheses after its name, or no parentheses at all, and these two definitions are not synonyms. What is the purpose of distinguishing these 2 syntaxes and when should I better use one instead of another? ...

Right Arrow meanings in Scala

In Chapter 9 of Programming In Scala, there is an example method like this: def twice(op: Double => Double, x: Double) = op(op(x)) The author said in the book: The type of op in this example is Double => Double, which means it is a function that takes one Double as an argument and returns another Double. I don't understan...

Scala traits / cake pattern vs case classes

In my web application authorized user has at least 4 "facets": http session related data, persistent data, facebook data, runtime business data. I've decided to go with case class composition instead of traits for at least two reasons: traits mixing can cause name clashes i want the free case class goodies like pattern matching and co...

PartialFunction type inference in Scala

Let's consider the following function: def printPfType[T](pf:PartialFunction[T, _])(implicit m:Manifest[T]) = { println(m.toString) } Then I define the following test class: case class Test(s:String, i:Int) I can't write this: printPfType { case Test(_,i) => i } because the compiler can't infer the first parametric type of t...

Array initializing in Scala

I'm new to Scala ,just started learning it today.I would like to know how to initialize an array in scala. Example Java code String[] arr={"Hello","World"}; What is the equivalent of the above code in Scala ? ...

Use of lazy val for caching string representation

I encountered the following code in JAXMag's Scala special issue: package com.weiglewilczek.gameoflife case class Cell(x: Int, y: Int) { override def toString = position private lazy val position = "(%s, %s)".format(x, y) } Does the use of lazy val in the above code provide considerably more performance than the following code? ...

Scala recursive generics: Parent[Child] and Child[Parent]

Update: Clarified and expanded, since the original question was simplified too far I need a pair of traits, each of which refers to the other such that parent and child classes must relate to each other. trait Parent [C <: Child] { def foo(c: C) } trait Child [P <: Parent] { def parent: P = ... def bar = parent.foo(this) } Suc...

Should my persistence class return Option or rely on exceptions?

My application's persistence layer is formed by a Storage trait and an implementing class. I'm vacillating on this issue: should the fetchFoo(key: Key) methods should return Option[Foo], or should they throw FooNotFound exceptions if the key cannot be found? To add flavour to the issue, the persistence layer - it is written in Scala - i...

Is there a Lift cheatsheet / quickref ?

I found a couple of blogs on how to get started with Lift, but I need a quick reference. Something like: here you define the application map, this is how to write snippets, etc. I want to start a Lift app that is not "hello world" and I need the tl;dr version :) ...

Scala, JPA & nullable fields

In trying to use Scala with JPA, I have the following snippet as part of the definition of an entity @Column(name = "ACC_INT", nullable = true) @BeanProperty var accInt: Double = _ All is fine until I retrieve some data and I get the following exception: org.springframework.orm.hibernate3.HibernateSystemException: Null value was as...

How can I use Scala's Manifest class to instantiate the erased class at runtime?

I'm doing some WebDriver+PageObject stuff. (If your not familiar with PageObjects, this is a pattern where you have a class representing each page on your site which exposes all the functions of the page using the domain language, hiding the HTML stuff from the test.) I want to be lazy and have one 'submit' method in my abstract Page c...