scala-2.8

Does the @inline annotation in Scala really help performance?

丶灬走出姿态 提交于 2019-11-27 09:57:14
问题 Or does it just clutter up the code for something the JIT would take care of automatically anyway? 回答1: I have yet to find a case where it improves performance, and I've tried in quite a few different spots. The JVM seems to be quite good at inlining when it's possible, and even if you ask for @inline in Scala, it can't always do it (and sometimes I've noticed that it doesn't even when I think it ought to be able to). The place where you expect to see a bytecode difference is in something

Iterators for mutable collections in Scala?

半城伤御伤魂 提交于 2019-11-27 06:55:25
问题 I just found out that there are such iterators in Java. Does Scala have iterators with 'set' and 'remove' methods for iterating (and modifying) mutable collections like array? If there is no such iterator then is there a good reason for that? 回答1: Scala does not currently have such an iterator. I suspect that it does not because Such iterators are not general (i.e they are only usable with mutable collections) but consume namespace. Because they can rapidly become confusing to think about in

Overload constructor for Scala's Case Classes?

女生的网名这么多〃 提交于 2019-11-27 06:51:15
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? Overloading constructors isn't special for case classes: case class Foo(bar: Int, baz: Int) { def this(bar: Int) = this(bar, 0) } new Foo(1, 2) new Foo(1) However, you may like to also overload the apply method in the companion object, which is called when you omit new . object Foo { def apply(bar: Int) = new Foo(bar) } Foo(1, 2) Foo(1) In Scala 2.8, named and default parameters can often be used instead of overloading. case class Baz(bar: Int, baz: Int = 0

How to unimport String “+” operator in Scala?

折月煮酒 提交于 2019-11-27 06:41:20
问题 I'm writing a DSL where the "+" operator is strictly numeric, like some other popular languages. It's close, but the String "+" operator is messing up my implicit conversions. What's the syntax for unimporting an operator of the String class? Just to be clearer, instead of this: scala> var x = "2" + 3; x: java.lang.String = 23 I'd like to get x: Int = 5 I imagine I just need 2 things to make that happen: Remove (unimport within my scope) the definition of "+" from Strings Define an implicit

Why does Iterator have a contains method but Iterable does not, in Scala 2.8?

血红的双手。 提交于 2019-11-27 06:38:49
问题 I would like to call 'contains' on my Iterables :-) 回答1: The reason Iterable does not have a contains method is because the way it is defined can have direct consequences on variance. Basically, there are two type signatures that make sense for it: def contains(v: Any): Boolean def contains(v: A): Boolean The second definition has increased type safety. However, A , which is the type parameter of collection, appears in a contra-variant position, which forces the collection to be invariant. It

What are Scala continuations and why use them?

天涯浪子 提交于 2019-11-27 05:51:33
I just finished Programming in Scala , and I've been looking into the changes between Scala 2.7 and 2.8. The one that seems to be the most important is the continuations plugin, but I don't understand what it's useful for or how it works. I've seen that it's good for asynchronous I/O, but I haven't been able to find out why. Some of the more popular resources on the subject are these: Delimited continuations and Scala Goto in Scala A Taste of 2.8: Continuations Delimited Continuations Explained (in Scala) And this question on Stack Overflow: What are the biggest differences between Scala 2.8

Proxies / delegates in Scala

房东的猫 提交于 2019-11-27 05:22:48
问题 I've seen several Scala questions recently (e.g. here, here, and here) that called for the use of proxies, and it's come up more than once in my own work. The Scala library has a number of proxy traits (14, if I counted correctly). Proxy classes/traits usually contain lots of boilerplate: class FooProxy(val self: Foo) extends Foo { // added behavior def mymethod = ... // forwarding methods def method1 = self.method1 def method2(arg: String) = self.method2(arg) ... } trait Foo { def method1:

Scala pattern matching with lowercase variable name

此生再无相见时 提交于 2019-11-27 04:32:29
I found that when using pattern matching with alternatives (for strings), Scala accepts variables starting with upper case (in the example below, MyValue1 and MyValue2 ), but not those starting with lower case ( myValue1 , myValue2 ). Is this a bug or a feature of Scala? I get this in version 2.8. If this is a feature, can anyone explain the rationale behind it? This is the code I used: val myValue1 = "hello" val myValue2 = "world" val MyValue1 = "hello" val MyValue2 = "world" var x:String = "test" x match { case MyValue1 | MyValue2 => println ("first match") case myValue1 | myValue2 =>

How do I exclude/rename some classes from import in Scala?

ぐ巨炮叔叔 提交于 2019-11-27 03:42:53
问题 Language FAQ says import scala.collection.mutable.{_, Map => _, Set => _} should import all classes from package scala.collection.mutable , except Map and Set . But it gives me this error: error: '}' expected but ',' found. import scala.collection.mutable.{_, Map => _, Set => _} Is there still a way to do this? 回答1: The _ has to be put at the end - not at the beginning: Exclude Map and Set from the import import scala.collection.mutable.{Map => _, Set => _, _} Exclude Set and rename Map to

Scala 2.8 CanBuildFrom

时间秒杀一切 提交于 2019-11-27 03:27:57
Following on from another question I asked, Scala 2.8 breakout , I wanted to understand a bit more about the Scala method TraversableLike[A].map whose signature is as follows: def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That Notice a few things about this method: It takes a function turning each A in the traversable into a B . It returns That and takes an implicit argument of type CanBuildFrom[Repr, B, That] . I can call this as follows: > val s: Set[Int] = List("Paris", "London").map(_.length) s: Set[Int] Set(5,6) What I cannot quite grasp is how the fact that That