From: Dave Rolsky Date: Sat, 27 Jun 2009 03:34:45 +0000 (-0500) Subject: A bunch of slides on TCs X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=636ceec10724c15edeb555e07630b261e094e74c;p=gitmo%2Fmoose-presentations.git A bunch of slides on TCs --- diff --git a/moose-class/slides/index.html b/moose-class/slides/index.html index 94178fe..c0b9aae 100644 --- a/moose-class/slides/index.html +++ b/moose-class/slides/index.html @@ -2516,6 +2516,378 @@ Iterate til this passes all its tests

Part 5: Types

+
+

A Type System for Perl

+ + +
+ +
+

Components of a Moose Type

+ + +
+ +
+

Built-in Type Hierarchy

+ +
+Any
+Item
+    Bool
+    Maybe[`a]
+    Undef
+    Defined
+        Value
+           Num
+             Int
+           Str
+             ClassName
+             RoleName
+
+
+ +
+

Built-in Type Hierarchy

+ +
+(Item)
+    (Defined)
+        Ref
+            ScalarRef
+            ArrayRef[`a]
+            HashRef[`a]
+            CodeRef
+            RegexpRef
+            GlobRef
+              FileHandle
+            Object
+
+
+ +
+

Bool

+ +

True

+
1
+924.1
+'true'
+{}
+ +

False

+
0
+0.0
+'0'
+undef
+ + +
+ +
+

Value (and subtypes)

+ + +
+ +
+

ClassName and RoleName

+ + +
+ +
+

Parameterizable Types

+ + +
+ +
+

Maybe[`a]

+ + +
+ +
+

Union Types

+ + +
+ +
+

Making Your Own Types

+ +
use Moose::Util::TypeConstraints;
+
+subtype 'PositiveInt',
+    as      'Int',
+    where   { $_ > 0 },
+    message { "The value you provided ($_)"
+              . " was not a positive number." };
+
+has size => (
+    is  => 'ro',
+    isa => 'PositiveInt',
+);
+
+ +
+

Automatic Types

+ + +
+ +
+

Automatic Types

+ +
package Employee;
+use Moose;
+
+has manager => (
+    is  => 'rw',
+    isa => 'Employee',
+);
+
+has start_date => (
+    is  => 'ro',
+    isa => 'DateTime',
+);
+
+ +
+

Subtype Shortcuts - class_type

+ +
use Moose::Util::TypeConstraints;
+class_type 'DateTime';
+
+subtype 'DateTime',
+    as      'Object',
+    where   { $_->isa('DateTime') },
+    message { ... };
+
+ +
+

Subtype Shortcuts - role_type

+ +
use Moose::Util::TypeConstraints;
+role_type 'Printable';
+
+subtype 'Printable',
+    as      'Object',
+    where
+        { Moose::Util::does_role(
+              $_, 'Printable' ) },
+    message { ... };
+
+ +
+

Subtype Shortcuts - duck_type

+ +
use Moose::Util::TypeConstraints;
+duck_type Car => qw( run break_down );
+
+subtype 'Car',
+    as      'Object',
+    where   { all { $_->can($_) }
+              qw( run break_down ) },
+    message { ... };
+
+ +
+

Subtype Shortcuts - enum

+ +
use Moose::Util::TypeConstraints;
+enum Color => qw( red blue green ) );
+
+my %ok = map { $_ => 1 } qw( red blue green );
+subtype 'Color'
+    as      'Str',
+    where   { $ok{$_} },
+    message { ... };
+
+ +
+

Anonymous Subtypes

+ +
package Person;
+
+my $posint =
+    subtype as 'Int', where { $_ > 0 };
+
+has size => (
+    is  => 'ro',
+    isa => $posint,
+);
+ + +
+ +
+

Coercions

+ +
use Moose::Util::TypeConstraints;
+
+subtype 'UCStr',
+    as    'Str',
+    where { ! /[a-z]/ };
+
+coerce 'UCStr',
+    from 'Str',
+    via  { uc };
+
+has shouty_name => (
+    is     => 'ro',
+    isa    => 'UCStr',
+    coerce => 1,
+);
+
+ +
+

Digression: The Type Registry

+ + +
+ +
+

Danger!

+ + +
+ +
+

Fix #1

+ + +
+ +
+

Namespace Fix

+ +
use Moose::Util::TypeConstraints;
+
+subtype 'MyApp::Type::DateTime',
+    as 'DateTime';
+
+coerce 'MyApp::Type::DateTime',
+    from 'HashRef',
+    via  { DateTime->new( %{$_} ) }
+
+has creation_date => (
+    is     => 'ro',
+    isa    => 'MyApp::Type::DateTime',
+    coerce => 1,
+);
+
+ +
+

Namespace Fix

+ +
subtype 'MyApp::Type::ArrayOfInt',
+    as 'ArrayRef[Int]';
+
+coerce 'MyApp::Type::ArrayOfInt',
+    from 'Int',
+    via  { [ $_ ] };
+
+ + +
+

Coercion Examples

+ +
subtype 'My::DateTime',
+    as class_type 'DateTime';
+
+coerce 'My::DateTime',
+    from 'HashRef',
+    via  { DateTime->new( %{$_} ) };
+
+coerce 'My::DateTime',
+    from 'Int',
+    via  { DateTime->from_epoch(
+               epoch => $_ ) };
+ + +
+ +
+

Coercion Examples

+ +
coerce 'ArrayRef[Int]',
+    from 'Int',
+    via  { [ $_ ] };
+ + +
+ +
+

Questions?

+
+ +
+

Exercises

+ +
# cd exercises
+# perl bin/prove -lv t/05-types.t
+
+Iterate til this passes all its tests
+
+

Part 6: Advanced Attributes