Skip to content

Commit

Permalink
Document [@error_message] (#3086)
Browse files Browse the repository at this point in the history
* Document [@error_message]

* Suggestions during review
  • Loading branch information
goldfirere authored Sep 27, 2024
1 parent 8ad4b07 commit e3ebd78
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions ocaml/jane/doc/extensions/error_message.md
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
```


0 comments on commit e3ebd78

Please sign in to comment.