Stdlib soundness of option returning functions

Offen
#9,233 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
25/100
Issue-Typ
Feature
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
ocaml

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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())
Vorherrschende Sprache
OCaml
Sterne
7.5k
Forks
485
Ø Merge
17 Std. 9 Min.
Gemergte PRs (30 T.)
264

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus rescript-lang/rescript

Alle Issues in rescript-lang/rescript

Ähnliche Issues

Weitere Issues zu Backend & API Design

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.