TraitとMoose::Role
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

TraitとMoose::Role

  • 58,404 views
Uploaded on

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

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

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
58,404
On Slideshare
58,402
From Embeds
2
Number of Embeds
2

Actions

Shares
Downloads
26
Comments
0
Likes
4

Embeds 2

http://www.slideshare.net 1
http://www.slideee.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide







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

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

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

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





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

Transcript

  • 1. Trait Moose::Role 2009 5 8
  • 2. : Moose::Role Perl Java interface ✤ requires API ✤ with API ✤ Perl gihyo.jp ✤ Trait ✤
  • 3. : Mix-in (1) ✤ 3 ( Linux 2005 7 ) Mix-in Lisp ✤
  • 4. : Mix-in (2) Mix-in ✤ ✤ Mix-in ✤ Mix-in ✤ ✤ ✤
  • 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. Trait OOP ✤ Mix-in ✤ 2002 Traits: Composable Units of Behavior ✤
  • 7. Trait Perl 5(Moose) Perl 6 ✤ JavaScript(Joose) ✤ Java(Scala) ✤ Ruby (module Mix-in ) ✤ Fortress 2006 sun ✤ Smalltalk, PHP, ActionScript 3.0 ✤
  • 8. Trait Mix-in Flatten ✤ (Perl Exporter ) ✤ = Trait ✤ (with, import) ✤ Trait = ✤ Trait ( ) ✤
  • 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. 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. Trait sum : t1 + t2 ✤ alias : t[a→b] ✤ exclusion : t - a ✤
  • 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. 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. 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. 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. 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. 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. 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. Class 1 SuperClass 1 (∵ ) ✤ Trait Trait ⇒ Trait required ✤ (Mix-in ) ⇒ ✤ ✤ alias exclusion ✤
  • 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. Trait state( ) Trait ✤ → has ✤ Trait ✤ → ✤
  • 22. ✤ Trait ✤ Trait Mix-in ✤