scala-2.8

cannot find class manifest for element type T

99封情书 提交于 2019-11-30 04:51:39
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 does "providing a manifest mean" ? To provide type information you can use a context bound def f[T :

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

﹥>﹥吖頭↗ 提交于 2019-11-30 04:31: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 the preferred way (any other way I try I am getting a type-mismatch error): error: type mismatch; found :

Passing Java array to Scala

懵懂的女人 提交于 2019-11-30 04:20:14
问题 Although I've been using Scala for a while and have mixed it with Java before, I bumped on a problem. How can I pass a Java array to Scala? I know that the other way around is fairly straightforward. Java to Scala is not so however. Should I declare my method in Scala? Here is a small example of what I'm trying to achieve: Scala: def sumArray(ar: Array[Int]) = ... Java: RandomScalaClassName.sumArray(new int[]{1,2,3}); Is this possible? 回答1: absolutely! The Array[T] class in Scala is mapped

Equality relations in Scala

a 夏天 提交于 2019-11-30 03:51:53
问题 I just stumbled on one of Tony Morris' blog-posts about Java and a fundamental problem with the language: that of defining a bespoke equality-relation for a collection. This is something that I think is a big deal and wondered whether there was some scala solution. The classic issue manifests itself in thinking about, say, a trade. Let's say I make two trades of +100 vodafone shares @150p. The two trades are equal, yes? Except they are not the same trade . In the case of a normal real-world

How do I use the trait scala.Proxy

只谈情不闲聊 提交于 2019-11-30 02:49:54
问题 I just found it in the API and would like to see one or two examples along with an explanation what it is good for. 回答1: The Proxy trait provides a useful basis for creating delegates, but note that it only provides implementations of the methods in Any ( equals , hashCode , and toString ). You will have to implement any additional forwarding methods yourself. Proxy is often used with the pimp-my-library pattern: class RichFoo(val self: Foo) extends Proxy { def newMethod = "do something cool"

What's the right way to use scala.io.Source?

血红的双手。 提交于 2019-11-30 01:37:30
In many examples, it is described that you can use scala.io.Source to read a whole file like this: val str = scala.io.Source.fromFile("test.txt").mkString() But closing the underlying stream is not mentioned. Why does Scala not provide a convenient way to do that such as with clause in Python? It looks useful but not difficult. Is there any other better way to do that safely in Scala, I means to read a whole file? For the sake of completeness val testTxtSource = scala.io.Source.fromFile("test.txt") val str = testTxtSource.mkString() testTxtSource.close() Should get things done. Daniel C.

What is the concept of “weak conformance” in Scala?

折月煮酒 提交于 2019-11-30 00:01:05
I just recently encountered the term "Weak Conformance" (in Stack Overflow user retronym 's answer to How to set up implicit conversion to allow arithmetic between numeric types? ). What is it? 3.5.3 Weak Conformance In some situations Scala uses a more general conformance relation. A type S weakly conforms to a type T , written S <: w T , if S <: T or both S and T are primitive number types and S precedes T in the following ordering. Byte <: w Short Byte <: w Character Short <: w Int Int <: w Long Long <: w Float Float <: w Double A weak least upper bound is a least upper bound with respect

What's the new way to iterate over a Java Map in Scala 2.8.0?

早过忘川 提交于 2019-11-29 21:24:20
How does scala.collection.JavaConversions supercede the answers given in Stack Overflow question Iterating over Java collections in Scala (it doesn't work because the "jcl" package is gone) and in Iterating over Map with Scala (it doesn't work for me in a complicated test which I'll try to boil down and post here later). The latter is actually a Scala Map question, but I think I need to know both answers in order to iterate over a java.util.Map . In 2.8, you import scala.collection.JavaConversions._ and use as a Scala map. Here's an example (in 2.8.0.RC1): scala> val jmap:java.util.Map[String

Function syntax puzzler in scalaz

喜你入骨 提交于 2019-11-29 18:43:46
Following watching Nick Partidge's presentation on deriving scalaz , I got to looking at this example, which is just awesome: import scalaz._ import Scalaz._ def even(x: Int) : Validation[NonEmptyList[String], Int] = if (x % 2 ==0) x.success else "not even: %d".format(x).wrapNel.fail println( even(3) <|*|> even(5) ) //prints: Failure(NonEmptyList(not even: 3, not even: 5)) I was trying to understand what the <|*|> method was doing, here is the source code: def <|*|>[B](b: M[B])(implicit t: Functor[M], a: Apply[M]): M[(A, B)] = <**>(b, (_: A, _: B)) OK, that is fairly confusing (!) - but it

Collection type generated by for with yield

非 Y 不嫁゛ 提交于 2019-11-29 11:05:01
问题 When I evaluate a for in Scala, I get an immutable IndexedSeq (a collection with array-like performance characteristics, such as efficient random access): scala> val s = for (i <- 0 to 9) yield math.random + i s: scala.collection.immutable.IndexedSeq[Double] = Vector(0.6127056766832756, 1.7137598183155291, ... Does a for with a yield always return an IndexedSeq , or can it also return some other type of collection class (a LinearSeq , for example)? If it can also return something else, then