Contracts for getters/setters
I found this example of refinement types from Raku(Perl 6) and decided to try to implement it as a property property of the class.
And found that contracts cannot be declared for setters
public int x { set requires (-10 <= x <= 10) { set(value); } }
error: syntax error, expected '{'
public int x { set requires (-10 <= x <= 10) { set(value); } }
^^^^^^^^
The way out that I found is either to use a regular assert at the beginning of the function, or like this and it looks very bad:
class Klass : Object {
private int _x;
public void set (int x) requires (-10 <= x <= 10) { _x = x;}
public int x { set { set(value); } }
}
static int main(string[] args) {
var arr = new Klass();
arr.x = 5;
arr.x = -11;
return 0;
}
Edited by gavr