facebook / facebook/flow

Pure functions

Ouverte
#2,715 10 commentaires 56 réactions 0 personnes assignées Voir sur GitHub
feature request Typing: refinements
Langage dominant
Rust
Étoiles
22.3k
Forks
1.9k
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.