scala-java-interop

How to use Scala varargs from Java code

蓝咒 提交于 2019-12-28 16:37:29
问题 There are plenty of articles on calling Java varargs from Scala code, but the only thing I could find the opposite way round was this question: Using scala vararg methods in java, which doesn't have any concrete examples. I'm trying to use scala.Console from some Java code, for the reason that java.io.Console doesn't work in Eclipse, whereas the Scala one does. But I cannot get the method def readLine (text: String, args: Any*): String to work because it seems to be expecting a scala

How to convert a java.util.List to a Scala list

狂风中的少年 提交于 2019-12-28 03:23:51
问题 I have this Scala method with below error. Cannot convert into a Scala list. def findAllQuestion():List[Question]={ questionDao.getAllQuestions() } type mismatch; found : java.util.List[com.aitrich.learnware.model.domain.entity.Question] required: scala.collection.immutable.List[com.aitrich.learnware.model.domain.entity.Question] 回答1: import scala.collection.JavaConversions._ will do implicit conversion for you; e.g.: var list = new java.util.ArrayList[Int](1,2,3) list.foreach{println} 回答2:

Cannot access a Java static method from Scala

允我心安 提交于 2019-12-25 02:28:37
问题 I'm having an issue with Scala and Java interoperability which Google and SO seem to be unhelpful (I've seen similar questions, but none offered a working solution for my case). I have created a jar file in Java (hosted here, if you need it to answer this question) which contains a class with a static method. However, I can't seem to access this static method from Scala. Here's the code: val graph1 = ... val graph2 = ... val union = DirectedGraph.merge(graph1, graph2) The method exists, and I

Scala/Java interoperability: How to deal with options containing Int/Long (primitive types)?

倖福魔咒の 提交于 2019-12-23 11:55:21
问题 Given a service in Scala: class ScalaService { def process1(s: Option[String], i: Option[Int]) { println("1: " + s + ", " + i) } } which is to be used from Java: public class Java { public static void main(String[] args) { ScalaService service = new ScalaService(); // This works, but it's confusing { scala.Option<String> so = scala.Option.apply("Hello"); scala.Option<Object> io = scala.Option.apply((Object) 10); service.process1(so, io); } // Would be OK, but not really nice { scala.Option

What's the difference between a class with a companion object and a class and object with the same name?

谁说我不能喝 提交于 2019-12-21 07:22:41
问题 A Scala class's "companion object" can be viewed as a singleton object with the same fully qualified name as the class (i.e. same name, in same package). They are used to hold utility functions common to all instances of the class, as a replacement for Java's static methods. However, in various places in the docs and in questions, it say that companion objects must be defined in the same compilation unit. For example, they must be defined in the same file; companion objects cannot be defined

Convert Scala Option to Java Optional

我们两清 提交于 2019-12-21 04:00:13
问题 I need to convert Scala Option to Java Optional. I managed to wrote this: public <T> Optional<T> convertOption2Optional(Option<T> option) { return option.isDefined() ? Optional.of(option.get()) : Optional.empty(); } But I don't like it. Is there a simple way to do it, or a built-in scala converter? I'm looking for something like: Options.asJava(option); 回答1: The shortest way I can think of in Java is: Optional.ofNullable(option.getOrElse(null)) @RégisJean-Gilles actually suggested even

How do I convert a java.util.Map to scala.collection.immutable.Map in Java?

和自甴很熟 提交于 2019-12-20 10:47:35
问题 I find lots of people trying to do this, and asking about this but the question is always answered in terms of scala code. I need to call an API that is expecting a scala.collection.immutable.Map but I have a java.util.Map, how can I cleanly convert from the latter to the former in my java code? The compiler disagrees with the sentiment that it is an implicit conversion as it barfs on that when I try it! Thank you! 回答1: Getting an immutable Scala map is a little tricky because the conversions

How do you call a Scala singleton method from Java?

﹥>﹥吖頭↗ 提交于 2019-12-20 04:24:28
问题 I'm trying to inject some Scala code into my existing Java app. (So, being said, I want some more fun). I create a singleton stuff in Scala ScalaPower.scala package org.fun class ScalaPower object ScalaPower{ def showMyPower(time:Int) = { (0 to time-1).mkString(", ") } } Now, inside OldJava.java class OldJava { public void demo(){ System.out.println(?) } } What should I fill in ? so that Java will call the showMyPower method? I tried both org.fun.ScalaPower.showMyPower(10) and org.fun

What do user-defined value classes look like from Java?

江枫思渺然 提交于 2019-12-19 16:52:09
问题 I think I understand the new "value class" feature of Scala 2.10, by comparison with Haskell's newtype : trait BoundedValue[+This] extends Any { this: This => def upperBound: This def lowerBound: This } class Probability @throws(classOf[IllegalArgumentException]) (v: Double) extends AnyVal with BoundedValue[Probability] { val value: Double = if ((v >= 0.0) && (v <= 1.0)) v else throw new IllegalArgumentException((v.toString) + "is not within the range [0.0, 1.0]") override val upperBound:

What do user-defined value classes look like from Java?

依然范特西╮ 提交于 2019-12-19 16:52:09
问题 I think I understand the new "value class" feature of Scala 2.10, by comparison with Haskell's newtype : trait BoundedValue[+This] extends Any { this: This => def upperBound: This def lowerBound: This } class Probability @throws(classOf[IllegalArgumentException]) (v: Double) extends AnyVal with BoundedValue[Probability] { val value: Double = if ((v >= 0.0) && (v <= 1.0)) v else throw new IllegalArgumentException((v.toString) + "is not within the range [0.0, 1.0]") override val upperBound: