-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Document [@error_message] * Suggestions during review
- Loading branch information
1 parent
8ad4b07
commit e3ebd78
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# The `[@error_message]` attribute | ||
|
||
You can put the `[@error_message]` attribute on type or kind constraints | ||
to add custom text to error messages, useful when writing a ppx. | ||
|
||
For example, suppose we have a ppx that translates `[%i_have_a x]` to `Some x`. | ||
This makes sense only for `value`s. So the ppx might actually translate | ||
|
||
``` | ||
let f x = [%i_have_a x] | ||
``` | ||
|
||
to | ||
|
||
``` | ||
let f x = Some (x : ((_ : value)[@error_message "only works with values"])) | ||
``` | ||
|
||
which will say `only works with values` to the user if the type of `x` does not | ||
have kind `value`. | ||
|
||
This also works on type constraints like `(x : string)`, | ||
though the implementation on type constraints is more fragile, producing | ||
the custom message only if we already know the type of the expression | ||
before seeing the type constraint. For example, this produces a custom | ||
message: | ||
|
||
``` | ||
let f (x : bool) = (x : int)[@error_message "custom"] | ||
``` | ||
|
||
But this does not: | ||
|
||
``` | ||
let f x = (x : bool)[@error_message "custom"] + 1 | ||
``` | ||
|
||
|