HaxeFoundation / HaxeFoundation/haxe

Method contracts

Open
#8,905 6 comments 1 reaction 0 assignees View on GitHub
discussion
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

As discussed on Slack, it would be nice to be able to annotate methods with pre- and post-conditions.

```haxe
@:pre(x >= 3 && x < array.length + 3)
@:post(return != null)
static function getOffset(x:Int):String {
return array[x - 3];
}
```

Potential applications:

- parse the contracts with the static analyser to avoid unnecessary checks (#8815)
- pass the contracts onto an external verifier (Viper, Boogie, Z3…) and emit compilation errors when the code does not fulfil the contract
- parse with dox to slightly enhance documentation

There are also some problems to work out with the syntax, e.g., how to represent:

- the return value? - In the example I used the `return` keyword, which might work.
- the previous value of a variable? - Conventionally annotated with `old`. There might also be issues due to variable shadowing or scoping.
- type assertions? - If a dynamic is passed in?
- enum case assertions?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.