scala-java-interop

Using Scala traits with implemented methods in Java

痞子三分冷 提交于 2019-11-26 17:14:44
I guess it is not possible to invoke methods implemented in Scala traits from Java, or is there a way? Suppose I have in Scala: trait Trait { def bar = {} } and in Java if I use it as class Foo implements Trait { } Java complains that Trait is not abstract and does not override abstract method bar() in Trait Tomasz Nurkiewicz Answer From Java perspective Trait.scala is compiled into Trait interface . Hence implementing Trait in Java is interpreted as implementing an interface - which makes your error messages obvious. Short answer: you can't take advantage of trait implementations in Java,

Using Scala from Java: passing functions as parameters

夙愿已清 提交于 2019-11-26 13:28:21
问题 Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int => String) { func(10) } } Now in Java, I would have liked to use MyScala as: package scala_java; public class MyJava { public static void main(String [] args) { MyScala.setFunc(myFunc); // This line gives an error } public static String myFunc(int someInt) { return String.valueOf(someInt); } } However, the above does not work (as expected since Java does not allow functional programming). What is the

How can I use a Scala singleton object in Java?

旧街凉风 提交于 2019-11-26 11:28:34
问题 I have a Scala object that I need to use in a Java class. Here\'s the Scala object object Person { val MALE = \"m\" val FEMALE = \"f\" } How can I use this Scala object in Java? I have tried the following so far without success (compile errors): Person.MALE() //returns a String which is useless as I want the actual Person object 回答1: Use Person$.MODULE$ . See also How can I pass a Scala object reference around in Java? Singletons as Synthetic classes in Scala? Edit : A working example (I

Using a Java library with Scala reserved words

人走茶凉 提交于 2019-11-26 09:58:34
问题 I\'m using an external library written in Java (Selenium). One of the function calls has the signature type(String, String) , and I keep getting compiler errors when trying to call it from Scala, that is: selenium.type(\"ab\",\"abc\") Is there a workaround for this issue? 回答1: selenium.`type`("ab","abc") 来源: https://stackoverflow.com/questions/1793984/using-a-java-library-with-scala-reserved-words

What's the difference between == and .equals in Scala?

ぐ巨炮叔叔 提交于 2019-11-26 07:57:30
问题 What is the difference between == and .equals() in Scala, and when to use which? Is the implementation same as in Java? EDIT: The related question talks about specific cases of AnyVal . The more general case is Any . 回答1: You normally use == , it routes to equals , except that it treats null s properly. Reference equality (rarely used) is eq . 回答2: == is a final method, and calls .equals , which is not final. This is radically different than Java, where == is an operator rather than a method

Iterating over Java collections in Scala

此生再无相见时 提交于 2019-11-26 07:56:43
问题 I\'m writing some Scala code which uses the Apache POI API. I would like to iterate over the rows contained in the java.util.Iterator that I get from the Sheet class. I would like to use the iterator in a for each style loop, so I have been trying to convert it to a native Scala collection but will no luck. I have looked at the Scala wrapper classes/traits, but I can not see how to use them correctly. How do I iterate over a Java collection in Scala without using the verbose while(hasNext())

What is the difference between JavaConverters and JavaConversions in Scala?

末鹿安然 提交于 2019-11-26 02:26:47
问题 In scala.collection, there are two very similar objects JavaConversions and JavaConverters. What is the difference between these two objects? Why do they both exist? When do I want to use one vs. the other? 回答1: EDIT: Java Conversions got @deprecated in Scala 2.13.0. Use scala.jdk.CollectionConverters instead. JavaConversions provide a series of implicit methods that convert between a Java collection and the closest corresponding Scala collection, and vice versa. This is done by creating