How to generate methods in scala
Basically, I have a class, that has several getters that return different Seq
s. But I need to use those collections in java code. I implemented alternate getters, that return java collections explicitly, but I want to do that in a more implicit way. Something like that:
class X开发者_开发技巧 {
@AsJava //annotation that generates method someSeqJava: java.util.List[Int]
def someSeq: Seq[Int] = Seq(1,2,3,4)
}
Or any other way. I do not want to write all those convertions myself all the time. Thanks.
Have a look at JavaConversions
/JavaConverters
.
If you want to use annotations, you might need to have a look at AspectJ.
This is similar to @BeanProperty annotation and it should be relatively easy to create a compiler plugin that does the job. In fact, from your needs, it seems you could hook the plugin after the typer
phase (so that symbols and types are all available).
EDIT:
I'd suggest you to take a look at the following discussions, they might help:
- https://groups.google.com/d/topic/scala-user/UF3KUSwl1C4/discussion
- https://groups.google.com/d/topic/scala-user/H-fjKUYo_QY/discussion
- https://groups.google.com/d/topic/scala-internals/cChbFPMk82w/discussion
- https://groups.google.com/d/topic/scala-debate/Vp4uDutxQkc/discussion
Here is a short guide from the official scala website
- http://www.scala-lang.org/node/140
精彩评论