generics in Groovy

后端 未结 2 1094
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 11:39

The following Groovy code prints \"it works\"

def printIt(Class clazz) {
  println \"it works\"
}

printIt(String.class)
<         


        
2条回答
  •  粉色の甜心
    2021-01-18 12:24

    Check out this link.

    [...]In some ways this is at odds with the emphasis of dynamic languages where in general, the type of objects can not be determined until runtime. But Groovy aims to accomodate Java's static typing when possible, hence Groovy 1.5 now also understands Generics. Having said that, Groovy's generics support doesn't aim to be a complete clone of Java's generics. Instead, Groovy aims to allow generics at the source code level (to aid cut and pasting from Java) and also where it makes sense to allow good integration between Groovy and Java tools and APIs that use generics.[...]

    In conclusion, I don't think it's possible to obtain that information at runtime.

提交回复
热议问题