开发者

defining variables with type in groovy

开发者 https://www.devze.com 2022-12-29 04:18 出处:网络
Is this valid ? def CallableStatement st try { ... st = sqlConn.prepareCall(\"call....\") ... } what I\'m really worried about is can you specify type and also use def at开发者_运维知识库 the s

Is this valid ?

def CallableStatement st

try {
 ...     
 st = sqlConn.prepareCall("call....")
 ...
}

what I'm really worried about is can you specify type and also use def at开发者_运维知识库 the same time?


Is this valid ?

Yes and no....

Yes, because the compiler will happily compile and execute the code above, but no, because it really doesn't make any sense to type something as def and also assign it as an explicit type. Basically what you're saying is "this can have any type, but it must be a CallableStatement". In my opinion, the definition above should generate a compiler error.

In practice this definition:

def CallableStatement st

Appears to be identical to:

CallableStatement st

As the following illustrates:

class Foo { 
  def List l;
}


new Foo().l = new ArrayList()  // this works
new Foo().l = "ddd"  // this throws a GroovyCastException
0

精彩评论

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