Skip to content

Recommend stdlib functions over Belt for converting between float/int/string #7453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#### :nail_care: Polish

- In type errors, recommend stdlib over Belt functions for converting between float/int/string. https://github.com/rescript-lang/rescript/pull/7453
- Make `Jsx.element` a private empty record to avoid unnecessary `Primitive_option.some`. https://github.com/rescript-lang/rescript/pull/7450

# 12.0.0-alpha.12
Expand Down
12 changes: 6 additions & 6 deletions compiler/ml/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -676,12 +676,12 @@ let rec bottom_aliases = function

let simple_conversions =
[
(("float", "int"), "Belt.Float.toInt");
(("float", "string"), "Belt.Float.toString");
(("int", "float"), "Belt.Int.toFloat");
(("int", "string"), "Belt.Int.toString");
(("string", "float"), "Belt.Float.fromString");
(("string", "int"), "Belt.Int.fromString");
(("float", "int"), "Float.toInt");
(("float", "string"), "Float.toString");
(("int", "float"), "Int.toFloat");
(("int", "string"), "Int.toString");
(("string", "float"), "Float.fromString");
(("string", "int"), "Int.fromString");
]

let print_simple_conversion ppf (actual, expected) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
- Convert all values in the array to the same type.
- Use a tuple, if your array is of fixed length. Tuples can mix types freely, and compiles to a JavaScript array. Example of a tuple: `let myTuple = (10, "hello", 15.5, true)

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
This has type: string
But it's expected to have type: int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
This has type: string
But this function argument is expecting: int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
The incompatible parts:
int vs string

You can convert int to string with Belt.Int.toString.
You can convert int to string with Int.toString.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
This has type: int
But this function argument is expecting: string

You can convert int to string with Belt.Int.toString.
You can convert int to string with Int.toString.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
This has type: string
But it's expected to have type: int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
This has type: string
But it's expected to have type: int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
This has type: string
But it's expected to have type: int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
This has type: string
But it's expected to have type: int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

if expressions must return the same type in all branches (if, else if, else).

You can convert int to string with Belt.Int.toString.
You can convert int to string with Int.toString.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
This has type: int
But it's expected to have type: string

You can convert int to string with Belt.Int.toString.
You can convert int to string with Int.toString.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
This has type: float
But it's expected to have type: int

You can convert float to int with Belt.Float.toInt.
You can convert float to int with Float.toInt.
If this is a literal, try a number without a trailing dot (e.g. 20).
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
- Ensure all values in this calculation has the type float. You can convert between floats and ints via Belt.Float.toInt and Belt.Int.fromFloat.
- Change the operator to +, which works on int

You can convert int to float with Belt.Int.toFloat.
You can convert int to float with Int.toFloat.
If this is a literal, try a number with a trailing dot (e.g. 20.).
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
This has type: int
But it's expected to have type: float

You can convert int to float with Belt.Int.toFloat.
You can convert int to float with Int.toFloat.
If this is a literal, try a number with a trailing dot (e.g. 20.).
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Further expanded:
int vs string

You can convert int to string with Belt.Int.toString.
You can convert int to string with Int.toString.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
This has type: int
But string concatenation is expecting: string

You can convert int to string with Belt.Int.toString.
You can convert int to string with Int.toString.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
This has type: int
But it's expected to have type: float

You can convert int to float with Belt.Int.toFloat.
You can convert int to float with Int.toFloat.
If this is a literal, try a number with a trailing dot (e.g. 20.).
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
- Ensure all values in this calculation has the type float. You can convert between floats and ints via Belt.Float.toInt and Belt.Int.fromFloat.
- Change the operator to +, which works on int

You can convert int to float with Belt.Int.toFloat.
You can convert int to float with Int.toFloat.
If this is a literal, try a number with a trailing dot (e.g. 20.).
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
This has type: string
But it's expected to have type: int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
This has type: string
But it's expected to have type: int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
You're assigning something to this field that has type: int
But this record field is of type: string

You can convert int to string with Belt.Int.toString.
You can convert int to string with Int.toString.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
The incompatible parts:
string vs int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
This has type: string
But it's expected to have type: int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
2 changes: 1 addition & 1 deletion tests/build_tests/super_errors/expected/type1.res.expected
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
This has type: int
But it's expected to have type: float

You can convert int to float with Belt.Int.toFloat.
You can convert int to float with Int.toFloat.
If this is a literal, try a number with a trailing dot (e.g. 20.).
2 changes: 1 addition & 1 deletion tests/build_tests/super_errors/expected/type2.res.expected
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
This has type: string
But this function argument is expecting: int

You can convert string to int with Belt.Int.fromString.
You can convert string to int with Int.fromString.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
This has type: int
But string concatenation is expecting: string

You can convert int to string with Belt.Int.toString.
You can convert int to string with Int.toString.