scala-2.8

How to correctly type-annotate this HList?

可紊 提交于 2019-11-29 07:59:06
sealed abstract trait HList case class :+:[H, T <: HList](head: H, tail: T) extends HList { def :+:[T](v: T) = new :+:(v, this) } case object HNil extends HList { def :+:[T](v: T) = new :+:(v, this) } object HListExpt { def main(args: Array[String]) { val me: String :+: Int :+: Symbol :+: HNil.type = "Rahul" :+: 20 :+: 'Male :+: HNil println(me.head, me.tail.head) } } On trying to compile the above code, I get the following compiler error: error: type mismatch; found : :+:[java.lang.String,:+:[Int,:+:[Symbol,object HNil]]] required: :+:[String,:+:[Int,:+:[Symbol,HNil.type]]] val me: String :+:

Expand a Set[Set[String]] into Cartesian Product in Scala

这一生的挚爱 提交于 2019-11-29 03:59:10
I have the following set of sets. I don't know ahead of time how long it will be. val sets = Set(Set("a","b","c"), Set("1","2"), Set("S","T")) I would like to expand it into a cartesian product: Set("a&1&S", "a&1&T", "a&2&S", ..., "c&2&T") How would you do that? I think I figured out how to do that. def combine(acc:Set[String], set:Set[String]) = for (a <- acc; s <- set) yield { a + "&" + s } val expanded = sets.reduceLeft(combine) expanded: scala.collection.immutable.Set[java.lang.String] = Set(b&2&T, a&1&S, a&1&T, b&1&S, b&1&T, c&1&T, a&2&T, c&1&S, c&2&T, a&2&S, c&2&S, b&2&S) Nice question.

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

自古美人都是妖i 提交于 2019-11-29 03:48:46
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 obviously some special compiler treatment going on because this compiles: trait MyTrait {} def main(args: Array

cannot find class manifest for element type T

感情迁移 提交于 2019-11-29 02:53:32
问题 Was trying to compile some code from this SO question and run into this error message cannot find class manifest for element type T . Here is another snippet that shows the behavior: scala> def f[T](a:T, b:T):Array[T] = { new Array[T](2) } <console>:4: error: cannot find class manifest for element type T def f[T](a:T, b:T):Array[T] = { new Array[T](2) } I can see that new collection.mutable.GenericArray[T](2) fixes the issue. Apparently providing a manifest is the other option... But what

pass variable number of arguments in scala (2.8) case class to parent constructor

血红的双手。 提交于 2019-11-29 02:07:21
I was experimenting with variable constructor arguments for case classes in Scala, but am unable to pass them to the constructor of a case classes' parent: abstract case class Node(val blocks: (Node => Option[Node])*) case class Root(val elementBlocks: (Node => Option[Node])*) extends Node(elementBlocks) the above doesn't compile... is it actually possible to do this? This works with 2.7: abstract case class A(val a: String*) case class B(val b: String*) extends A(b:_*) Should work with 2.8. You need to use the :_* syntax which means "treat this sequence as a sequence" ! Otherwise, your

What's the motive behind Chained Package clauses in Scala?

邮差的信 提交于 2019-11-29 01:57:13
Chained package clause were introduced in Scala 2.8, as described by Martin Odersky on the Scala site . I don't quite get the intuition behind this. Following was the example in the Scala book for the nested packages: package bobsrockets { package navigation { // In package bobsrockets.navigation class Navigator package tests { // In package bobsrockets.navigation.tests class NavigatorSuite } } } This use case of nested packages made sense because we could use multiple nested packages in the same file, however the new syntax achieves the same thing as before but without the brackets. Won't it

Convert Scala Set into Java (java.util.Set)?

ⅰ亾dé卋堺 提交于 2019-11-29 01:45:53
问题 I have a Set in Scala (I can choose any implementation as I am creating the Set. The Java library I am using is expecting a java.util.Set[String]. Is the following the correct way to do this in Scala (using scala.collection.jcl.HashSet#underlying): import com.javalibrary.Animals var classes = new scala.collection.jcl.HashSet[String] classes += "Amphibian" classes += "Reptile" Animals.find(classes.underlying) It seems to be working, but since I am very new to Scala I want to know if this is

When is a return type required for methods in Scala?

旧巷老猫 提交于 2019-11-28 23:35:57
The Scala compiler can often infer return types for methods, but there are some circumstances where it's required to specify the return type. Recursive methods, for example, require a return type to be specified. I notice that sometimes I get the error message "overloaded method (methodname) requires return type", but it's not a general rule that return types must always be specified for overloaded methods (I have examples where I don't get this error). When exactly is it required to specify a return type, for methods in general and specifically for overloaded methods? VonC The Chapter 2. Type

Transforming Scala varargs into Java Object… varargs

我的梦境 提交于 2019-11-28 23:30:56
I have a Java class that logs stuff which has a method like this: void info(Object message, Object... params); In Scala, I've created a wrapper around such call that looks like this: def info(msg: => String, params: Any*) { log.info(msg, params); } When I call: val host = "127.0.0.1" val port = "1234" info("Start on {0}:{1}", host, port) I get: "Started on WrappedArray(127.0.0.1, 1234):{1}" Now, does anyone now how to convert params into an Object[] that can be consumed properly? I tried to do: def info(msg: => String, params: Any*) log.info(msg, params.toList.toArray); } But that doesn't work

Is the PartialFunction design inefficient?

我只是一个虾纸丫 提交于 2019-11-28 23:29:53
This is something I've wondered about for a while. I see this pattern a lot: if (pf.isDefinedAt(in)) pf(in) By breaking this up into two separate calls, all of the patterns that were evaluated in #isDefinedAt are then also evaluated in #apply. For example: object Ex1 { def unapply(in: Int) : Option[String] = { println("Ex1") if (in == 1) Some("1") else None } } object Ex2 { def unapply(in: Int) : Option[String] = { println("Ex2") if (in == 2) Some("2") else None } } val pf : PartialFunction[Int,String] = { case Ex1(result) => result case Ex2(result) => result } val in = 2 if (pf.isDefinedAt(in