Stdlib soundness of option returning functions

Đang mở
#9,233 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
ocaml
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Start by examining the standard library bindings for Array.shift, Array.pop, Array.get, and Map.get, then trace how option-returning functions are represented and compiled. The issue proposes several competing approaches, so first determine which behavior maintainers want; done means the selected approach makes these functions sound while addressing the interop edge cases raised.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Array.shift, Array.pop, Array.get, Map.get, and likely others are unsound, see Playground.

Here is what a correct version of shift could be:

let shiftSafe = xs =>
  if xs->Array.isEmpty {
    None
  } else {
    let value = xs->Array.getUnsafe(0)
    let _ = xs->Array.shift
    Some(value)
  }

This is a common mistake when writing JS bindings and can be unexpected to users who expect ReScript to be a safe language.

I couldn't find a previous discussion, but I believe this needs a bit of consideration. The choices I see are:

  1. Implement safe ReScript version of Stdlib functions, rename the zero overhead to shiftUnsafe, getUnsafe, etc.
  2. Fix the compiler's handling of option-returning functions.
  3. Fix the representation of options.

Unfortunately, I think there will always be edge-cases that make (2) a losing battle. Even with correct code generation, people often use imprecise types for interop making compile-time information insufficient to prevent unexpected results. For example, users storing undefined containing data in a JSON type.

Similar to the previous reasoning, (3) is likely going to cause too many issues from users that are relying on a particular option implementation.

So I believe (1) is the correct choice for the standard library. However, this still makes the "obvious" implementation of JS bindings written by users prone to errors. One idea would be to generate a fixture by default unless disabled:

@send external shift: array<'a> => option<'a> = "shift"
@send @noInterop external shiftUnsafe: array<'a> => option<'a> = "shift"
// Stdlib_Array.bs.js
let shift = t => Primitive_option.some(t.shift())
Ngôn ngữ chính
OCaml
Star
7.5k
Fork
484
Merge trung bình
17 giờ 9 phút
Pull request đã merge (30 ngày)
264

Chuẩn bị môi trường

Mở trong Codespaces

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.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rescript-lang/rescript

Tất cả issue của rescript-lang/rescript

Issue tương tự

Thêm issue về Backend & API Design

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.