-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
The runtime error that the CFE produces on a failed pattern match in an irrefutable context should be changed. Currently, the CFE uses ReachabilityError
as a placeholder.
The following is a relevant and very helpful fragment from an offline conversation with @lrhn. Thank you for the clear and elaborate guidance, as always, Lasse!
First of all, a pattern in an irrefutable context cannot fail to match. That should be a compile-time error.
If it fails anyway, it's either because a!
oras
failed, and then it's aTypeError
as usual.
Or it's a list or map pattern which doesn't have the required elements/keys, in which case ... I'd be fine with just doinglist[2]
and letting it throw, butmap["key"]
won't throw, it just returnsnull
, and we should throw if the key isn't there, even if the value pattern would acceptnull
. So we do need to decide what to throw in that case.Not inventing a new error would be nice.
ReachabilityError
is definitely a strawman name 🙂I'd probably go with
StateError
. The state of the object being matched does not support the operation required (having a[2]
element or["key"]
entry).
It's not anArgumentError
, because there is no function and no argument.
It's could beUnsupportedError
, but that's usually reserved for an object which will never support that operation, and a list or map can be updated to have the value.
So,StateError
matches, a state-based inability to support the operation, not an inherent inability like adding to an unmodifiable list.That means it requires a message. I'd go with:
StateError("List pattern requires ${_patternMinLength} elements, list has only ${list.length}") StateError("Map pattern requires key not provided by map value.")