HaxeFoundation / HaxeFoundation/haxe
Express function purity in type system.
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
This is related to #6021, but not quite the same thing. Any data type that defines a `map` operation may do certain optimizations if it can assume that the provided function is pure. In that case it's safe not to run the transformation. Consider this example:
```haxe
sequence({ from:0, to: 1000000000 })
.map(function dup(x) return x * 2)
.skip(5000).limit(4)
.iterate(function (x) trace(x));
```
A good implementation will execute `dup` four times only. But it depends on the implementor and the user sharing the understanding that the argument to `map` must be pure and therefore it's safe not to run it on 0 to 4999 and 5000 to 999999999.
Technically, it is possible to write code like:
```haxe
sequence({ from:0, to: 1000000000 })
.map(function dup(x) return x * 2)
.skip(5000).limit(4)
.map(function (x) trace(x));//oh no!
```
Now, this may sound like a silly example. The concrete problem we keep running into at Docler is that every now and then people use `map` / `flatMap` on a `Future` from tink_core to produce a side effect (e.g. update the state of the current class instance in scope). And if the resulting `Future` is never actually consumed (by registering a `Callback`), the side effect doesn't occur and confusion ensues.
I'd like to be able to write something like this:
```haxe
abstract Future {
function map(f:PureR>):Future;
}
```
Mostly just so that people get a clear error when sticking an impure function where a pure one is expected.
In the same vain, it would be nice to be able to check this:
```haxe
interface Foo {
@:pure function bar():Void;
}
```
In that case any implementation of `bar` would have to either be determined to be pure by the compiler, or explicitly marked so by the user (if the compiler's analysis falls short or if they simply want to put in a trace statement).
Contributor guide
Assessment
This issue has not been assessed yet.