D - contracts bug
- Daniel Yokomiso (49/49) Nov 28 2002 Hi,
- Walter (3/5) Dec 02 2002 Yes. result is declared implicitly.
Hi, Is this code valid? int[] fun(int i) in { assert(i > 0); } out (result) { assert(result.length == 10); } body { int[] res = new int[10]; res[] = i; int isZero = (result[0] == 0); printf("Is first item zero? %.*s\r\n", cast(char[]) (isZero ? "true" : "false")); result[0] = 0; return res; } int main() { int[] values = fun(2); return 0; } I think it should not be, unless result (declared in out section) function as an implicit variable, like Eiffel defines the Result implicit variable. This code compiles and run (dmd 0.50). Statement 'int isZero = (result[0] == 0);' executes fine, but 'result[0] = 0;' raises an 'Access Violation'. Implicit result variable looks like a good idea, but is terrible when the language provides closures. The following code demonstrates this: template TCollection(T) { class Collection { ... boolean all(boolean (*predicate)(T)) { ... } ... } Collection copyFrom(Collection other) out (result) { assert(result.length == other.length); assert(result !== other); // if result is an implicit variable, the result used in the // closure represents the closure result, not the copyFrom result assert(other.all(fun(item) {return (item in result);}); } body { ... } Best regards, Daniel Yokomiso. "Those are my principles. If you don't like them I have others." - Groucho Marx
Nov 28 2002
"Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> wrote in message news:as6i38$1f3e$1 digitaldaemon.com...Hi, Is this code valid?Yes. result is declared implicitly.
Dec 02 2002