RFC: New syntax for fixed external arguments (replacing `@as(...) _`)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- compilers
Hướng nghiên cứu
Read the RFC’s four syntax proposals and compare their tradeoffs; the payload also points to parser and printer changes and compiler work in #8875 and #8876. Start by reviewing the existing fixed argument parser and printer behavior. Done means a syntax is chosen and its parser and printer changes are specified; this RFC is seeking design feedback, so implementation work is not yet defined.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
External bindings sometimes need an argument that is always the same value and that callers never pass, e.g. document.createElement("img") or Object.assign({}, ...). Today such fixed arguments are written as an @as(...) attribute on a _ type:
@send external createImg: (document, @as("img") _) => element = "createElement"
@val external assign: (@as(json`{}`) _, t, {..}) => t = "Object.assign"
This RFC proposes a dedicated syntax for them and presents four options:
%raw("..."): the value as JavaScript source.=value/~label=value: the value as a ReScript literal, in "default argument" style._ = value/~label: _ = value: the value as a ReScript literal, after the_that marks the argument as not passed by callers.fixed value/~label: fixed value: the value as a ReScript literal, after a contextualfixedkeyword.
These are starting points, not a closed list: better proposals are very welcome.
Motivation
The current syntax has several problems:
- The meaning is hidden in an attribute.
@as(...)on_turns a type into a value, which is hard to discover and unlike anything else in the language.@asalso means something quite different on record fields and variant constructors. - Anything beyond strings and integers needs the special
jsontag, e.g.@as(json`{}`)for an empty object,@as(json`null`)or@as(json`false`). This madejsontemplate literals a special case in the compiler. - The value is easy to misread as a type, and the formatter and editor tooling see an attribute rather than a value.
The compiler side is being prepared independently of the syntax: #8875 gives fixed arguments their own parsetree node and removes the special json constant. #8876 adds option 1 as a provisional syntax. Whichever option this RFC settles on, it is a parser and printer change on top of that node.
Options
The examples bind document.createElement("img"), Object.assign({}, ...), history.replaceState(null, "", url) and an @obj option that is always false.
Option 1: %raw(...)
@send external createImg: (document, %raw(`"img"`)) => element = "createElement"
@val external assign: (%raw("{}"), t, {..}) => t = "Object.assign"
@val external replaceState: (%raw("null"), %raw(`""`), string) => unit = "history.replaceState"
@obj external makeOptions: (~objectMode: %raw("false"), ~name: string, unit) => options = ""
The payload is JavaScript source and must be exactly one JavaScript literal.
- Pros
- Reuses an existing construct; no new grammar.
- Clearly an escape hatch, consistent with
%rawelsewhere. - Can express any JavaScript literal, including regexps and bigints.
- Cons
- Strings need two levels of quoting (
%raw(`"img"`)). - The value is a string of JavaScript, so it gets no highlighting or formatting, and mistakes are reported late.
- Encourages raw JavaScript for what are almost always plain constants.
- Strings need two levels of quoting (
Option 2: =value and ~label=value
@send external createImg: (document, ="img") => element = "createElement"
@val external assign: (={}, t, {..}) => t = "Object.assign"
@val external replaceState: (=null, ="", string) => unit = "history.replaceState"
@obj external makeOptions: (~objectMode=false, ~name: string, unit) => options = ""
The value is a ReScript literal: string, number, boolean, null, undefined, array, object or record.
- Pros
- The labelled form mirrors the call site (
~objectMode=false). - Values are ordinary ReScript literals: no double quoting, and the compiler produces the JavaScript.
- The formatter and editor tooling treat the value like any other expression.
- The labelled form mirrors the call site (
- Cons
~x=valueis exactly the syntax of a default argument in function definitions, where the caller may still pass a different value. Here the caller cannot pass it at all.- The unlabelled form
="img"looks unusual. ~x=valueand~x: t=?side by side can be confusing.- Can't express JavaScript-only literals such as regexps.
Option 3: _ = value and ~label: _ = value
@send external createImg: (document, _ = "img") => element = "createElement"
@val external assign: (_ = {}, t, {..}) => t = "Object.assign"
@val external replaceState: (_ = null, _ = "", string) => unit = "history.replaceState"
@obj external makeOptions: (~objectMode: _ = false, ~name: string, unit) => options = ""
The values are ReScript literals, as in option 2.
- Pros
- The closest to today's syntax (
@as(x) _becomes_ = x). It keeps the_, which already says "the caller does not pass this". - The same shape for unlabelled and labelled arguments.
- Values are ordinary ReScript literals, with the same benefits as option 2.
- The closest to today's syntax (
- Cons
- Slightly more verbose than option 2.
~x: _ = valueresembles a typed default argument (~x: int=1) in function definitions.- Can't express JavaScript-only literals such as regexps.
Option 4: fixed value and ~label: fixed value
@send external createImg: (document, fixed "img") => element = "createElement"
@val external assign: (fixed {}, t, {..}) => t = "Object.assign"
@val external replaceState: (fixed null, fixed "", string) => unit = "history.replaceState"
@obj external makeOptions: (~objectMode: fixed false, ~name: string, unit) => options = ""
The values are ReScript literals, as in options 2 and 3. fixed is a contextual keyword: it only has this meaning when a value follows it, which is never valid type syntax today, so types, record fields and variables named fixed keep working.
- Pros
- Says what it means:
fixed {}reads as "this argument is always{}", with no resemblance to default arguments. - The same shape for unlabelled and labelled arguments, and reads well in both.
- Values are ordinary ReScript literals, with the same benefits as option 2.
- Easy to search for and to document.
- Says what it means:
- Cons
- Adds a (contextual) keyword to the language.
- Editor syntax highlighting needs to learn it.
- Can't express JavaScript-only literals such as regexps.
Details common to all options
- Where they're allowed. Fixed arguments are only allowed as direct arguments of an
external, inside the parenthesized argument list, soexternal f: _ = "f"keeps its current meaning. - Optional arguments. A fixed argument can't be optional, because it is always passed.
- Values in options 2 to 4. The value is read as written and isn't type-checked:
{}means an empty JavaScript object, andnull/undefinedmean the JavaScript values. - Migration.
rescript formatrewrites the existing@as(...) _forms to the new syntax automatically. Until then they keep working with a deprecation warning, and they will be removed in a later major version. The stdlib and Belt would be migrated in the same release. - PPX compatibility. PPXs keep seeing fixed arguments in the existing form,
_ [@as ...], so they aren't affected.
Feasibility
All four options have been implemented or prototyped in the parser. Option 1 is in #8876. Options 2 to 4 parse without conflicting with existing syntax, compile to the expected JavaScript, and leave the existing parser and printer test suites unchanged. For option 4, existing code using fixed as a type, field or variable name is unaffected.
Question
Which option do you prefer, and why? Or do you have a better idea?
- Ngôn ngữ chính
- ReScript
- Star
- 7.5k
- Fork
- 483
- Merge trung bình
- 1 ngày 3 giờ
- Pull request đã merge (30 ngày)
- 136
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của rescript-lang/rescript
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
rescript-lang/rescript#8806 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
rescript-lang/rescript#8798 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 Nửa ngày Mức phù hợp với người mới 58/100
rescript-lang/rescript#8757 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 40/100
rescript-lang/rescript#8753 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 22/100
rescript-lang/rescript#8733 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của rescript-lang/rescript
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
JakeChampion/lang#12684 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`lvm_compact` has no self-hosted twin, and `docs/158` §5 says every language-face component mustĐang mởbug sev-3 vm
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
Bisect issue233157Đang mởbisect
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
dtcxzyw/llvm-bisect-service#545 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
Maintainer thường phản hồi trong vòng 1 ngày