facebook / facebook/flow

Pure functions

Abierto
#2,715 10 comentarios 56 reacciones 0 asignados Ver en GitHub
feature request Typing: refinements
Lenguaje dominante
Rust
Estrellas
22.3k
Forks
1.9k
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

This is a feature request for marking functions as not having any side effects, aka being pure.

Flow reports the following safe code as unsafe:

```
/* @flow */
type T = { func: ?(number) => void };
function f() { return 7; }
function g(val: T) {
if (val.func) {
val.func(f());
}
}
```

The reasoning for this is because Flow has no idea who else has a reference to `val`, and the call to `f` might modify the value of `val.func`, so the previous non-null assertion is invalidated.

It would be nice to be able to mark functions as `pure`. Flow could assert that:
- no variables outside the local scope are modified (including properties of objects)
- no non-pure functions are called

As a straw man syntax for this, I suggest `pure` before the return type in the type definition:

```
type APureFunction: () => pure void
function aPureFunction(): pure void { }
```

I don't think this is a duplicate of #18 because that focuses on data immutability, which is different from function purity.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.