Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you declare parameter-less methods in Java for use in Scala?

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]; }
like image 367
Edoardo Vacchi Avatar asked Nov 14 '22 00:11

Edoardo Vacchi


1 Answers

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.

like image 93
Edoardo Vacchi Avatar answered Jan 13 '23 12:01

Edoardo Vacchi