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...
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 ?
...
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...
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...
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;
...
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";
}
...
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...
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.
...
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?
...
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?
...
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...
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...
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...
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 ?
...
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?
...
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...
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...
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 :)
...
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...
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...