scala-java-interop

Scala getters and setters in Java class

与世无争的帅哥 提交于 2019-12-04 02:56:16
问题 I would like to create a Java class that follows the Scala setters/getters convention. I tried following simple class, but it does not work: public class JavaA { private int a = 0; public int a() { return a; } public void a_$eq(int a) { this.a = a; } } But when I try to access it from scala: val x = new JavaA x.a = 1 and I get "reassignment to val" error message. I tried to look for this, but all issues I found where the other way around from scala to java. What is the right way to do it?

How do you mock scala call-by name in Mockito

杀马特。学长 韩版系。学妹 提交于 2019-12-04 02:01:23
问题 Im trying to mock scala call-by name method in mockito. But running into this error. This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example: //correct: someMethod(anyObject(), eq("String by matcher")); Any suggestion would be appreciated. Thanks! Here is the sample code and test file: Here Im trying to mock createCommand function. and give a mock so I

How to use scala.None from Java code [duplicate]

廉价感情. 提交于 2019-12-03 18:00:19
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Accessing scala.None from Java In Java you can create an instance of Some using the constructor, i.e. new Some(value) , but None has no partner class. How do you pass None to a Scala function from Java? 回答1: I think this ugly bit will work: scala.None$.MODULE$ There is no need for a new instance since one None is as good as another... 回答2: The scala.None$.MODULE$ thing doesn't always typecheck, for example this

Scala cast to generic type (for generic numerical function)

心不动则不痛 提交于 2019-12-03 16:31:47
I'm trying to implement a generic function that wraps a mathematical Java function. For simplicity, we can assume that the Java function (Java 7) takes one parameter and returns a result, both of type java.lang.Double. Of course, the wrapper function should take a parameter and a result, both of generic but numeric type A. The problem is that I'm not able to cast the result back to type A in the wrapper function. Where/what is the problem? Note: (I'm a newbie on Scala and used the following reference to solve the problem.) I'm using implicit type trait Numeric to enforce that template types

Implement Java Interface with Raw type from Scala

一个人想着一个人 提交于 2019-12-03 14:08:54
I'm trying to build an extension for Sonar, using Scala. I need to extend the following Java interface: public interface Decorator extends BatchExtension, CheckProject { void decorate(Resource resource, DecoratorContext context); } but Resource type is actually defined like: public abstract class Resource<PARENT extends Resource> I know I can workaround creating a Java raw super-class. I'd like to stick to Scala-only, also know if there's a solution I'm missing, and whether there's an improvement I could suggest to SonarSource people to make on their side (on using raw types). I've read there

Convert Scala Option to Java Optional

♀尐吖头ヾ 提交于 2019-12-03 12:14:58
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); Dici The shortest way I can think of in Java is: Optional.ofNullable(option.getOrElse(null)) @RégisJean-Gilles actually suggested even shorter if you are writing the conversion in Scala: Optional.ofNullable(option.orNull) By the way you must

java.util.Iterator to Scala list?

試著忘記壹切 提交于 2019-12-03 10:03:44
I have the following code: private lazy val keys: List[String] = obj.getKeys().asScala.toList obj.getKeys returns a java.util.Iterator<java.lang.String> Calling asScala , via JavaConverers (which is imported) according to the docs.. java.util.Iterator <==> scala.collection.Iterator scala.collection.Iterator defines def toList: List[A] So based on this I believed this should work, however here is the compilation error: [scalac] <file>.scala:11: error: type mismatch; [scalac] found : List[?0] where type ?0 [scalac] required: List[String] [scalac] private lazy val keys : List[String] = obj

Please explain use of Option's orNull method

天涯浪子 提交于 2019-12-03 06:59:50
问题 Scala's Option class has an orNull method, whose signature is shown below. orNull [A1 >: A](implicit ev : <:<[Null, A1]) : A1 I'm bewildered by the implicit thing. Would somebody please explain how it can be used, ideally with an example? 回答1: scala> Some(1).orNull <console>:10: error: could not find implicit value for parameter ev: <:<[Null,Int] Some(1).orNull ^ scala> (None : Option[Int]).orNull <console>:10: error: could not find implicit value for parameter ev: <:<[Null,Int] (None :

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

旧街凉风 提交于 2019-12-03 00:30:38
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! Getting an immutable Scala map is a little tricky because the conversions provided by the collections library return all return mutable ones, and you can't just use toMap because it

How to learn about using scala.None from Java using javap?

两盒软妹~` 提交于 2019-12-03 00:16:42
问题 In a previous question, Accessing scala.None from Java , it seems that people had used javap to figure out how to access scala.None from Java. I would like to know how they did that. FYI, the answer is: scala.Option$.MODULE$.apply(null); which can be shorted to: scala.Option.apply(null); Given this program ( OptionTest.scala ): object OptionTest extends App { val x = scala.None val y = scala.Some("asdf") } I ran javap on it like this: javap -s -c -l -private OptionTest This is a portion of