facebook / facebook/flow

Pure functions

Đang mở
#2,715 10 bình luận 56 reaction 0 người được giao Xem trên GitHub
feature request Typing: refinements
Ngôn ngữ chính
Rust
Star
22.3k
Fork
1.9k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.