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

Erwin Harte harte at is-here.com
Tue Mar 23 18:42:21 CET 2004


On Tue, Mar 23, 2004 at 04:28:43PM +0000, Robert Forshaw wrote:
[...]
> 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.

Unfortunately that is not going to work right for typechecking mode 2,
because the assingment of a non-int value to 'i' in the if-condition
will cause a runtime error of "Value is not an int" or something along
those lines.

I'd go for something like this:

    int nil_to_int(mixed v)
    {
	return v == nil ? 0 : v;
    }

    ...

    luck -= nil_to_int(query_property("luck_reduction"));

If you put the nil_to_int() in an inheritable it doesn't look so bad?

Just a thought,

Erwin.
-- 
Erwin Harte <harte at is-here.com>
_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list