scala-java-interop

Builder Library for Scala and Java

痞子三分冷 提交于 2019-12-10 14:24:08
问题 I need a builder library that can be called from Scala and Java. Easy enough in Scala using default, named parameters. But how do I call this code from Java? See below. Or perhaps I should go with a fluent API that is more common to both languages? Scala: case class Person(gender:Gender.Value, firstName:String, lastName:String){ def fullName = lastName+", "+firstName override def toString = firstName+","+lastName+","+gender } case class PersonBob( gender:Gender = GenderBob().build, firstName

“Trickiness” to calling Scala code from Java?

限于喜欢 提交于 2019-12-10 14:13:53
问题 There is a Scala library (that only exists written in Scala) that I really want to use in my Java app. I am trying to evaluate whether I can do this or not without suffering any hidden gotchyas/caveats/pitfalls. So I went straight to the Scala FAQ, where they answer this very question (well, sort of): Accessing Java classes from Scala code is no problem at all. Using a Scala class from Java can get tricky, in particular if your Scala class uses advanced features like generics, polymorphic

Using Scala reflection with Java reflection

大兔子大兔子 提交于 2019-12-10 12:48:49
问题 I have a lot of code that has been using Java reflection to invoke arbitrary user-specified methods on Java objects, as part of a DSL. However, a lot of those Java objects, in practice, are Scala objects, and some of the methods on them are marked with the Scala @deprecated annotation. Unfortunately, Java's annotations mechanism cannot see Scala annotations, and until Scala 2.10 there was no convenient way (that I know of?) to get access to those annotations. I have finally upgraded to 2.10,

Type-safe Builder Library for Scala and Java

隐身守侯 提交于 2019-12-10 06:38:34
问题 Below is a type-safe, fluid, builder pattern in Scala as described at http://www.tikalk.com/java/blog/type-safe-builder-scala-using-type-constraints. It's similar to Builder Library for Scala and Java, but deals specifically with compile-time builder checks. How can this called from Java? Can it be done with a clean API for Scala AND Java given the "scala.Predef$$eq$colon$eq" parameters? sealed trait TBoolean sealed trait TTrue extends TBoolean sealed trait TFalse extends TBoolean class

Can I use java 8 in an mixed scala 2.10 / java project built by sbt?

ε祈祈猫儿з 提交于 2019-12-10 03:39:42
问题 To what degree can I use java 8 in a mixed java / scala 2.10 sbt project? Can I emit java 8 bytecode? Use java 8 language features? Or are there features in scala 2.11 that are necessary? Is there an interop story? 回答1: From the 2.10.4 Scala release notes: New ByteCode emitter based on ASM Can target JDK 1.5, 1.6 and 1.7 Emits 1.6 bytecode by default Old 1.5 backend is deprecated Also The official Scala 2.12 distribution will be built for Java 8 (and thus require it). More background info of

Catch in Java a exception thrown in Scala - unreachable catch block

不羁岁月 提交于 2019-12-10 03:34:20
问题 Scala doesn't have checked exceptions. However, when calling scala code from java, it's desirable to catch exceptions thrown by scala. Scala: def f()= { //do something that throws SomeException } Java: try { f() } catch (SomeException e) {} javac doesn't like this, and complains that "this exception is never thrown from the try statement body" Is there a way to make scala declare that it throws a checked exception? 回答1: Use a throws annotation: @throws(classOf[SomeException]) def f()= { //do

Difference in type erasure of List[Int] and List[Integer]

南笙酒味 提交于 2019-12-08 16:23:41
问题 Why does List[scala.Int] type erase to List[Object] whilst Integer in List[java.lang.Integer] seems to be preserved? For example, javap for object Foo { def fooInt: List[scala.Int] = ??? def fooInteger: List[java.lang.Integer] = ??? } outputs public scala.collection.immutable.List<java.lang.Object> fooInt(); public scala.collection.immutable.List<java.lang.Integer> fooInteger(); where we see Integer was preserved in second case. The docs state Replace all type parameters in generic types with

ambiguous reference to overloaded definition, from a Java library

╄→гoц情女王★ 提交于 2019-12-07 16:38:04
问题 I was tying to convert this example for JsonPath to Scala. It should be easy with java like: List<String> authors = JsonPath.read(json, "$.store.book[*].author"); Which I converted to this Scala: val authors = JsonPath.read(json, "$.store.book[*].author"); Where json is a String. But I get this compile error. ambiguous reference to overloaded definition, both method read in object JsonPath of type [T](x$1: String, x$2: String, x$3: <repeated...>[com.jayway.jsonpath.Filter[_]])T and method

is it possible to have a circular dependency between .java and .scala classes?

放肆的年华 提交于 2019-12-07 05:04:12
问题 Lets say I have class A defined in .java file, and class B defined in .scala file. class A use class B and class B use class A. If I use java compiler I will have a compilation error because class B is not compiled yet. If I use scala compiler class A will not be found. Is there a compiler that can compile both together? 回答1: I thought that Scala 2.7.2 introduced a joint compilation mode to do exactly this? Which version of scalac are you using, and is it running with this mode disabled? Edit

Scala - ambiguous reference to overloaded definition — with varargs [duplicate]

会有一股神秘感。 提交于 2019-12-06 19:09:43
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How do I disambiguate in Scala between methods with vararg and without I am currently porting part of an application to scala and it uses the Oval library. The method is question is the Validator.validate method. It has two signatures: List<ConstraintViolation> validate(Object validatedObject) List<ConstraintViolation> validate(Object validatedObject, String... profiles) The scala code looks generally like this: