I am trying to define a generic container whose elements can return the enclosing container. Something like:
abstract class Container[E <: Element] { // compile error
def contains( e: E ): Boolean
def addNewElement(): Unit
}
abstract class Element[C <: Container] { // compile error
def enclosingContainer(): C
}
class MyContainer...
Assuming you have a string containing the name of a method, an object that supports that method and some arguments, is there some language feature that allows you to call that dinamically? Kind of like Ruby's send.
...
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...
I'm a bit shaky on the rules as to when you need a _ after a method to use it as a function. For example, why is there a difference between Foo's and Nil's :: in the following?
def square(n: Int) = n * n
object Foo { def ::(f: Int => Int) = f(42) }
// ...
scala> Foo.::(square)
res2: Int = 1764
scala> Nil.::(square)
<console>:6: er...
Hi! I have quite some trouble with actors which contain long running operations, in my case persistent socket connections. Here is some test code which runs fine if I create less than four Server instances, but if I create more instances, I always end up with only three or sometimes four concurrent socket connections, because the other o...
Good day.
Is there a simple way to return regexp matches as an array?
Here is how I am trying in 2.7.7:
val s = """6 1 2"""
val re = """(\d+)\s(\d+)\s(\d+)""".r
for(m <- r.findAllIn(s)) println(m) // prints "6 1 2"
r.findAllIn(s).toList.length // 3? No! It returns 1!
But I then tried:
s match {
case r(m1, m2, m3) => println(m1)
}
...
I am trying to use the answer of a preceding question to implement a small graph library. The idea is to consider graphs as colections, where vertices wrap collection elements.
I would like to use abstract types to represent Vertex and Edge types (because of type safety) and I want to use type parameters to represent the type of the col...
Suppose I have the following code:
trait Trait1 {
trait Inner {
val name = "Inner1"
}
}
trait Trait2 {
trait Inner {
val name = "Inner2"
}
}
class Foo extends Trait1 with Trait2 {
// I want Concrete1 to be a Trait1.Inner not a Trait2.Inner
class Concrete1 extends Inner
val c = new Concrete1
}
object Obj {
def...
I'm trying to package a scala project into a jar and write properties to the Manifest using Buildrs package() method.
The package seems to have no affect on the Manifest. Here's the build file:
VERSION_NUMBER = "1.0.0"
GROUP = "Green"
COPYRIGHT = "Green CopyRight"
require 'buildr/scala'
Buildr::Scala::Scalac::REQUIRES.library = '2.8....
For example:
scala> val l:List[String] = List("one", "two")
l: List[String] = List(one, two)
scala> l.contains(1) //wish this didn't compile
res11: Boolean = false
The various explanations of why things were done this way in Java don't seem to apply as much here, as Map and Set do implement the type-safe version of contains and frie...
While debugging or exploring spec features it would be more advantageous to type them in REPL (Scala interpreter) rather then in file with spec and run it with something like maven. What is the optimal way to create in REPL the same "environment" as in Specification object?
Update:
It looks like the simplest way to experiment with specs...
Hi everyone,
I am a beginner to functional programming and I have recently started studying Scala and I really love this language for the all goodies it provides like closures, pattern matching, currying etc.
However I am not able to understand the point of Option[T] class in Scala. I mean, I am not able to see any advanages of None ov...
Hi,
I'm new with scala and lift but I already got the pocketchangeapp up
and running and playing around with the RestAPI thing in it.
Now I created a new project and it worked quite well until last night.
I made my latest changes and commited them in svn but hat a problem
with my workspace so I deleted my projects and did a checkout of ...
Despite the upcoming java 7 standard fork/join framework, I am building some helper method that is light weight in syntax for client to run code in parallel.
Here is a runnable main method to illustrate the idea.
import actors.Futures
object ForkTest2 {
def main(args: Array[String]) {
test1
test2
}
def test1 {
v...
There's not much info in the spec on what type ascription is, and there certainly isn't anything in there about the purpose for it. Other than "making passing varargs work", what would I use type ascription for? Below is some scala REPL for the syntax and effects of using it.
scala> val s = "Dave"
s: java.lang.String = Dave
scala> va...
Let's say I have a method expecting another method as a parameter. Is it possible to send an object's instance methods for that parameter? How would I handle methods that have no parameters?
I'll write some pseudocode:
void myMethod1(callback<void,int> otherFunc); // imagine a function returning void, and taking a int parameter
void m...
I know you can create an anonymous function, and have the compiler infer its return type:
val x = () => { System.currentTimeMillis }
Just for static typing's sake, is it possible to specify its return type as well? I think it would make things a lot clearer.
...
I have a helper method:
def controlStructure[T <: SomeObject](exceptions: Class[_]*)(body: => T) = {
try {
val tempObject = body
tempObject.callSomeMethod
Some(tempObject)
} catch {
case e if (exceptions.contains(e.getClass)) => None
}
}
called with:
controlStructure[MySomeObject...
Hi I am new to scala and trying to write addition program in with generic type parameter as shown below
object GenericTest extends Application {
def func1[A](x:A,y:A) :A = x+y
println(func1(3,4))
}
But this does not work .What mistake i am making .
...
In Scala there is a Stream class that is very much like an iterator. The topic Difference between Iterator and Stream in Scala? offers some insights into the similarities and differences between the two.
Seeing how to use a stream is pretty simple but I don't have very many common use-cases where I would use a stream instead of other a...