I just got it! (invariant/const)

Georg Wrede georg at nospam.org
Wed Apr 9 10:14:04 PDT 2008


Janice Caron wrote:
> On 09/04/2008, Georg Wrede <georg at nospam.org> wrote:
> 
> However,
> the following are all equivalent.
> 
>     int f(invariant D d) invariant pure { ... }
>     int f(invariant D d) pure invariant { ... }
>     pure int f(invariant D d) invariant { ... }
>     invariant int f(invariant D d) pure { ... }
>     pure invariant int f(invariant D d) { ... }
>     invariant pure int f(invariant D d) { ... }

Thanks. (Although I'd really love to see less possibilities here. 
Grepping for code, quickly skimming others' code, and for me personally, 
making less mistakes, ...)

> ...unless of course pure implies invariant, which we would hope, in
> which case you only have one of
> 
>     int f(D d) pure { ... }
>     pure int f(D d) { ... }

Excellent.

>>>Well, they can be optimised away, at least.
>>
>> They can be flatly removed!
> 
> That's what "optimised away" means. :-)

:-)

>>  - Any pure function has to return a value.
> 
> To be /useful/ it has to return a value. To be /syntactically
> correct/, probably not. For example:
> 
>     void doNothing(int x) pure
>     {
>     }
> 
>     int doSomething(int x) pure
>     {
>         doNothing(x);
>         return x + 1;
>     }
> 
> That looks OK to me. doNothing() will be optimised away completely,
> but I think it needs to be /allowed/ because such a function could
> conceivably arise as a result of template instantiation or string
> mixin.
> 
>>  - In a pure function, ignoring the return value of a called function is an
>>error.
> 
> I see no reason why that should be an error. Again, generic
> programming might result in such code.
> 
>> This is even stronger than (1) above, it is clearer,
>> and it's *right*.
> 
> Sure, but you don't need to outlaw code that does nothing. :-)

In the same manner as unreachable code is an error with many compilers, 
this might be polite.

But, I do believe the template issue!



More information about the Digitalmars-d mailing list