[DGD] casting nil (another idea...)

Colin Ross c.ross at cantab.net
Tue Mar 23 18:11:45 CET 2004


> Just a small idea. How about being able to typecast nil to integer, 
> converting it to 0? If a variable is expected to hold nil, 0, or another 
> integer, and you want to always treat it as an integer, then:
> 
> luck -= (int)query_property("luck_reduction");
> 
> Looks much neater than:
> 
> int i;
> if((i = query_property("luck_reduction") == nil)
>     i = 0;
> luck -= i;
> 
> And probably saves a few ticks as well.

You could make the query_property() function do the work for you and
return a 0 for non-existent properties.  That's what I tend to do
in these cases where a default of 0 makes sense.

Colin
_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list