0
Trait Moose::Role
2009   5   8
: Moose::Role

            Perl              Java interface
✤




        requires                    API
    ✤




      ...
: Mix-in           (1)


✤


                                    3
    (   Linux 2005   7   )

        Mix-in             ...
: Mix-in   (2)

    Mix-in
✤




    ✤




             Mix-in
    ✤




    Mix-in
✤




    ✤




    ✤
: Mix-in                  (3)
Ruby
                                        SuperClass
module MixinA
 def mixin1
          ...
Trait



           OOP
✤




                 Mix-in
✤




    2002     Traits: Composable Units of Behavior
✤
Trait

    Perl 5(Moose) Perl 6
✤




    JavaScript(Joose)
✤




    Java(Scala)
✤




    Ruby (module Mix-in           ...
Trait Mix-in

    Flatten
✤




                         (Perl   Exporter     )
    ✤




                                ...
Moose::Role Trait
                                                              (provided)
✤




                         ...
Moose::Role Trait

                  Trait
✤




    required
✤




                                                Goods
...
Trait



    sum : t1 + t2
✤




    alias : t[a→b]
✤




    exclusion : t - a
✤
Trait                        (1) - sum

                                           package TraitsA;
    required + provide...
sum :                              ...

                (m, ⊥:     ,⟙:     )             m1 ⊔ ⟙ = ⟙
        →             ...
Trait                (2) - alias

                                   package AnotherTraits;
                              ...
Trait                   (3) - exclusion

                                 package AnotherTraits;
                         ...
Trait                             :
    package TraitsA;
    use Moose::Role;
                                            ...
Trait                             :
    package TraitsA;
    use Moose::Role;
                                            ...
Trait
✤




    package Trait;                                   package Class;
    use Moose::Role;                      ...
Class 1      SuperClass 1       (∵              )
✤


                    Trait   Trait
    ⇒

    Trait                  ...
package TraitA;
use Moose::Role;                    package Class;
sub vs{ print __PACKAGE__, quot;nquot; }   use Moose;
n...
Trait

     state(              )   Trait
 ✤




         → has
     ✤




                 Trait
 ✤




         →
     ✤
✤




    Trait
✤




    Trait Mix-in
✤
Upcoming SlideShare
Loading in...5
×

TraitとMoose::Role

56,951

Published on

Moose::Role による Trait の解説。社内勉強会資料です。

Published in: Technology, Business
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
56,951
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
27
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide







  • ツリー的ではなく、加算的に合成する

  • ここで、例としてTraitsの論文の図を見る

  • provided+providedは、本当は競合。競合にprovidedを加えても競合のまま(1式に矛盾)

  • Mooseのaliasと論文内の記法の向きが違うので注意





  • 物件のようなstateが主要素であるクラス群では実力を発揮しきれない
    Traitは、実装がたまたま同じものを共通化する場合に強力であることに注意

  • Transcript of "TraitとMoose::Role"

    1. 1. Trait Moose::Role 2009 5 8
    2. 2. : Moose::Role Perl Java interface ✤ requires API ✤ with API ✤ Perl gihyo.jp ✤ Trait ✤
    3. 3. : Mix-in (1) ✤ 3 ( Linux 2005 7 ) Mix-in Lisp ✤
    4. 4. : Mix-in (2) Mix-in ✤ ✤ Mix-in ✤ Mix-in ✤ ✤ ✤
    5. 5. : Mix-in (3) Ruby SuperClass module MixinA def mixin1 MixinB print quot;Hello Mixin Anquot; end end SubClass module MixinC def mixin2 MixinA MixinC mixin1 print quot;Hello Mixin Cnquot; end SubSubClass end MixinA class SubSubClass < SubClass include MixinA include MixinC end SuperClass2 SubSubClass.new.mixin2
    6. 6. Trait OOP ✤ Mix-in ✤ 2002 Traits: Composable Units of Behavior ✤
    7. 7. Trait Perl 5(Moose) Perl 6 ✤ JavaScript(Joose) ✤ Java(Scala) ✤ Ruby (module Mix-in ) ✤ Fortress 2006 sun ✤ Smalltalk, PHP, ActionScript 3.0 ✤
    8. 8. Trait Mix-in Flatten ✤ (Perl Exporter ) ✤ = Trait ✤ (with, import) ✤ Trait = ✤ Trait ( ) ✤
    9. 9. Moose::Role Trait (provided) ✤ (required) ✤ ( OK) ✤ package TaxRole; TaxRole use Moose::Role; requires 'price'; comsumption_tax price our $TAX_RATE = 0.05; tax_inclusive_price sub consumption_tax{ return shift->price * $TAX_RATE; } sub tax_inclusive_price{ my $self = shift; return $self->price + $self->consumption_tax; } no Moose::Role;
    10. 10. Moose::Role Trait Trait ✤ required ✤ Goods package Goods; use Moose; price has price => ( isa => 'Int', TaxRole is => 'ro', required => 1, comsumption_tax price ); tax_inclusive_price with 'TaxRole'; no Moose;
    11. 11. Trait sum : t1 + t2 ✤ alias : t[a→b] ✤ exclusion : t - a ✤
    12. 12. Trait (1) - sum package TraitsA; required + provided = provided ✤ use Moose::Role; required + required = required ✤ with 'TraitsB'; provided + provided = required ( ) ✤ no Moose::Role; TraitA TraitB TraitA += a ⇒ a c d b a b d c c d
    13. 13. sum : ... (m, ⊥: ,⟙: ) m1 ⊔ ⟙ = ⟙ → ✤ ✤ ⟘⊔⟙=⟙ ✤ ⟘ ⊔⟘=⟘ ✤ ⟙⊔⟙=⟙ ✤ m1 ⊔ ⟘ = m1 ✤ provided: ⊥ ⟙ ✤ m1 ⊔ m1 = m1 ✤ m1 ⊔ m2 = ⟙ required: - provided ✤ ✤ TraitA TraitB TraitA += a = m3, c = m4 ⇒ a = m1, b = m2 b = m2, c = m4 c = ⟘, d = ⟘ d=⟘ a = ⟙, d = ⟘
    14. 14. Trait (2) - alias package AnotherTraits; use Moose::Role; → ✤ with 'TraitsA' => { alias => {a => 'e'}, }; no Moose::Role; TraitA TraitA [e → a] ⇒ a c a c b d b d e
    15. 15. Trait (3) - exclusion package AnotherTraits; use Moose::Role; − ✤ with 'TraitsA' => { excludes => ['a'], }; no Moose::Role; TraitA TraitA -= a ⇒ a c b c b d d
    16. 16. Trait : package TraitsA; use Moose::Role; TraitA with 'TraitsB' => { a b alias => {b => 'e'}, excludes => ['c'], c }; d e no Moose::Role; TraitA TraitB a d += ( b a [e → b] - c ) b c c d
    17. 17. Trait : package TraitsA; use Moose::Role; TraitA with 'TraitsB' => { a b alias => {b => 'e'}, excludes => ['c'], c }; d e no Moose::Role; TraitA TraitB += ( b = e a d a [e → b] - c ) = b c c d
    18. 18. Trait ✤ package Trait; package Class; use Moose::Role; use Moose; sub class_vs_trait{ print __PACKAGE__, quot;nquot;; } extends 'SuperClass'; with 'Trait'; sub super_vs_trait{ print __PACKAGE__, quot;nquot;; } sub class_vs_trait{ print __PACKAGE__, quot;nquot;; } no Moose::Role; no Moose; package SuperClass; package main; use Moose; my $c = Class->new; sub super_vs_trait{ print __PACKAGE__, quot;nquot;; } $c->class_vs_trait; no Moose; $c->super_vs_trait;
    19. 19. Class 1 SuperClass 1 (∵ ) ✤ Trait Trait ⇒ Trait required ✤ (Mix-in ) ⇒ ✤ ✤ alias exclusion ✤
    20. 20. package TraitA; use Moose::Role; package Class; sub vs{ print __PACKAGE__, quot;nquot; } use Moose; no Moose::Role; with 'TraitA', 'TraitB'; no Moose; package TraitB; use Moose::Role; package main; sub vs{ print __PACKAGE__, quot;nquot; } Class->new->vs; no Moose::Role;
    21. 21. Trait state( ) Trait ✤ → has ✤ Trait ✤ → ✤
    22. 22. ✤ Trait ✤ Trait Mix-in ✤
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×