scala-java-interop

Visibility of properties in scala class

℡╲_俬逩灬. 提交于 2019-12-06 08:51:07
I defined a property in the constructor of my class the following way: class Step(val message:String = "") When I try access to message value from Java code y get a visbility error. Why? If you add the @scala.reflect.BeanProperty annontation you get "automatic" get and set methods See http://www.scala-lang.org/docu/files/api/scala/reflect/BeanProperty.html scala> class Step(@scala.reflect.BeanProperty val message:String ) defined class Step scala> val s = new Step("asdf") s: Step = Step@71e13a2c scala> s.message res6: String = asdf scala> s.getMessage res10: String = asdf The code is correct,

ambiguous reference to overloaded definition, from a Java library

℡╲_俬逩灬. 提交于 2019-12-05 19:48:55
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 read in object JsonPath of type [T](x$1: Any, x$2: String, x$3: <repeated...>[com.jayway.jsonpath.Filter[_

Type-safe Builder Library for Scala and Java

蹲街弑〆低调 提交于 2019-12-05 12:38:19
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 Builder[HasProperty <: TBoolean] private(i: Int) { protected def this() = this(-1) def withProperty(i: Int

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

一笑奈何 提交于 2019-12-05 03:38:10
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? Use a throws annotation: @throws(classOf[SomeException]) def f()= { //do something that throws SomeException } You can also annotate a class constructor : class MyClass @throws

Scala cast to generic type (for generic numerical function)

旧巷老猫 提交于 2019-12-05 02:54:37
问题 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

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

时光怂恿深爱的人放手 提交于 2019-12-05 00:51:47
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: def validate(toValidate: AnyRef) = { val validator = createValidator validator.validate(toValidate) }

Implement Java Interface with Raw type from Scala

天涯浪子 提交于 2019-12-04 22:32:23
问题 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

java.util.Iterator to Scala list?

走远了吗. 提交于 2019-12-04 15:59:30
问题 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]

Get Scala Type for a java.lang.Class[T] in Scala 2.10

不羁的心 提交于 2019-12-04 13:56:15
问题 I was looking at the scala reflection overview and I'm wondering if it is possible to use a java.lang.Class<T> as a Type in Scala 2.10. import scala.reflect.runtime.{ universe => ru } class Reflector { def getType: ru.Type = { ru.typeOf[java.lang.String] } def getType[T](clazz: Class[T]): ru.Type = { //is it possible to implement me? } } Is it possible to implement the parse[T](clazz: Class[T]): ru.Type method without changing its signature in order to be able to call it from java with new

Why does Scala complain about illegal inheritance when there are raw types in the class hierarchy?

我们两清 提交于 2019-12-04 03:54:35
问题 I'm writing a wrapper that takes a Scala ObservableBuffer and fires events compatible with the Eclipse/JFace Databinding framework. In the Databinding framework, there is an abstract ObservableList that decorates a normal Java list. I wanted to reuse this base class, but even this simple code fails: val list = new java.util.ArrayList[Int] val obsList = new ObservableList(list, null) {} with errors: illegal inheritance; anonymous class $anon inherits different type instances of trait