Perl 6 OOP
Was ist OOP?
Du meinst OPP?
Was ist OOP?
Position
Out Of Position
OOP
Mann Mit Ahnung
Lies Das!
Damian Sagt: Object-oriented programming... many opinions, theories, and even ideologies have been formulated on the subject.... Most are mutually inconsistent.
OOP Klassen / Prototypen (Multiple) Vererbung / Rollen MMD + Delegation Typen + Subtypen Introspektion / Metaobj.
Seine Meinung
All There in Beauty
In Search Of Perf.
Klasse
Klasse class
Klasse class instanzierbarer Namensraum
Klasse class module package
Klasse class Excalibur; class Babylon;
Klasse class Instanzierbarer Namensraum
NR in Klammern class Excalibur {... }
Objekt
Objekt my $obj = Klasse.new();
Objekt my $obj = Klasse.new();
Neu Erschaffen
Bestehendes Klonen
Objekt my $obj = $alt.clone();
Objekt my $obj = $alt.clone(...);
Positionale Paramter clone($pos1, $pos2);
Benannte Parameter clone( :key('value'),);
Mit Autoquoting clone( :key<value>,);
Old School Geht Auch clone( key=>'value',);
Objekt new & clone bless blieb
Attribute + Methoden
Raumschiff
Klasse class Raumschiff { has Int $.speed; method stop { $.speed = 0 } }
Kann Ich auch!
In Perl 5 package Raumschiff; use Moose; has 'speed' => ( is => 'ro'; isa => 'Int'; ); sub stop { $self = shift; $self->speed = 0; }
In Perl 5 use MooseX::Declare; class Spaceship { has 'speed' => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; } }
Klasse class Raumschiff { has Int $.speed; method stop { $.speed = 0; } }
Attributbenutzung P5 $self->speed shift->speed P6 $.speed self.speed $!speed
Twigil der Accessoren.! öffentlich privat
Twigil der Accessoren.! öffentlich privat has $!speed; # privat
Twigil der Accessoren.! has $speed; öffentlich privat # auch privat
trusts
trusts class Hund { trusts Katze; has $!Knochen; }
trusts class Katze { method stehlen { my $tim = Hund.new(); $tim!knochen = 0;...
.! ^ : *? = ~ Twigils öffentliche A. private A. pos. auto para. ben. Auto para. globale compiler info POD sublang
Sigils $ @ % Skalar Array Hash
Sigils has $.speed; has @.shuttle; has %.crew;
In Perl 5 use MooseX::Declare; class Raumschiff { has 'speed' => ( is => 'ro'; isa => 'Int'; ); method stop { $self->speed = 0; } }
In Perl 5 use MooseX::Declare; class Raumschiff { has 'speed' => ( is => 'rw'; isa => 'Int'; ); method stop { $self->speed = 0; } }
Klasse class Raumschiff { has Int $.speed is rw; } method stop { $.speed = 0; }
Klasse class Raumschiff { has Int $.speed is rw = 0; } method stop { $.speed = 0; }
In Perl 5 use MooseX::Declare; class Raumschiff { has 'speed' => ( is => 'rw'; isa => 'Int'; default => 0; ); method stop { $self->speed = 0; } }
Perl 6 Attribute kein: isa default (nur Syntax) predicate required coerce reader writer init_arg clearer builder lazy_build
Hab ich mir ausgedacht!
Perl 6 & Moose has is
Subtypen
Moose subtype 'Slogan' => as 'Str' => where {length $_< 50};
Perl 6 my subset Slogan of Str where {$_.chars < 50};
Delegation
Excalibur
Perl 6 class Excalibur; has $.clock handles 'now'; $excalibur = Excalibur.new; $excalibur.clock.now;
Perl 6 class Excalibur; has DateTime $.clock handles 'now'; $excalibur = Excalibur.new; $excalibur.now;
Moose Delegation has 'clock' => ( handles => 'now'; );
Umbenennen M* has 'clock' => ( handles => { now => 'time' }; );
Umbenennen P6 has DateTime $.clock handles { :time<'now'>};
Perl 6 Rename class Spaceship; has DateTime $.clock handles { :time<'now'>};
Methoden
Methods method stop { }
Methods method!stop { }
Methods method!stop { } submethod
Methods method!stop { } submethod #!erbbar
MMD?
MMD Multi Method Dispatch
MMD only multi proto
MMD only #sowieso default multi # anschaun! proto # später
MMD multi method navigate (Coord $place) {} multi method navigate (Str $cmd) {};
MMD $excalibur.navigate('back');
MMD only #sowieso default multi # MMD proto # selber regeln
Vererbung
MooseX::Declare class WhiteStar extends Spaceship;
Vererbung extends => is
Perl 6 class WhiteStar is Spaceship;
Mehrfachvererbung class WhiteStar is Spaceship is Membari;
Vererbung später extends => also is
MooseX::Declare class WhiteStar;... extends Spaceship;
Perl 6 class WhiteStar {... also is Spaceship;
Rollen
Klassenhierarchie
Wo kommt die Neue rein?
Lösung des Problems Rolle: wiederverwendbare Funktionseinheiten
Optimierung Rolle: Möglichst Klein
Kl. Erinnerung Rolle: Möglichst Klein Klasse: instanzierbarer Namensraum
Alles Was Objekt braucht Rolle: Möglichst Klein Klasse: Vollständig => Groß
Klasse Kann nicht beides Rolle: Möglichst Klein!= Klasse: Vollständig => Groß
Rollen werden vererbt! wenn in die Klasse & zur Laufzeit raus
Rollen Konflikte werfen Ausnahme
Rollen Konflikte werfen Ausnahme Überschreiben nicht global wie Ruby Mixins
Rollen Konflikte werfen Ausnahme Rollen > Mehrfachvererbung (dort bleiben Konflikte auch unbemerkt)
Rollen Konflikte werfen Ausnahme außer wenn Methode leer
Rollen Konflikte werfen Ausnahme außer wenn Methode leer dann muß überschrieben werden (Interface)
Rollen role Excalibur { has Int $.speed; method stop { $.speed = 0 } }
Rollen role Clock { has DateTime $.time; method alarm {... } }
Rollen anwenden with => does
Moose class Excalibur extends Spaceship with Clock;
Perl 6 class Excalibur is Spaceship does Clock;
Perl 6 class Excalibur is Spaceship; also does Clock;
Perl 6 class Excalibur is Spaceship; also does Clock does PlasmaGun;
Perl 6 $excalibur does Clock;
Introspektion
Methoden WHAT short name WHICH object ID (type) WHO package, long name in str context WHERE memory address HOW object of meta class WHEN (reserved for events?) WHY (reserved for documentation) WHENCE autovivification of closures
Methoden WHAT short name WHICH object ID (type) WHO package, long name in str context WHERE memory address HOW object of meta class WHEN (reserved for events?) WHY (reserved for documentation) WHENCE autovivification of closures
Methoden WHAT short name WHICH object ID (type) WHO package, long name in str context WHERE memory address HOW object of meta class WHEN (reserved for events?) WHY (reserved for documentation) WHENCE autovivification of closures
Introspektion Class.HOW.methods($obj) Class.^methods()
Metaobjektmethoden identifier name authority version author description licensed subject parents language roles
Immer tiefer $obj.^methods()[$which].signature
Introspektion Alles is ein Objekt
Introspektion Alles is ein Objekt Objekte sind doof.uc
Introspektion Alles is ein Objekt Befehle sind Methoden
Introspektion Alles is ein Objekt Befehle sind Methoden (Operatoren auch)
Introspektion Alles is ein Objekt Befehle sind Methoden (Operatoren auch) MMD ist überall
Introspektion Alles is ein Objekt Befehle sind Methoden (Operatoren auch) MMD ist überall Auch in den Regex
Namenräume package module class
Auch 'ne Art Klasse package module class grammar
Grammatiken grammar { token { } rule { } regex { } }
Lern Mehr S12: Objekte,S14: Rollen perl6.org/documentation http://perlcabal.org/syn/ opt. Präzision & Umfang
Lern Mehr Perl 6 Docs doc.perl6.org/language/objects optimiert: Kürze & Genauigkeit
Lern Mehr Perl 6 Tablets tablets.perl6.org opt.: Hypertext & Umfang
Schlaraffenland
Thank You