P6 OO vs Moose (&Moo)
-
Upload
lichtkind -
Category
Technology
-
view
453 -
download
1
description
Transcript of P6 OO vs Moose (&Moo)
Perl 6 OOP
What is OOP ?
Larry Wall Says:
What is OOP ?
Position
Out Of Position
O O P
Man With Clue
Read That !
Damian Says:
Object-orientedprogramming ... many opinions, theories, and even ideologies have been formulated on the subject. ... Most are mutually inconsistent.
O O P
Classes / Prototypes(Multiple) Inheritance / Roles
MMD + DelegationTypes + Subtypes
Introspection / Metaobj.
His Opinion
TIMTOWTDI
All There in Beauty
In Search Of Perf.
Starts With A Class
Class
class
Class
class
instanceable name space
Class
class module package
Class
class Excalibur; class Babylon;
Class
class
instanceable name space
NS in Braces
class Excalibur { ... }
Object
Object
my $obj = Class.new();
Ops Changed
my $obj = Class.new();
Create New
Clone Existing
Object
my $obj = $old.clone();
Object
my $obj = $old.clone(...);
Positional Paramters
clone($pos1, $pos2);
Named Parameters
clone( :key('value'),);
With Autoquoting
clone( :key<value>,);
Old School Works Too
clone( key=>'value',);
Object
new & clone
bless stayed
Attributes
+
Methods
Space Ship
Class class Spaceship { has Int $.speed; method stop { $speed = 0 } }
I can do that too !
In Perl 5package Spaceship;use Moose;has 'speed' => ( is => 'ro'; isa => 'Int';);sub stop { $self = shift; $self->speed = 0;}
Me too !
In Perl 5package Spaceship;use Moo;has 'speed' => ( is => 'ro'; isa => sub { die "…" unless looks_like_number($_[0]);});sub stop { $self = shift; $self->speed = 0;}
In Perl 5use MooseX::Declare;class Spaceship { has 'speed' => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; }}
Class class Spaceship { has Int $.speed; method stop { $.speed = 0; } }
Attribute Usage
P5 P6
$self->speed $.speedshift->speed self.speed $!speed
Twigil of Accessors
. public ! private
Twigil of Accessors
. public ! private
has $!speed; # private
Twigil of Accessors
. public ! private
has $speed; # private too
trusts
trusts
class Dog { trusts Cat; has $!Bone;}
trusts
class Cat { method steal { my $carlo = Dog.new(); $carlo!Bone = 0; ...
Twigils . punlic access. ! private access. ^ pos. auto para. : named auto p. * global var ? compiler info = POD ~ sublang
Sigils
$ Scalar @ Array % Hash
Sigils
has $.speed;has @.shuttle;has %.crew;
In Perl 5use MooseX::Declare;class Spaceship { has 'speed' => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; }}
In Perl 5use MooseX::Declare;class Spaceship { has 'speed' => ( is => 'rw'; isa => 'Int'; ); method stop { $self->speed = 0; }}
Class class Spaceship { has Int $.speed is rw; method stop { $.speed = 0; } }
Class class Spaceship { has Int $.speed is rw = 0; method stop { $.speed = 0; } }
In Perl 5use MooseX::Declare;class Spaceship { has 'speed' => ( is => 'rw'; isa => 'Int'; default => 0; ); method stop { $self->speed = 0; }}
Perl 6 Attribute
no:isa default (just Syntax)predicate required coercereader writer init_argclearer builder lazy_build
That was my idea!
Perl 6 & Moose
has is
Subtypes
Moose
subtype 'Slogan' => as 'Str' => where {length $_< 50};
Perl 6
my subset Slogan of Str where {$_.chars < 50};
Delegation
Excalibur
Perl 6class Excalibur;has $.clock handles 'now';
$excalibur = Excalibur.new;$excalibur.clock.now;
Perl 6class Excalibur;has DateTime $.clock handles 'now';
$excalibur = Excalibur.new;$excalibur.now;
Moose
has 'clock' => ( handles => 'now'; );
Moose Rename
has 'clock' => ( handles => { now => 'time'
}; );
Perl 6 Renameclass Spaceship;has DateTime $.clock handles { :time<now>};
Methods
Methods
method stop { … }
Methods
method !stop { … }
Methods
method !stop { … }
submethod
Methods
method !stop { … }
submethod # !inherit
MMD
?
MMD
MultiMethodDispatch
MMD
only multi proto
MMD
only # default anywaymulti # look at !proto # later
MMD
multi method navigate (Coord $place) {}
multi method navigate (Str $cmd) {};
MMD
$excalibur.navigate('back');
MMD
only # default anywaymulti # MMDproto # own handling
Inheritance
MooseX::Declare
class WhiteStar extends Spaceship;
Inheritance
extends => is
Perl 6
class WhiteStar is Spaceship;
Multiple Inheritance
class WhiteStaris Spaceship is Minbari;
Vererbung später
extends => also is
MooseX::Declare
class WhiteStar;...extends Spaceship;
Perl 6
class WhiteStar { ... also is Spaceship;
Roles
Class Hierarchy
Where to insert ?
Solution
Role:Unit Of Reusable
Functionality
Therefore
Role:Unit Of Reusable
Functionality
Outside Any Hierarchy
Solution
Role:Unit Of Reusable
Functionality
Trait Elsewhere
Therefore
Role:Unit Of Reusable
Functionality
Roles have Atributes, Traits not
Therefore
Role:Reusable => Small
Remember?
Role:Reusable => Small
Class:instanceable name space
How To Solve That
Role:Reusable => Small
Class:Complete => Big
Class Do Can't Both
Role:Reusable => Small
!=Class:
Complete => Big
Roles
may be inherited !
if mixed into a class
& remove @ run time
Rolesconflicts throw exceptions
Rolesconflicts throw exceptions
No global overwrite likeRuby Mixins
Rolesconflicts throw exceptions
No global overwrite likeRuby Mixins
Refinements doesn't solve it all
Rolesconflicts throw exceptions
Roles > multiple inheritance(conflicts remain unhandled
- in intelligent way)
Rolesconflicts throw exceptions
except when method is empty
Rolesconflicts throw exceptions
except when method is empty
then is has to be overwritten (interface)
Roles role Spaceship { has Int $.speed; method stop { $.speed = 0 } }
Roles role Clock { has DateTime $.time; method alarm { ... } }
Apply Roles
with => does
Moose
class Excalibur extends WhiteStar with Clock;
Moo too !
Moo::Role
package Excalibur;extends 'WhiteStar';with 'Clock';
Perl 6
class Excalibur is WhiteStar does Clock;
Perl 6
class Excalibur is Whitestar;
also does Clock;
Perl 6
class Excalibbur is WhiteStar;
also does Clock does PlasmaGun;
Perl 6
$excalibur does Clock;
Introspection
MethodsWHAT short name WHICH object ID (type)WHO package, long name in str contextWHERE memory addressHOW object of meta classWHEN (reserved for events?)WHY (reserved for documentation)WHENCE autovivification of closures
MethodsWHAT short name WHICH object ID (type)WHO package, long name in str contextWHERE memory addressHOW object of meta classWHEN (reserved for events?)WHY (reserved for documentation)WHENCE autovivification of closures
MethodsWHAT short name WHICH object ID (type)WHO package, long name in str contextWHERE memory addressHOW object of meta classWHEN (reserved for events?)WHY (reserved for documentation)WHENCE autovivification of closures
Introspection
Class.HOW.methods($obj)
Class.^methods()
Metaobjectmethods
identifier name authority version author description subject language licensed parents roles
Deeper & Deeper
$obj.^methods()[$which].signature
Introspection
All is an Object
Introspection
All is an Object
„objects are stupid“.uc
Introspection
All is an ObjectCommands are Methods
Introspection
All is an ObjectCommands are Methods
(Operators too)
Introspection
All is an ObjectCommands are Methods
(Operators too)MMD is everywhere
Introspection
All is an ObjectCommands are Methods
(Operators too)MMD is everywhere
also in Regexes
Name Spaces
package module
class
A Kind Of Class
package module
class grammar
Grammars
grammar { token { … } rule { … } regex { … }}
Learn MoreS12: Objekte,S14: Rollen
perl6.org/documentationhttp://perlcabal.org/syn/
opt. Precise & Volume
Learn More
Perl 6 Docs
doc.perl6.org/language/objects
Opt.: Short & Precise
Learn More
Perl 6 Tablets
tablets.perl6.org
opt.: Hypertext & Volume
Cockaigne
Thank You