Scala Language

The main forum for discussions and news about the Scala language.

A question about view bounds

Hi scala-user (this is my first post, be gentle :-),

Is there any way to do something like the following? If it makes any
difference, I'm not particularly tied to the syntax in Foo, but changing the
inheritance hierarchy of A/B/C would be painful :-)

class A
class B extends A
class C(val attribute: Int) extends A

class Foo[B <: A]
{
def method[X <: A <% { val attribute: Int }](x: X) =
"Has 'attribute': " + x.attribute.toString

def method(a: A) =
"No 'attribute'"
}

val foo = new Foo
println(foo.method(new B)) // No 'attribute'
println(foo.method(new C(0))) // Has 'attribute': 0

Thanks in advance!

\t

Reminder: ScalaDays 2012: 'Call for Speakers' Closes Wednesday

A gentle reminder... deadline for submitting to the Scala Days is 22nd February (ie Wednesday, next week)
Full details on how to submit a talk can be found at:
  http://days2012.scala-lang.org/node/101

Re: Scala Enumeration

The posts about enumeration are getting annoying, sorry. I've created a gist which tries to fix/improve the serialization of the enums. Whenever someone redesigns the class hopefully to provide user customizable Value classes he could still use the method. When serializing a "static object" (an object which is not nested in a class) enum only its class (boxed in private ObjectRepl class) is serialized, when serializing a value of such enum only its id (wrapped in private ValRepl class) is serialized. Other enums and their values are serialized fully.
https://gist.github.com/1835099

Scala Days London – Travel advice needed!

Hi everyone,

after having a look at the available flights and airports I wonder if there are some practical suggestions from people living in London or Great Britain or have experience with traveling there.

For instance it looks like most cheap flights use London/Stansted which seems to be quite a bit away from London. Assuming that they to rip people of with the transfer from Stansted to London, would it make sense to use Heathrow instead?

Does it make sense to book train tickets from the airport to London City in advance?

Are there any practical suggestions regarding accommodation?

Thanks a lot!

Bye,

Simon

Chained assignment in Scala

Just in case someone always been missing chained assignment in Scala, here is an equivalent of ´a =
b = value´:

scala> val a@b = new Object
a: java.lang.Object = java.lang.Object@80b973
b: java.lang.Object = java.lang.Object@80b973

Notice that the variables above get initialized with the same value, as opposed to:

scala> val a,b = new Object
a: java.lang.Object = java.lang.Object@d1223d
b: java.lang.Object = java.lang.Object@1ee8c1

Also possible with ´var´s and with more than two names (but not with ´def´s):

scala> var a@(b@(c@d)) = new Object
a: java.lang.Object = java.lang.Object@cbbdf3
b: java.lang.Object = java.lang.Object@cbbdf3
c: java.lang.Object = java.lang.Object@cbbdf3
d: java.lang.Object = java.lang.Object@cbbdf3

Enjoy :)

--
EL

Scala Enumeration

Can someone explain why IDLE is always printed as 'x'?

object State extends Enumeration {
val IDLE, STARTED = Value
var x = IDLE
}

scala> State.x
res16: State.Value = x // okay...

scala> State.x = State.STARTED
State.x: State.Value = STARTED

scala> State.x
res17: State.Value = STARTED // expected.

scala> State.x = State.IDLE
State.x: State.Value = x // huh?

scala> State.x
res18: State.Value = x // what?

Thanks.

InfixExpr

Hi All

The infix expression is defined in SLS as follows[1]:

InfixExpr ::= PrefixExpr
| InfixExpr id [nl] InfixExpr

I wonder, why not to allow an optional newline also before ´id´ (id denotes an infix operator here):

InfixExpr ::= PrefixExpr
| InfixExpr [nl] id [nl] InfixExpr

This would allow for constructions that are currently not accepted by compiler or interpreted not as
expected (that in turn is often a cause of irritations):

1) val x = "Hello"
+ "world"

2) 5
-
3

3) Or, in parser combinators we must actually place the ´|´ combinator at the end of the line or
enclose the whole expression in parentheses to "disallow newlines [to be treated as semicolons]".

scare-quoted "type-safe" contains method for covariant collections

class MyCovariantCollection[+T, +CC[X] <: Seq[X]](xs: CC[T]) {  // Oops, our "evidence" seems to be useless, as what we want  // to know is that T1 <:< T, but of course we can't do that because   // of the variance position.  def contains[T1](x: T1)(implicit ev: T <:< T1): Boolean = xs contains x}
// But wait! Unspecified implementation vagaries to the rescue! object Test extends App {  val xs = new MyCovariantCollection(List(1, 2, 3))  assert(xs contains "abc", "I'm sure it's in there somewhere")}
Syndicate content

Copyright © 2013 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland