I keep checking in an out of the ReScript ecosystem. I love it, but there are things I’m missing - automated bindings and better error messages.
To keep ReScript even more relevant, can we incorporate AI for error messages? Could we have an LLM reading the documentation and then explaining why, in this particular context of my code, the error message is there? Could it provide an example of what I should be doing instead or how I can fix it? Can it fix it for me?
here’s a toy example:
me:
FAILED: src/tasks/Task.cmj
We've found a bug for you!
/.../src/tasks/Task.res:15:13-15
13 │ let taskfn = async (db: Mongoose.Connection.t, _task: TaskModel.t) => {
14 │ let now = Date.make()
15 │ let foo = now + 1
16 │
17 │ let oneYearAgo = Date.fromFloat(Date.now())
This has type: Prelude.Date.t (defined as Js_date.t)
Somewhere wanted: int
rescript: [131/150] src/tasks/Task.cmj
1. explain the error
2. provide patched code where the error has been fixed```
response:
Certainly, let's provide an explanation from the perspective of the compiler:
**Error Explanation (as the Compiler):**
In the code you provided, there is a type mismatch error. The line causing the error is:
```rescript
let foo = now + 1
The now
variable is of type Prelude.Date.t
(which is essentially Js_date.t
), representing a date, while you are trying to add 1
to it. However, somewhere else in your code, it’s expected that foo
should have a type of int
.
ReScript is strict about type safety, and it’s flagging this as an issue because you’re trying to perform an operation (addition) that doesn’t make sense between a date and an integer.
Patched Code (as the Compiler):
To fix this error and ensure type compatibility, you need to explicitly convert the date to an int
or adjust your logic to match the expected type. Here’s a corrected version of the code:
let taskfn = async (db: Mongoose.Connection.t, _task: TaskModel.t) => {
let now = Date.make()
let foo = Date.getTime(now) + 1 /* Now, foo is an int representing time in milliseconds */
let oneYearAgo = Date.fromFloat(Date.now())
/* Rest of your code */
}
In this patched code, we use Date.getTime
to convert the date now
to an integer (representing milliseconds since epoch) and then add 1
. This way, foo
is now of type int
, and the error is resolved, ensuring type compatibility throughout the code.
Using some clever prompt engineering, could we have automatically fixed errors or really good in-context explanations of errors? are there other build systems already attempting to do this which we could look at?