scala-java-interop

Using Scala type aliases from Java code

对着背影说爱祢 提交于 2019-11-28 07:21:30
问题 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

Using scala vararg methods in java

梦想的初衷 提交于 2019-11-28 03:06:25
问题 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>) . 回答1: 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

Scala: Overriding Generic Java Methods II

*爱你&永不变心* 提交于 2019-11-28 02:41:52
问题 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

In Scala, how can I define a companion object for a class defined in Java?

廉价感情. 提交于 2019-11-28 00:58:29
I'd like to add implicit conversions to Java classes generated by a modeling tool. So I want to add them to the companion object of those classes, so that the compiler automatically finds them. But I cannot add them in a separate file, because the companion has to be defined in the same file. Is there anything I can do about this? Of course, I can define all my implicit conversions in another object and then bring it into scope, but this requires an extra import. Any other solution? With the Scala compiler as it stands now there is no way to define companion objects other than by putting them

Convert Java List to Scala Seq

会有一股神秘感。 提交于 2019-11-27 22:03:05
问题 I need to implement a method that return a Scala Seq , in Java. But I encounter this error: java.util.ArrayList cannot be cast to scala.collection.Seq Here is my code so far: @Override public Seq<String> columnNames() { List<String> a = new ArrayList<String>(); a.add("john"); a.add("mary"); Seq<String> b = (scala.collection.Seq<String>) a; return b; } But scala.collection.JavaConverters doesn't seem to offer the possibility to convert as a Seq . 回答1: JavaConverters is what I needed to solve

Can I use scala List directly in Java?

无人久伴 提交于 2019-11-27 20:41:59
Can I use scala List in Java, like : import scala.collection.immutable.List; class HelloScalaList { public static void main (String[] args) { List xs = List(1, 2, 3); System.out.println(xs); } } It does not seem to compile. can't find List$.apply method. when I change it to List xs = Dir.ls() where Dir is my scala class, and ls() returns a scala List, the compiler complaints about "Internal compiler error: java.lang.ClassCastException: org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding cannot be cast to org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding at org.eclipse.jdt.internal

Accessing scala.None from Java

血红的双手。 提交于 2019-11-27 15:59:21
How can you access scala.None from Java? The last line causes the compiler to die with "type scala.None does not take parameters". import scala.Option; import scala.Some; import scala.None; final Option<String> object1 = new Some<String>("Hi there"); final Option<String> object2 = new None<String>(); This fails with "cannot find symbol constructor None()": final Option<String> object2 = new None(); This fails with "cannot find symbol variable None": final Option<String> object2 = None; In 2007 this used to work, but then Scala changed. The Java compiler gives error: incompatible types : final

How to convert a Java Stream to a Scala Stream?

試著忘記壹切 提交于 2019-11-27 11:15:33
问题 As a part of an effort of converting Java code to Scala code, I need to convert the Java stream Files.walk(Paths.get(ROOT)) to Scala. I can't find a solution by googling. asScala won't do it. Any hints? The followings is the related code: import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util

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

情到浓时终转凉″ 提交于 2019-11-27 10:46:13
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] Neil 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} You can simply convert the List using Scala's JavaConverters : import scala.collection.JavaConverters._ def

How can I convert a Java Iterable to a Scala Iterable?

99封情书 提交于 2019-11-27 10:14:34
问题 Is there an easy way to convert a java.lang.Iterable[_] to a Scala.Iterable[_] ? 回答1: In Scala 2.8 this became much much easier, and there are two ways to achieve it. One that's sort of explicit (although it uses implicits): import scala.collection.JavaConverters._ val myJavaIterable = someExpr() val myScalaIterable = myJavaIterable.asScala EDIT: Since I wrote this, the Scala community has arrived at a broad consensus that JavaConverters is good, and JavaConversions is bad, because of the