facebook / facebook/flow

Pure functions

Aperta
#2,715 10 commenti 56 reazioni 0 assegnatari Vedi su GitHub
feature request Typing: refinements
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.