Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Hi, could you contrast Class::Trait to Class::Role? Thanx!
    • Note that in the following that I do not use the word "role" even though that's the Perl 6 term. This is deliberate to make it clear I'm only talking about Perl 5 traits. Even when I discuss Class::Role I'll call it a trait. Yes, I know that's awkward :)

      Well, first and foremost, Class::Trait [cpan.org] is the only Perl module to have a substantially complete implementation of traits as described in the classic "traits paper [unibe.ch]" that introduced most programmers to traits.

      As for Class::Role, it appears to have the s

      • By the way: The same deficiency could be pointed out about the base pragma. If it would call import on the super class, an abstract super class could enforce its child the implement certain methods.

        I don't know if Class::Trait implements this, but it should be possible for a class that uses a trait to declare that it it is also a trait or abstract class that requires certain methods to be implemented by child classes. If both traits require a "name" method to be implemented, that should not be a compile t

        • it should be possible for a class that uses a trait to declare that it it is also a trait or abstract class that requires certain methods to be implemented by child classes.

          If I understood you correctly, it sounds like you're asking for traits to be composed of other traits and for all of those traits to specify required methods even if the composite of all of those traits does not provide them, thus requiring the class using the traits to make them available. If that is what you mean, then yes, Class::Trait supports this. I've recently uploaded version 0.11. If you check the new t/99_trait_errors.t tests, you'll see code like the following:

          #
          # Extra::TSpouse: explode [ requires lawyer() ]
          #           does: Original::TSpouse: fuse explode [ requires alimony() ]
          #

          clean_inc();
          eval <<'END_PACKAGE';
          package Polygamy;
          use Class::Trait 'Extra::TSpouse';
          Class::Trait->initialize;
          sub lawyer {}
          END_PACKAGE

          ok $@,   'Trying to load a trait which does not meet requirements should fail';
          like $@, qr/\QRequirement (alimony) for Extra::TSpouse not in Polygamy\E/,
            '... and @REQUIREMENTS should bubble up correctly';

          In this code, the trait Extra::TSpouse uses the trait Original::TSpouse. The extra spouse requires a lawyer (note that this method is supplied) and the original spouse requires alimony. Because of the way traits are flattened into classes, the alimony trait is shown for the extra spouse, but the problem is easy to track down.

          I trust you'll forgive my politically incorrect tests :)