E4118#
Cannot match type with map pattern.
You can use map patterns on custom types, as long as they have a method op_get
that returns an optional value for a given key.
错误示例#
type MyMap[K, V] Map[K, V]
fn main {
let map : MyMap[String, Int] = { "a": 1, "b": 2, "c": 3 }
match map {
{ "a": a, .. } => println("a: \{a}")
//^~~~~~~~~~~~~~
// Error: Please implement method `op_get` for type MyMap[String, Int] to match it with map pattern.
}
}
建议#
Implement the op_get
method as suggested in the error message.
pub fn MyMap::op_get[K: Eq + Hash, V](self : MyMap[K, V], key : K) -> V? {
self._[key]
}