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

TraitとMoose::Role

on

  • 58,195 views

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

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

Statistics

Views

Total Views
58,195
Views on SlideShare
58,193
Embed Views
2

Actions

Likes
4
Downloads
26
Comments
0

2 Embeds 2

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

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • ツリー的ではなく、加算的に合成する <br />
  • <br />
  • ここで、例としてTraitsの論文の図を見る <br />
  • <br />
  • provided+providedは、本当は競合。競合にprovidedを加えても競合のまま(1式に矛盾) <br />
  • <br />
  • Mooseのaliasと論文内の記法の向きが違うので注意 <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • 物件のようなstateが主要素であるクラス群では実力を発揮しきれない <br /> Traitは、実装がたまたま同じものを共通化する場合に強力であることに注意 <br />
  • <br />

TraitとMoose::Role TraitとMoose::Role Presentation Transcript

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