Hi, All
枚举中的payload获取我有些疑问。
- 第一个是内置的Result[T, Err]
有获取T的方法, 我没有找到获取Err的方法, 就是说, 希望能获取到Err, 而不是panic - 自定义的枚举, 如何获取payload, 是否需要提供unwrap0, unwrap1…这样的方法?
比如
pub(all) enum Either[M, N] {
Left(M)
Right(N)
} derive(Show, Eq)
需要能够获取到M,N, 而不是Left(M), Right(N)
所以, 要提供如下方法吗? 不确定最佳实践是什么,
// unwarp0
pub fn unwrap0[M, N](self: Either[M, N]) -> M? {
match self {
Left(x) => Some(x)
Right(_) => None
}
}
// unwrap1
pub fn unwrap1[M, N](self: Either[M, N]) -> N? {
match self {
Left(_) => None
Right(x) => Some(x)
}
}