exhaustive record pattern #247
Labels
accepted
The issue has been reviewed and accepted for implementation
language proposal
proposal of a major feature to the language
Proposing syntax to make sure you've looked at all the fields of a record value. Adding or removing a field should in that case warn you that you need to explicitly ignore/use it.
Some common use cases
Parser.number
checking all arguments)pattern syntax
{ _ | command = commandValue }
for inexhaustive match with explicit variable name (equivalent to current{ command = commandValue }
){ _ | command }
for inexhaustive match with field value variable name inherited from field name (equivalent to current{ command }
){ model = modelValue, command = commandValue }
for exhaustive match using all field values{ model = modelValue, command = _ }
for exhaustive match ignoring a field value{ model = modelValue, command }
for exhaustive match using all field values, with a field value variable name inherited from field nameThis is consistent with
{}
being a match on a record with exactly 0 fields, instead of a record with whatever fields.example code
if we then e.g. add
the compiler will give an error about
lightness
not being handled in theupdate
case branch.Had this been an inexhaustive record match as in current gren, the user choice of lightness would simply be ignored, which is a pretty subtle bug!
similar mindset in existing features
\{} ->
instead of\_ ->
for laziness to avoid accidentally ignoring future values. E.g. changing fromTest.test
toTest.fuzz
should trigger a compiler error that you didn't use the fuzzed value{ contextA = old.contextA, contextB = [] }
instead of{ old | contextB = [] }
when you want to assure yourself you've considered for each field whether it needs changing or notconflicts
The proposed exhaustive record pattern does not allow matching on a value of an extensible record type.
This prevents a coding style where functions like view take info as an extensible record (also slightly related: #240).
alternatives considered
current workaround
Explicitly add a
let
declaration for the record with an explicit record fields type annotation which only has the fields you've considered.discussion
This issue is mostly a summary of my points brought up in this discord thread
The text was updated successfully, but these errors were encountered: