May 20, 2007
Reply to Jan,

> Actually I know all this, and I've been using it this way. Maybe the
> example I gave was a bit misleading but what I actualy want to know is
> if it's possible to use these pre- and postconditions somewhere else
> then with a function body. Because in the manual they state that with
> a funtion is "the most typical use", so I'm asking myself what the
> other uses could be.
> 
> Jan
> 

other uses would be logging

void foo(int i)
in{writef(">foo(%d)\n", i);}
out{writef("<foo()\n");}


May 20, 2007
Reply to Ary,

> It's gone from the documentation, but luckily I've got an old one
> (this is from DMD 0.176). In the "Contracts" section it was said:
> 
> | The in-out statement can also be used inside a function, for
> example,
> | it can be used to check the results of a loop:
> |
> | in
> | {
> |     assert(j == 0);
> | }
> | out
> | {
> |     assert(j == 10);
> | }
> | body
> | {
> |     for (i = 0; i < 10; i++)
> | 	j++;
> | }
> |
> | This is not implemented at this time.
> I guess this is exactly what you want in the example you gave. I don't
> know why it's gone from the documentation, though. Maybe these feature
> was dropped from future plans?
> 

you can fake this with scope(exit)


|assert(j == 0);
|{
|    scope(exit) assert(j == 10);
|    for (i = 0; i < 10; i++)
|        j++;
|}


May 21, 2007
BCS escribió:
> 
> you can fake this with scope(exit)
> 
> 
> |assert(j == 0);
> |{
> |    scope(exit) assert(j == 10);
> |    for (i = 0; i < 10; i++)
> |        j++;
> |}

Almost. If your "in" or "out" bodies have statements that are not exclusively asserts, then they won't be gone when compiling in release mode.

The following would also be nice (althugh I seriosuly doubt people will use it):

| for(int i = 0; i < 10; i++) {
|     invariant {
|        assert(0 >= i && i < 10);
|     }
|     // some code...
| }

Here again, the invariant may contain code that is not exclusively assert, so in release mode it will be gone. And it's much nicer to the eye.
May 26, 2007
Frits van Bommel wrote:
> Derek Parnell wrote:
>> I don't believe that 'in' and 'out' can yet be used for nested functions or
>> anonymous functions.
> 
> It works fine for nested functions:
> ---
> import std.stdio;
> 
> void main()
> {
>     void nested()
>     in {
>         writefln("nested : in");
>     } out {
>         writefln("nested : out");
>     } body {
>         writefln("nested : body");
>     }
> 
>     nested();
> }
> ---
> 
> You seem to be right about anonymous functions though.

It works fine for anonymous functions too:
----
void main(char[][] args) {
    function void ()
    in {
        writefln("nested : in");
    } out {
        writefln("nested : out");
    } body {
        writefln("nested : body");
    } ();
	
	writeln("END");
}
----
One just is not able to use the shortened anonymous function syntax.

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
May 26, 2007
Bruno Medeiros wrote:
> Frits van Bommel wrote:
>> Derek Parnell wrote:
>>> I don't believe that 'in' and 'out' can yet be used for nested functions or
>>> anonymous functions.
>>
[snip nested function counterexample]
>> You seem to be right about anonymous functions though.
> 
> It works fine for anonymous functions too:
> ----
> void main(char[][] args) {
>     function void ()
>     in {
>         writefln("nested : in");
>     } out {
>         writefln("nested : out");
>     } body {
>         writefln("nested : body");
>     } ();
>         writeln("END");
> }
> ----
> One just is not able to use the shortened anonymous function syntax.

Ah, I forgot to try the more explicit form. Good catch.
(Though the fact it took almost a week for someone to post this would seem to indicate that not many people were aware of this fact...)
1 2
Next ›   Last »