scala-java-interop

Please explain use of Option's orNull method

拜拜、爱过 提交于 2019-12-02 21:49:41
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? 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 : Option[Int]).orNull scala> Some("hi").orNull res21: java.lang.String = hi scala> Some(null : String).orNull res22

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

痞子三分冷 提交于 2019-12-02 13:54:35
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 the javap output: public static final scala.None$ x(); Signature: ()Lscala/None$; Code: 0: getstatic #11;

How do you call a Scala singleton method from Java?

旧城冷巷雨未停 提交于 2019-12-02 06:09:06
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.ScalaPower.getInstance().showMyPower(10) but none work. (Decompile the class file using Jad show me nothing

How to implement Java interface in Scala with multiple variable parameter methods (type eraser issue)?

ⅰ亾dé卋堺 提交于 2019-12-01 16:32:05
问题 I have a Scala class that is trying to implement a Java interface (EntityManager in JavaEE 7 for unit testing purposes to be specific). The interface has these two methods (among others): public StoredProcedureQuery createStoredProcedureQuery(String procedureName, Class... resultClasses); public StoredProcedureQuery createStoredProcedureQuery(String procedureName, String... resultSetMappings); In the Scala implementation I have: override def createStoredProcedureQuery(procedureName: String,

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

半腔热情 提交于 2019-12-01 16:03:57
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: Probability = new Probability(0.0) override val lowerBound: Probability = new Probability(1.0) //

Scala getters and setters in Java class

若如初见. 提交于 2019-12-01 15:27:35
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? Thanks! You can only sort of do this, and it's hard enough that you probably don't want to. What you can't

access java base class's static member in scala

我是研究僧i 提交于 2019-12-01 04:47:38
Ihave some codes written in Java. And for new classes I plan to write in Scala. I have a problem regarding accessing the protected static member of the base class. Here is the sample code: Java code: class Base{ protected static int count = 20; } scala code: class Derived extends Base{ println(count); } Any suggestion on this? How could I solve this without modifying the existing base class This isn't possible in Scala. Since Scala has no notation of static you can't access protected static members of a parent class. This is a known limitation . The work-around is to do something like this: //

Time complexity of JavaConverters asScala method

你离开我真会死。 提交于 2019-12-01 02:37:41
Starting with Scala version 2.9 there exists a handy converter to convert from java.util.List and other collections to Scala's data structures by writing something like this: import scala.collection.JavaConverters._ def scalaVersion = callJavaMethod.asScala This is a lovely little feature, as it allows one to exploit the advantages of Scala when interacting with existing Java code. However, I am uncertain about the involved time and space complexity and could not find anything in the official documentation, hence, the following question: Where can I get information on the complexity (time and

Time complexity of JavaConverters asScala method

狂风中的少年 提交于 2019-11-30 22:21:00
问题 Starting with Scala version 2.9 there exists a handy converter to convert from java.util.List and other collections to Scala's data structures by writing something like this: import scala.collection.JavaConverters._ def scalaVersion = callJavaMethod.asScala This is a lovely little feature, as it allows one to exploit the advantages of Scala when interacting with existing Java code. However, I am uncertain about the involved time and space complexity and could not find anything in the official

Convert a Java Future to a Scala Future

怎甘沉沦 提交于 2019-11-30 09:01:49
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? How about just wrapping it (I'm assuming there's an implicit ExecutionContext here): val scalaFuture = Future { javaFuture.get } EDIT: A simple polling strategy could look like this (java.util.Future => F): def pollForResult[T](f: F[T]):