Improve error message for missing fields
This commit is contained in:
parent
4658791b97
commit
dc21e59c2a
|
@ -509,7 +509,7 @@ data TypeMessage
|
||||||
| IfBranchMismatch (Expr X) (Expr X) (Expr X) (Expr X)
|
| IfBranchMismatch (Expr X) (Expr X) (Expr X) (Expr X)
|
||||||
| InvalidFieldType Text (Expr X)
|
| InvalidFieldType Text (Expr X)
|
||||||
| NotARecord Text (Expr X) (Expr X)
|
| NotARecord Text (Expr X) (Expr X)
|
||||||
| MissingField Text
|
| MissingField Text (Expr X)
|
||||||
| CantAnd (Expr X) (Expr X)
|
| CantAnd (Expr X) (Expr X)
|
||||||
| CantOr (Expr X) (Expr X)
|
| CantOr (Expr X) (Expr X)
|
||||||
| CantAppend (Expr X) (Expr X)
|
| CantAppend (Expr X) (Expr X)
|
||||||
|
@ -867,10 +867,28 @@ You tried to access a field named:
|
||||||
txt0 = Text.toStrict (pretty k )
|
txt0 = Text.toStrict (pretty k )
|
||||||
txt1 = Text.toStrict (pretty expr0)
|
txt1 = Text.toStrict (pretty expr0)
|
||||||
txt2 = Text.toStrict (pretty expr1)
|
txt2 = Text.toStrict (pretty expr1)
|
||||||
build (MissingField t ) =
|
build (MissingField k expr0) =
|
||||||
"Error: Missing field\n"
|
Builder.fromText [NeatInterpolation.text|
|
||||||
<> "\n"
|
Error: Missing record field
|
||||||
<> "Field: " <> build t <> "\n"
|
|
||||||
|
Explanation: You can only retrieve record fields if they are present
|
||||||
|
|
||||||
|
{ foo = True, bar = "ABC" }.foo -- This is valid ...
|
||||||
|
|
||||||
|
λ(r : {{ foo : Bool, bar : Text }}) → r.foo -- ... and so is this
|
||||||
|
|
||||||
|
... but you *cannot* access fields missing from a record:
|
||||||
|
|
||||||
|
{ foo = True, bar = "ABC" }.qux -- The field `qux` is missing
|
||||||
|
|
||||||
|
You tried to access a field named:
|
||||||
|
↳ $txt0
|
||||||
|
... but the field is missing because the record only has these fields defined:
|
||||||
|
↳ $txt1
|
||||||
|
|]
|
||||||
|
where
|
||||||
|
txt0 = Text.toStrict (pretty k )
|
||||||
|
txt1 = Text.toStrict (pretty expr0)
|
||||||
build (CantAnd e t ) =
|
build (CantAnd e t ) =
|
||||||
"Error: Can't use `(&&)` on a value that's not a `Bool`\n"
|
"Error: Can't use `(&&)` on a value that's not a `Bool`\n"
|
||||||
<> "\n"
|
<> "\n"
|
||||||
|
@ -1224,7 +1242,7 @@ typeWith ctx e@(Field r x ) = do
|
||||||
Record kts ->
|
Record kts ->
|
||||||
case Data.Map.lookup x kts of
|
case Data.Map.lookup x kts of
|
||||||
Just t' -> return t'
|
Just t' -> return t'
|
||||||
Nothing -> Left (TypeError ctx e (MissingField x))
|
Nothing -> Left (TypeError ctx e (MissingField x t))
|
||||||
_ -> Left (TypeError ctx e (NotARecord x r t))
|
_ -> Left (TypeError ctx e (NotARecord x r t))
|
||||||
typeWith _ (Embed p ) = do
|
typeWith _ (Embed p ) = do
|
||||||
absurd p
|
absurd p
|
||||||
|
|
Loading…
Reference in New Issue
Block a user