scala-2.8

How does the NotNull trait work in 2.8 and does anyone actually use it?

谁说胖子不能爱 提交于 2019-12-29 06:01:02
问题 trait NotNull {} I've been trying to see how this trait can guarantee that something is not null and I can't figure it out: def main(args: Array[String]) { val i = List(1, 2) foo(i) //(*) } def foo(a: Any) = println(a.hashCode) def foo(@NotNull a: Any) = println(a.hashCode) //compile error: trait NotNull is abstract def foo(a: Any with NotNull) = println(a.hashCode) //compile error: type mismatch at (*) And: val i = new Object with NotNull //compile-error illegal inheritance There is

How does the NotNull trait work in 2.8 and does anyone actually use it?

谁都会走 提交于 2019-12-29 06:00:26
问题 trait NotNull {} I've been trying to see how this trait can guarantee that something is not null and I can't figure it out: def main(args: Array[String]) { val i = List(1, 2) foo(i) //(*) } def foo(a: Any) = println(a.hashCode) def foo(@NotNull a: Any) = println(a.hashCode) //compile error: trait NotNull is abstract def foo(a: Any with NotNull) = println(a.hashCode) //compile error: type mismatch at (*) And: val i = new Object with NotNull //compile-error illegal inheritance There is

How to make a class a member of two linked lists in Scala?

匆匆过客 提交于 2019-12-24 23:26:33
问题 I have a feeling I'm missing something very obvious here. I'm converting a compiler generator written in Java into Scala as a learning exercise. It's not really written in Java, it seems to be transliterated C. In one part of the program, there are Nodes. Each node is part of two linked lists, and there are fields that are references to the next item in each of the linked lists (call these "across" and "down"). Various methods traverse these lists, either one of them or both of them and do

how to start remote actors in scala

戏子无情 提交于 2019-12-24 07:15:11
问题 I want to start remote actors from my local computer using scala . Can I just start running the actors on the remote computer without manually starting a server program of some kind in the remote computer. I have a master actor which has to start some remote actors. So any ideas on how I should do it? or can I do it without executing some kind of program on the remote computer to which i have to connect first in order to start new remote actors. 回答1: You would need to have a program running

Efficient map with case class as a key in Scala?

穿精又带淫゛_ 提交于 2019-12-23 15:16:23
问题 A following C code uses enum and array as efficient "map" from enum to anything: enum Color { ColorRed, ColorGreen, ColorBlue, ColorSize}; void f() { int x[ColorSize]; x[ColorRed] = 12; x[ColorGreen] = 33; x[ColorBlue] = 4; return x[ColorGreen]; } Is this possible with Scala? I.e. to have a "map" from case class to something, implemented as efficient array and not as tree or as hashmap. Yet I would like to be able to index only with a paricular type not with Int. Update: In short I would like

Use cases of Scala collection forwarders and proxies

好久不见. 提交于 2019-12-23 09:26:48
问题 Scala's collection library contains the forwarders IterableForwarder, TraversableForwarder, SeqForwarder and proxies like IterableProxy, MapProxy, SeqProxy, SetProxy, TraversableProxy, etc. Forwarders and proxies both delegate collection methods to an underlying collection object. The main difference between these two are that forwarders don't forward calls that would create new collection objects of the same kind. In which cases would I prefer one of these types over the other? Why and when

How do I specify a static array in a Scala 2.8 annotation?

微笑、不失礼 提交于 2019-12-21 07:42:10
问题 I've been building out some annotated domain classes in Scala 2.8.0 using Hibernate Annotations 3.4.0. It's been working fine, except that there are certain annotations which take an array as a parameter. For example, here's a Java annotation that I want to express in Scala: @OneToMany(mappedBy="passport_id", cascade=CascadeType.PERSIST) However, the annotation requires an array/set as input: [ERROR] .../Passport.scala:50: error: type mismatch; [INFO] found : javax.persistence.CascadeType

Scala immutable objects and traits with val fields

醉酒当歌 提交于 2019-12-20 19:42:29
问题 I would like to construct my domain model using immutable objects only. But I also want to use traits with val fields and move some functionality to traits. Please look at the following example: trait Versionable { val version = 0 def incrementVersion = copy(version=version+1) } Unfortunatelly such code doesn't work - copy method is unknown for trait Versionable. I think that it would be nice to have copy method generated for every trait and class. Such method should create shallow copy of

How to find a matching element in a list and map it in as an Scala API method?

狂风中的少年 提交于 2019-12-20 11:35:12
问题 Is there a method to do the following without doing both methods: find and map ? val l = 0 to 3 l.find(_ * 33 % 2 == 0).map(_ * 33) // returns Some(66) 回答1: How about using collect? // Returns List(66) List(1, 2, 3) collect { case i if (i * 33 % 2 == 0) => i * 33 } However that will return all matches and not just the first one. The better answer would have been, based on Scala 2.9: // Returns Some(66) List(1, 2, 3) collectFirst { case i if (i * 33 % 2 == 0) => i * 33 } The solution suggested

short way to breakOut to specific collection type?

醉酒当歌 提交于 2019-12-19 06:03:53
问题 scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> m.map{case (a, b) => (a+ 1, a+2, a+3)} res42: scala.collection.immutable.Iterable[(Int, Int, Int)] = List((2,3,4)) What I want is for the result type to be List[(Int, Int, Int)]. The only way I found is: scala> m.map{case (a, b) => (a+ 1, a+2, a+3)}(breakOut[Map[_,_], (Int, Int, Int), List[(Int, Int, Int)]]) res43: List[(Int, Int, Int)] = List((2,3,4)) Is there a shorter way? 回答1: You can make it a bit