class::contract woes

statico on 2005-02-04T13:51:51

My partner and I were up to 3 AM working on our homework. What killed us at the end was the fact that with all of our Class::Contract modules, preconditions on attributes do not work.

My code appears as follows:

    # x coordinate of Tile on Graph
    # assumptions: +1 => EAST
    attr 'x';
        pre { is_int($_[0]) };
            failmsg "x attribute must be an integer";


Fairly straightforward, right? Before the x attribute is set, make sure that it's an integer (via Data::Types::is_int). The problem is that when this precondition is called, the arguments are not available.

This morning I fired up the debugger and found that the precondition is actually called twice: once with the argument list, and then once to run the closure. That doesn't make sense.

The docs and tests neither document or test this functionality, yet everything in the instructions seems to point to it. Yes, you can use the value() function to retrieve the value of the accessor, but that doesn't any good in a precondition where the value hasn't been set yet.

Arg!