E4125#
?
operator cannot be used with ..
.
..
operator allows chaining of method call that returns Unit
. This is
especially useful when the method performs side-effects, like modifying the
object itself, and thus returns Unit
.
Using ?
materializes the error that will be raised by the method to
Result[T, E]
. If one combine the ?
with ..
, the method call will return
Result[T, E]
instead of Unit
, which is not allowed by the ..
operator.
错误示例#
type A Double derive(Show)
pub fn A::check(self : A) -> Unit! {
if self._.is_nan() {
fail!("NaN")
}
}
pub fn A::div(self : A, other : A) -> A {
A(self._ / other._)
}
fn main {
let a : A = 1.0
let b : A = 2.0
println(a.div(b)..check?())
}
建议#
Use the single dot syntax to call the method, and store the result in a variable.
fn main {
let a : A = 1.0
let b : A = 2.0
println(a.div(b).check?())
}