1
我想爲類型爲Example
的對象添加對類String
的轉換。從字符串到某個對象的自定義類型轉換
當我做這樣的
class Example {
def x = 5
}
class ExampleConversionCategory {
static def support = String.&asType
static Object asType(String self, Class cls) {
if (cls == Example.class) {
"convert"
} else { support(cls) } // argument type mismatch
}
}
String.mixin(ExampleConversionCategory)
def x = "5" as int
println x
我得到異常
java.lang.IllegalArgumentException: argument type mismatch
問題是什麼? cls
有Class
類型。