-
Lets say I have definitions of fruits and want to define recipes, but the recipes should only contain defined fruits: let fruits = {
banana.calories = 200,
apple.calories = 150,
peach.calories=100,
}
in
let FruitsAreValid = std.contract.from_predicate (fun ingredients =>
ingredients
|> std.record.fields
|> std.array.all (fun used => std.array.elem used (std.record.fields fruits) )
)
in
{
ingredients = {
apple = 5,
banana = 2,
peach = 3,
melon = 1, # this is invalid
}|FruitsAreValid,
} This gives the following error:
How can I apply a contract to record field names, such that the error only points towards the melon? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The
Unfortunately, I don't know of a stable way of making the error point at the field name. That is something we should add to the error reporting interface, in my opinion. |
Beta Was this translation helpful? Give feedback.
The
from_predicate
function doesn't give you a way of customizing error messages. You can write the contract explicitly like this:Unfortunately, I don't know of a stable way of making …