GroovyTruth和隐式构造函数之间的冲突

时间:2013-11-12 17:00:30

标签: groovy implicit-conversion specifications

我注意到隐式构造函数和GroovyTruth之间存在一些冲突。

考虑以下代码

assert new File('/') == ['/'] as File
assert Boolean.TRUE == ["false"] as Boolean

第一行是对File(String)构造函数的隐含调用。 第二行只返回 true ,因为list不为空。但它可以(应该?)调用具有不同结果值的布尔(String)构造函数( false )。

是bug,记录的功能还是smth。其他?我应该将其报告为错误吗?

1 个答案:

答案 0 :(得分:1)

当你这样做时:

['false'] as Boolean

最终通过DefaultTypeTransformation.castToTypecalls castToBoolean as you can see检查null,然后在asBoolean类型上调用Collection just checks it's not empty

使用File示例,它会落到castToType的底部,只是尝试调用constructor with the contents of the list

我不会说这是一个错误,但它肯定是Groovy的一个特质,必须考虑到(并且现在改变它将是兼容性的巨大突破)