rescript-lang / rescript-lang/rescript

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

Open
#7,027 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
OCaml
Stars
7.5k
Forks
485
Avg merge
1d 2h
Merged PRs (30d)
55

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the compiler's special handling for tagged strings and function arity behind the @as(json...) mechanism. Explore whether zero-cost raw functions can replace it while preserving external bindings and inlining raw JavaScript; done means the hack is removed without adding runtime cost.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, ocaml
Domain
compilers
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.