View mode: basic / threaded / horizontal-split · Log in · Help
October 31, 2007
D 2.x invariant question
Would the following function:
invariant Body	opIndex	(Key k)
{  if (k in _cache)
   { ....
      return  _cache[k].bdy;
   }
   return  null;
}

return values that were invariant, or would that type have to 
be declared at the declaration of the type "Body"?
October 31, 2007
Re: D 2.x invariant question
"Charles D Hixson" <charleshixsn@earthlink.net> wrote in message 
news:fgao71$21fp$1@digitalmars.com...
> Would the following function:
> invariant Body opIndex (Key k)
> {  if (k in _cache)
>    { ....
>       return  _cache[k].bdy;
>    }
>    return  null;
> }
>
> return values that were invariant, or would that type have to be declared 
> at the declaration of the type "Body"?

I think this makes the method opIndex invariant, which means this function 
can only access invariant members, or something.  If you want an 
invariant(Body), use... invariant(Body) as the return type.

Aside: performance increase, you can avoid the double lookup:

if(auto val = k in cache)
{
   ..use val here..
   return val.bdy;
}

'in' returns a pointer to the value, and you can declare and assign a 
variable in the condition of an 'if' statement.
November 01, 2007
Re: D 2.x invariant question
Jarrett Billingsley wrote:
> "Charles D Hixson" <charleshixsn@earthlink.net> wrote in message 
> news:fgao71$21fp$1@digitalmars.com...
>> Would the following function:
>> invariant Body opIndex (Key k)
>> {  if (k in _cache)
>>    { ....
>>       return  _cache[k].bdy;
>>    }
>>    return  null;
>> }
>>
>> return values that were invariant, or would that type have to be declared 
>> at the declaration of the type "Body"?
> 
> I think this makes the method opIndex invariant, which means this function 
> can only access invariant members, or something.  If you want an 
> invariant(Body), use... invariant(Body) as the return type.
> 
> Aside: performance increase, you can avoid the double lookup:
> 
> if(auto val = k in cache)
> {
>     ..use val here..
>     return val.bdy;
> }
> 
> 'in' returns a pointer to the value, and you can declare and assign a 
> variable in the condition of an 'if' statement. 
> 
> 
Thanks.
(I *kenw* that about "in", but I keep forgetting.)
Top | Discussion index | About this forum | D home