<h1>Part 5: Types</h1>
</div>
+<div class="slide">
+ <h1>A Type System for Perl</h1>
+
+ <ul>
+ <li>Sort of ...</li>
+ <li><em>Variables</em> are not typed</li>
+ <li>Attributes can have types</li>
+ <li>MooseX modules let you define method signatures</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Components of a Moose Type</h1>
+
+ <ul>
+ <li>A type is a name and a constraint</li>
+ <li>Types have a hierarchy</li>
+ <li>Constraints are cumulative from parents</li>
+ <li>Types can have associated coercions</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Built-in Type Hierarchy</h1>
+
+ <pre>
+Any
+Item
+ Bool
+ Maybe[`a]
+ Undef
+ Defined
+ Value
+ Num
+ Int
+ Str
+ ClassName
+ RoleName
+</pre>
+</div>
+
+<div class="slide">
+ <h1>Built-in Type Hierarchy</h1>
+
+<pre>
+(Item)
+ (Defined)
+ Ref
+ ScalarRef
+ ArrayRef[`a]
+ HashRef[`a]
+ CodeRef
+ RegexpRef
+ GlobRef
+ FileHandle
+ Object
+</pre>
+</div>
+
+<div class="slide">
+ <h1>Bool</h1>
+
+ <h2>True</h2>
+ <pre><code>1
+924.1
+'true'
+{}</code></pre>
+
+ <h2>False</h2>
+ <pre><code>0
+0.0
+'0'
+undef</code></pre>
+
+ <ul>
+ <li>Like Perl's <code>if ($foo)</code></li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Value (and subtypes)</h1>
+
+ <ul>
+ <li>Value is true when <code>! ref $thing</code></li>
+ <li>An overloaded object which numifies does not pass the <code>Num</code> constraint!</li>
+ <li>Perl 5's overloading is hopelessly broken</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>ClassName and RoleName</h1>
+
+ <ul>
+ <li>A string with a package name</li>
+ <li>The package <strong>must already be loaded</strong></li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Parameterizable Types</h1>
+
+ <ul>
+ <li>What does <code>ArrayRef[`a]</code> mean?</li>
+ <li><code>s/`a/Int/</code> (or <code>Str</code> or ...)</li>
+ <li>When you use it you can write ...
+ <ul>
+ <li><code>ArrayRef</code> (== <code>ArrayRef[Item]</code>)</li>
+ <li><code>ArrayRef[Str]</code></li>
+ <li><code>ArrayRef[MyTypeName]</code></li>
+ <li><code>ArrayRef[HashRef[Maybe[Int]]]</code></li>
+ </ul>
+ </li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Maybe[`a]</h1>
+
+ <ul>
+ <li>Maybe means either the named type or <code>undef</code></li>
+ <li><code>Maybe[Int]</code> accepts integers or <code>undef</code></li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Union Types</h1>
+
+ <ul>
+ <li>This or that (or that or ...)</li>
+ <li><code>Int | ArrayRef[Int]</code></li>
+ <li>But use a coercion instead when possible</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Making Your Own Types</h1>
+
+ <pre><code>use Moose::Util::TypeConstraints;
+
+<span class="incremental current">subtype 'PositiveInt',</span>
+ <span class="incremental">as 'Int',</span>
+ <span class="incremental">where { $_ > 0 },</span>
+ <span class="incremental">message { "The value you provided ($_)"
+ . " was not a positive number." };</span>
+
+has size => (
+ is => 'ro',
+ <span class="incremental">isa => 'PositiveInt',</span>
+);</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Automatic Types</h1>
+
+ <ul>
+ <li>Moose creates a type for every Moose class and role</li>
+ <li>Unknown names are assumed to be classes</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Automatic Types</h1>
+
+ <pre><code>package Employee;
+use Moose;
+
+has manager => (
+ is => 'rw',
+ <span class="highlight">isa => 'Employee',</span>
+);
+
+has start_date => (
+ is => 'ro',
+ <span class="highlight">isa => 'DateTime',</span>
+);</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Subtype Shortcuts - <code>class_type</code></h1>
+
+ <pre><code>use Moose::Util::TypeConstraints;
+class_type 'DateTime';
+
+subtype 'DateTime',
+ as 'Object',
+ where { $_->isa('DateTime') },
+ message { ... };</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Subtype Shortcuts - <code>role_type</code></h1>
+
+ <pre><code>use Moose::Util::TypeConstraints;
+role_type 'Printable';
+
+subtype 'Printable',
+ as 'Object',
+ where
+ { Moose::Util::does_role(
+ $_, 'Printable' ) },
+ message { ... };</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Subtype Shortcuts - <code>duck_type</code></h1>
+
+ <pre><code>use Moose::Util::TypeConstraints;
+duck_type Car => qw( run break_down );
+
+subtype 'Car',
+ as 'Object',
+ where { all { $_->can($_) }
+ qw( run break_down ) },
+ message { ... };</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Subtype Shortcuts - <code>enum</code></h1>
+
+ <pre><code>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 { ... };</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Anonymous Subtypes</h1>
+
+ <pre><code>package Person;
+
+<span class="highlight">my $posint =
+ subtype as 'Int', where { $_ > 0 };</span>
+
+has size => (
+ is => 'ro',
+ <span class="highlight">isa => $posint,</span>
+);</code></pre>
+
+ <ul>
+ <li>Shortcuts have anonymous forms as well</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Coercions</h1>
+
+ <pre><code>use Moose::Util::TypeConstraints;
+
+subtype 'UCStr',
+ as 'Str',
+ where { ! /[a-z]/ };
+
+<span class="incremental current">coerce 'UCStr',</span>
+ <span class="incremental">from 'Str',</span>
+ <span class="incremental">via { uc };</span>
+
+has shouty_name => (
+ is => 'ro',
+ isa => 'UCStr',
+ <span class="incremental">coerce => 1,</span>
+);</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Digression: The Type Registry</h1>
+
+ <ul>
+ <li>Types are actually <code>Moose::Meta::TypeConstraint</code> <em>objects</em></li>
+ <li>Stored in an interpreter-global registry mapping names to objects</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Danger!</h1>
+
+ <ul>
+ <li>Coercions are attached to type objects</li>
+ <li>Therefore also global</li>
+ <li>Name conflicts between modules!</li>
+ <li>Coercion conflicts between modules!</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Fix #1</h1>
+
+ <ul>
+ <li>Use some sort of pseudo-namespacing scheme</li>
+ <li>Never coerce directly to a class name, or <em>to</em> built-in types</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Namespace Fix</h1>
+
+ <pre><code>use Moose::Util::TypeConstraints;
+
+subtype <span class="highlight">'MyApp::Type::DateTime',</span>
+ as 'DateTime';
+
+<span class="highlight">coerce 'MyApp::Type::DateTime',</span>
+ from 'HashRef',
+ via { DateTime->new( %{$_} ) }
+
+has creation_date => (
+ is => 'ro',
+ <span class="highlight">isa => 'MyApp::Type::DateTime',</span>
+ coerce => 1,
+);</code></pre>
+</div>
+
+<div class="slide">
+ <h1>Namespace Fix</h1>
+
+ <pre><code>subtype 'MyApp::Type::ArrayOfInt',
+ as 'ArrayRef[Int]';
+
+coerce 'MyApp::Type::ArrayOfInt',
+ from 'Int',
+ via { [ $_ ] };</code></pre>
+</div>
+
+
+<div class="slide">
+ <h1>Coercion Examples</h1>
+
+ <pre><code>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 => $_ ) };</code></pre>
+
+ <ul>
+ <li>Using a coercion to inflate a value</li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Coercion Examples</h1>
+
+ <pre><code>coerce 'ArrayRef[Int]',
+ from 'Int',
+ via { [ $_ ] };</code></pre>
+
+ <ul>
+ <li>Coerce instead of a union like <code style="white-space: nowrap">Int | ArrayRef[Int]</code></li>
+ </ul>
+</div>
+
+<div class="slide">
+ <h1>Questions?</h1>
+</div>
+
+<div class="slide">
+ <h1>Exercises</h1>
+
+ <pre># cd exercises
+# perl bin/prove -lv t/05-types.t
+
+Iterate til this passes all its tests</pre>
+</div>
+
<div class="slide fake-slide0">
<h1>Part 6: Advanced Attributes</h1>
</div>