Additional ideas about the design by contract RFC
| From: | Net Mo | Date: | Mon, 17 Aug 2015 08:07:35 +0000 |
| Subject: | Additional ideas about the design by contract RFC | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-87758@lists.php.net to get a copy of this message | ||
I like the idea a lot, although I'm not so sure of the proposed syntax.
I would make it less specific. Preconditions/Postconditions assertions are
indeed useful but the functionality could be extended to regular checks as
well by making the error user defined.
Plus, I would prefer having named checks rather than having the expression
repeated every time: it's great for reusability and signatures would be
shorter (... they are very lengthy already).
For instance:
public function offsetGet(int{NonNegative} $offset){}
function NonNegative(int $offset){
if($offset < 0) throw new WhateverError;
}
If one wants them to act like assertions could still write:
function NonNegative(int $offset){
assert($offset >= 0);
}
Some blue sky thinking code is here: http://pastie.org/10355629
Although, I'm not sure whether the validation function should be a simple
named function or a type:
public function baz(Foo{MyChecker} $foo);
interface MyChecker extends Validator{}
class MyStrictChecker implements MyChecker{
function check($value){
if($value < 10) throw new MyException;
}
}
class MyDevChecker implements MyChecker{
function check($value){
assert($value >= 10);
}
}
That could be useful, I believe, but kind of makes "the contract" too
abstract. What do you think?
Forgive my poor english.