On Wed, 17 Aug 2005, Luke Palmer wrote:

Dependencies, Exclusions, and "Require one-of"

With P::V I can do this:

   { credit_card_number =>
     { optional => 1,
       depends => [ 'credit_card_expiration', 'credit_card_holder_name' ] },

     credit_card_expiration => { optional => 1 },

     credit_card_holder_name => { optional => 1 },
   }

I have no idea how I might do this in Perl6, but I would love to see it
supported as part of parameter declarations

You sortof can:

   sub validate (+$credit_card_number,
                 +$credit_card_expiration,
                 +$credit_card_holder_name)
       where { defined $credit_card_number xor
                 defined $credit_card_expiration &&
                 defined $credit_card_holder_name }
   {...}

But that's really yucky.

Hideous, indeed. Presumably with macros or some other compile time thing that can be turned into something more palatable.

Similarly, something I've wanted to add to P::V is exclusions:

   { credit_card_number =>
     { optional => 1,
       excludes => [ 'ach_bank_account_number' ] },
   }

Another thing that would be really nice would be to require one of a set
of parameters, or one set out of multiple sets, so we could say "we need
credit card info _or_ bank account info".

Yeah, I suppose that would be nice.  However, when you're doing these
kinds of complex dependencies, you'd like to provide your own error
messages when they fail.  That is, instead of:

   '$credit_card_number excludes $ach_bank_account_number'

You could say:

   'You can't give a credit card number and a bank account number, silly!'

Actually, I forgot to mention this in my original post, but in general it'd be nice to be able to provide some sort of callback/object to be called whenever a parameter check fails, and it'd be nice to be able to provide more than one, so I can have custom parameter exception logic per class or sub.

So I wonder whether this kind of logic is better for a P::V module in
Perl 6.  Let somebody else think about the hard stuff like that.

It'd be nice to catch this at compile time whenever possible, though.

Presumably this can be done with the existing language.  It doesn't add
anything at compile time, so it really doesn't need to be part of the
language.

Even things that do add things at compile time don't need to be part
of the language.  That's why "use" is a macro.  :-)

Yes, but see above. I know we can do things like add syntax at compile time, but can we do these sorts of checks? I'm sure the answer is yes, but how easy will it be? Of course, if it's implemented via a C6AN module it's only got to be done once, but it's worth thinking about.


-dave

/*===================================================
VegGuide.Org                        www.BookIRead.com
Your guide to all that's veg.       My book blog
===================================================*/

Reply via email to