Stdlib soundness of option returning functions

Aberta
#9,233 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
5/5
Tempo estimado
Mais de uma semana
Facilidade para iniciantes
25/100
Tipo de issue
Funcionalidade
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
ocaml

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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())
Linguagem predominante
OCaml
Estrelas
7.5k
Forks
485
Merge médio
17h 9min
PRs com merge (30d)
264

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de rescript-lang/rescript

Todas as issues de rescript-lang/rescript

Issues semelhantes

Mais issues de Backend & API Design

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.