开发者

How to generate methods in scala

开发者 https://www.devze.com 2023-04-06 07:16 出处:网络
Basically, I have a class, that has several getters that return different Seqs. But I need to use those collections in java code. I implemented alternate getters, that return java collections explicit

Basically, I have a class, that has several getters that return different Seqs. 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
0

精彩评论

暂无评论...
验证码 换一张
取 消