开发者

Is there a good reason to use metaprogramming to modify the return type of a method?

Say you have a method:

boolean test() { return true; }

Would it ever be a good idea to use metaprogramming to change it to return a String:

String test() {return "test"}

I think using metaprogramming for that is confusing and should be avoided since it changes the public interface that calling code expects. Does an开发者_如何学Cyone have an example of when it would be a good idea?


I think if you are dynamically returning different types it would make more sense to just type the method return as def such as:

def test() {
   if (something) {
        return true
   } else {
        return 'test'
   }
}

So to answer your question, no I do not think it would ever be a good idea. :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜