Skip to content

Idea: Implicit open for arithmetic infix operators #6477

@cometkim

Description

@cometkim

Note: This proposal is not related to the custom infix operators. Just improves the ergonomics of the + operator for built-in types.

One of the most awkward parts of introducing ReScript today is the arithmetic operators. Especially when using +., -. for float .

To be clear, the distinction of integers and floatings and strict type checking are not bad at all. However, the fact that it introduces various operators such as +, +., and ++ depending on the type is awkward for users from JS or other languages that support overloading / extensions.

This is clearly a constraint from OCaml. The situation can get worse as we drop support for custom infix operators.

For example, https://forum.rescript-lang.org/t/new-operator-for-bigint-support/3922 when we add support for BigInt literals, should we add new arithmetic operators for it? how?

I tried to think a simple concept. Please let me know if this is not possible.

When an arithmetic operator (+, -, *, /) is detected in an expression, the module containing the infix implementation of the left type is implicitly local-open. If ReScript adds further operator support equivalent to JS, like >>, <<, %, ** may be added.

1.2 + 3. treated as Float.Infix.(1.2 + 3.) so it could be compiled instead of type error. 1.2 + 3 should be a type error as it is. To prevent this behavior from being abused by other modules, the compiler uses a restricted list of supported types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions