Kotlin类型转换

huangapple go评论74阅读模式
英文:

Kotlin TypeCasting

问题

以下是这三个尽管都产生相同结果但不同之处的翻译:

val binding = DataBindingUtil.inflate<MyBinding>(LayoutInflater.from(view.context), R.layout.my_layout,
    null, false)

val binding: MyBinding = DataBindingUtil.inflate(LayoutInflater.from(view.context), R.layout.my_layout,
    null, false)

val binding = DataBindingUtil.inflate(LayoutInflater.from(view.context), R.layout.my_layout,
    null, false) as MyBinding
英文:

What is the difference between the following 3 even though all yield the same result?

val binding = DataBindingUtil.inflate<MyBinding>(LayoutInflater.from(view.context), R.layout.my_layout,
    null, false)

val binding: MyBinding = DataBindingUtil.inflate(LayoutInflater.from(view.context), R.layout.my_layout,
    null, false)

val binding = DataBindingUtil.inflate(LayoutInflater.from(view.context), R.layout.my_layout,
    null, false) as MyBinding

答案1

得分: 4

binding将是从RHS返回的类型。唯一可能返回的错误是运行时错误,指出“null不能是非空类型的值”。

val binding: MyBinding = DataBindingUtil.inflate(LayoutInflater.from(view.context), R.layout.my_layout, null, false)

与上面相同,但如果两个类型不匹配,可能会引发编译时错误,显示Type mismatch. Required: XXX Found: YYY

val binding = DataBindingUtil.inflate(LayoutInflater.from(view.context), R.layout.my_layout, null, false) as MyBinding

这主要用于派生类,但在正常情况下也适用。这不会引发编译时错误,但如果转换不成功,将引发运行时异常

英文:

Let me explain the very basic of it!

>val binding = DataBindingUtil.inflate<MyBinding>(LayoutInflater.from(view.context), R.layout.my_layout,
null, false)

binding will be of type whatever is returned from RHS. Only error this could return is a run time error saying null cannot be the value of a non-nullable type

>val binding: MyBinding = DataBindingUtil.inflate(LayoutInflater.from(view.context), R.layout.my_layout,
null, false)

Same as above but could throw compile-time error saying Type mismatch. Required: XXX Found: YYY if both types don't match.

>val binding = DataBindingUtil.inflate(LayoutInflater.from(view.context), R.layout.my_layout,
null, false) as MyBinding

This should be mainly used for derived classes but work in normal case too. This would not throw any compile time errow but would throw Run time exceptions if casting is not successful.

答案2

得分: 2

没有真正的区别。完整版本是:

val binding: MyBinding = DataBindingUtil.inflate<MyBinding>(...)

但是如果省略变量类型(: MyBinding),它会从类型参数中推断出来,反之亦然(它们在这种情况下相同,因为这个特定方法的签名)。Kotlin还允许从立即的强制转换中推断类型参数,就像在您的第三个示例中一样。如果我记得没错,这是为了一个相当特定的用例而引入的(某个方法以前返回一个超类型,但后来变成了泛型?),否则没有真正的理由使用它。

英文:

There's no real difference. The full version would be

val binding: MyBinding = DataBindingUtil.inflate&lt;MyBinding&gt;(...)

but if you leave out the variable type (: MyBinding), it is inferred from the type parameter, and vice versa. (They are the same in this case, because of this particular method's signature.)

Kotlin also allows inferring type parameters from an immediate cast, like in your third example. IIRC this was introduced for a pretty specific use-case (some method used to return a supertype but later became generic?) and there is no real reason to use it otherwise.

huangapple
  • 本文由 发表于 2020年1月3日 18:32:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定