E4118#
不能用映射模式匹配此类型。
您可以在自定义类型上使用映射模式,只要它们有一个方法 op_get
,该方法返回给定键的可选值即可。
错误示例#
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.
}
}
建议#
请根据错误消息中的建议实现 op_get
方法。
pub fn MyMap::op_get[K: Eq + Hash, V](self : MyMap[K, V], key : K) -> V? {
self._[key]
}