In Scala we can have parameterless methods, which can be very useful for DSL purposes; they are also used for auto-defining getters for class fields.
I am developing a Java library that I would like to integrate as seamlessly with Scala as possible. I was wondering if there is a way to annotate a Java method with an empty parameter list so that Scala recognize it as parameterless.
For instance
class Foo {
@ParameterLess
public String[] bar() { return someArray; }
}
so that in Scala I can do:
val f = new Foo
f bar (0)
without having the compiler to complain about bar() having an empty argument list?
Alternatively, is there a way to define Scala-style getters and setter from Java?
Note: of course I know that in this particular example I can workaround the problem by defining
public String bar(int i) { return someArray[i]; }
This probably answers my question:
Scala getters and setters in Java class
The answer is that you can't, unless you resort to some trickery involving traits. Well, I guess that some kind of annotation à la @scala.reflect.BeanProperty
would be a useful improvement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With