开发者

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

开发者 https://www.devze.com 2023-02-02 11:19 出处:网络
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:

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

精彩评论

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

关注公众号