E4118#
Cannot match type with map pattern.
You can use map patterns on custom types, as long as they have a method get
that returns an optional value for a given key.
Erroneous example#
///|
priv struct MyMap {}
///|
test {
let map : MyMap = {}
match map {
{ "a": a, .. } => println("a: \{a}")
//^~~~~~~~~~~~~~
// Error: Please implement method `get` for type MyMap to match it with map pattern.
}
}
Suggestion#
Implement the get method as suggested in the error message.
///|
priv struct MyMap {
entries : Map[String, Int]
}
///|
fn MyMap::get(self : MyMap, key : String) -> Int? {
self.entries.get(key)
}
///|
test {
let map : MyMap = { entries: { "a": 1, "b": 2, "c": 3 } }
match map {
{ "a": a, .. } => println("a: \{a}")
_ => println("missing")
}
}