|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
D - contracts bug
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... Dec 02 2002
|