Can I generate foo_ = and foo accessors from setFoo and getFoo?

I am starting to use Scala on Android, and many access methods are standard JavaBeans accessories, for example. setTitle(...) and getTitle() . Itโ€™s a lot nicer to use the title_= and title methods in scala, so I can write code like:

 button.title = "Foo" 

Is there a way to automatically display them from JavaBeans-style accessories, possibly using the Dynamic trait?

+6
source share
3 answers

I think Dynamic will work, except that it is currently not supported by syntactic sugar. In addition, it would return AnyRef , since there is no way to pass what the expected return type is.

Of course, you can simply use the pimp my library to add the appropriate Scala line getters and setters.

+3
source

I'm not sure if this is supported on Android, but you can use @BeanProperty :

 class X { @scala.reflect.BeanProperty var y:String = _ } val x = new X() x.setY("Test") println(x.getY) //--> Test 

There are more annotations to support Bean, for example. @BooleanBeanProperty and @BeanInfo .

0
source

I do not think that a dynamic trait will help you. I think you need to speculate in order to use the methods, and they will not be detectable through reflection.

I think that you are best off creating a plugin for the scala compiler (see http://www.scala-lang.org/node/140 and http://www.sts.tu-harburg.de/people/mi.garcia / ScalaCompilerCorner / ).

0
source

Source: https://habr.com/ru/post/889818/


All Articles