Skip to content

Get rid of @as(json hack for default arguments of external functions #7027

Open
@cknitt

Description

@cknitt

Currently, default arguments for bindings to external functions can be supplied with

@as(json`someLiteral`) _

where someLiteral is a JS number, string or object literal.

This is used in several bindings, like in this example (adapted from rescript-react-native):

@val
external unsafeAddStyle: (@as(json`{}`) _, style, {..}) => style = "Object.assign"

Unfortunately, the implementation of this feature in the compiler is quite hacky, requiring special handling for both tagged strings and function arity.

We should therefore try to find a better, but still zero-cost mechanism to replace it.

We discussed that a possible direction to explore would be "zero cost raw functions", meaning that the above example could be written as something like the following, with raw JS code as the function definition:

@rawFunction
external unsafeAddStyle: (style, {..}) => style = "(style, obj) => Object.assign({}, style, obj)"

This means that the raw JS function definition would be inlined where the external is used.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions