scala-java-interop

What is the Java equivalent of a Scala object?

耗尽温柔 提交于 2019-11-30 08:08:59
问题 In Scala, we can write object Foo { def bar = {} } How is this implemented by the compiler? I am able to call Foo.bar(); from Java but new Foo(); from Java gives the error cannot find symbol symbol: constructor Foo() Does the JVM support singletons natively? Is it possible to have a class in Java that does not have a constructor? Note: here is the code output by scalac -print package <empty> { final class Foo extends java.lang.Object with ScalaObject { def bar(): Unit = (); def this(): object

Scala convert List[Int] to a java.util.List[java.lang.Integer]

[亡魂溺海] 提交于 2019-11-30 05:40:22
Is there a way in Scala to convert a List[Int] to java.util.List[java.lang.Integer] ? I'm interfacing with Java (Thrift). JavaConversions supports List --> java.util.List , and implicits exist between Int --> java.lang.Integer , but from what I can tell I would still need an extra pass to manually do the conversion: val y = List(1) val z: java.util.List[Integer] = asList(y) map { (x: Int) => x : java.lang.Integer } Apparently you need both conversions. However, you can group them in a single implicit conversion: implicit def toIntegerList( lst: List[Int] ) = seqAsJavaList( lst.map( i => i:java

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 :

How can I convert Scala Map to Java Map with scala.Float to java.Float k/v conversion

做~自己de王妃 提交于 2019-11-30 04:15:02
I would like to be able to perform the following, but it fails in the call to useMap. How can I perform this conversion? scala> import scala.collection.JavaConversions._ import scala.collection.JavaConversions._ scala> import scala.collection.JavaConverters._ import scala.collection.JavaConverters._ scala> def useMap(m: java.util.Map[java.lang.Integer, java.lang.Float]) = m useMap: (m: java.util.Map[Integer,Float])java.util.Map[Integer,Float] scala> val v: Map[Int, Float] = Map() v: Map[Int,Float] = Map() scala> useMap(v) <console>:10: error: type mismatch; found : scala.collection.immutable

Using Java libraries in Scala

僤鯓⒐⒋嵵緔 提交于 2019-11-29 16:26:57
问题 I am a new to Scala. I am only able to write basic code thus far, but I want to start using it more concretely, rather than just learning theory. Lets say I have the following Java code in HelloWorld.java : public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } What would the equivalent Scala code be? 回答1: In your example, you just have a main, not a function you would necessarily call from somewhere else. But let's said you did have a

Convert a Java Future to a Scala Future

一曲冷凌霜 提交于 2019-11-29 13:31:35
问题 I have a Java Future object which I would like to convert into a Scala Future . Looking at the j.u.c.Future API, there is nothing much that I could use other than the isDone method. Is this isDone method blocking? Currently this is what I have in my mind: val p = Promise() if (javaFuture.isDone()) p.success(javaFuture.get) Is there a better way to do this? 回答1: How about just wrapping it (I'm assuming there's an implicit ExecutionContext here): val scalaFuture = Future { javaFuture.get } EDIT

Using Scala type aliases from Java code

你。 提交于 2019-11-29 13:28:12
Suppose I have type alias defined in scala as object Foo { type Bar = Option[String] } It looks like I cannot refer to alias in Java code like that (it simply complains cannot find symbol ): import Foo.*; public class Cafebabe { void bar(Bar x) { //... } } I've tried static import as well. (More specifically, I have java reflection code which I cannot change that needs to know parameter type and I need to feed Bar alias to it). I know, I can create wrapper in Scala class BarWrapper(value: Bar) but maybe I'm missing some other way? Type aliases are only visible to the Scala compiler, and like

Automatically convert Scala code to Java code [closed]

我与影子孤独终老i 提交于 2019-11-29 11:21:42
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I have an app written in Scala and some of my team members want a Java version of it. It is a demo app to use another API written in Scala, and they want a Java version of the app to be able to use the API from Java. However, the app is somewhat large and I don't want to manually rewerite in Java (and they don't

Using scala vararg methods in java

╄→гoц情女王★ 提交于 2019-11-29 09:41:42
Why do all scala vararg methods, when used from java, seem to accept a Seq of variables, and can't be used as java native vararg methods. Is this a bug? For instance, Buffer has method def append(elems: A*): Unit . But in java it has another signature: void append(Seq<A>) . It is not a bug. It is a design choice that favors vararg use within Scala over interoperability with Java. For example, it allows you to pass a List into a Scala varargs method without having to convert it to an Array on the way. If you need to use Scala varargs from Java, you should create some scala Seq instead. You can,

Scala: Overriding Generic Java Methods II

旧巷老猫 提交于 2019-11-29 09:17:05
In Scala, I need to override the following, given, Java classes and methods: public abstract class AbstractJava<T> { protected abstract T test(Class<? extends T> clazz); } public class ConcreteJava extends AbstractJava<Object> { @Override protected Object test(Class<?> clazz) { return null; } } // Scala class ConcreteScala extends ConcreteJava { protected override def test(clazz: Class[_ <: AnyRef]): AnyRef = super.test(clazz) } I'm getting the compilation error: error: ambiguous reference to overloaded definition, both method test in class ConcreteJava of type (clazz: java.lang.Class[_])java