typo
[gitmo/moose-presentations.git] / moose-class / slides / index.html
index c0b9aae..a44f504 100644 (file)
@@ -52,7 +52,8 @@ img#me05 {top: 43px;left: 36px;}
 
 <div class="slide">
   <h1>Introduction to Moose</h1>
-  <h2>YAPC 2009</h2>
+  <h2>OSCON 2009</h2>
+  <h2><a href="git://git.moose.perl.org/moose-presentations.git"><tt>git://git.moose.perl.org/moose-presentations.git</tt></a></h2>
 </div>
 
 <div class="slide">
@@ -940,7 +941,7 @@ sub BUILD {
 </div>
 
 <div class="slide">
-  <h1>The Object is Oqaque</h1>
+  <h1>The Object is Opaque</h1>
 
   <ul>
     <li>Technically it's a hash reference</li>
@@ -1127,6 +1128,7 @@ Person->can('extends');</code></pre>
 
   <ul>
     <li><code>no Moose</code> at the end of a package is a best practice</li>
+    <li>Or <code>namespace::clean</code> at the top</li>
     <li>Just do it</li>
   </ul>
 </div>
@@ -1187,9 +1189,10 @@ use Moose;
   <h1>Exercises</h1>
 
   <pre># cd exercises
-$ perl bin/prove -lv t/00-prereq.t
 
-Missing anything? Install it. (see tarballs/)
+# perl bin/prove -lv t/00-prereq.t
+
+# perl install-moose (if needed)
 
 # perl bin/prove -lv t/01-classes.t
 
@@ -2599,7 +2602,8 @@ undef</code></pre>
   <h1>Value (and subtypes)</h1>
 
   <ul>
-    <li>Value is true when <code>! ref $thing</code></li>
+    <li><code>Value</code> is true when <code>! ref $thing</code></li>
+    <li><code>Value</code> and <code>Str</code> are effectively the same, but <code>Str</code> is more expressive</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>
@@ -2641,12 +2645,14 @@ undef</code></pre>
 </div>
 
 <div class="slide">
-  <h1>Union Types</h1>
+  <h1>Type Union</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>
+    <li>Or use a <code>role_type</code>,  <code>duck_type</code>, or anything not a union</li>
+    <li>A union is often a code smell</li>
   </ul>
 </div>
 
@@ -2784,6 +2790,113 @@ has shouty_name =&gt; (
 </div>
 
 <div class="slide">
+  <h1>Coercion Examples</h1>
+
+  <pre><code>subtype 'My::DateTime',
+    as class_type 'DateTime';
+
+coerce 'My::DateTime',
+    from 'HashRef',
+    via  { DateTime-&gt;new( %{$_} ) };
+
+coerce 'My::DateTime',
+    from 'Int',
+    via  { DateTime-&gt;from_epoch(
+               epoch =&gt; $_ ) };</code></pre>
+
+  <ul>
+    <li>Use 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>Using Types with Attributes</h1>
+
+  <pre><code>package Person;
+
+use Moose::Util::TypeConstraints;
+
+has height =&gt; (
+    is  =&gt; 'rw',
+    <span class="highlight">isa =&gt; 'Num',</span>
+);
+
+has favorite_numbers =&gt; (
+    is     =&gt; 'rw',
+    <span class="highlight">isa    =&gt; 'ArrayRef[Int]',
+    coerce =&gt; 1,</span>
+);</code></pre>
+</div>
+
+<div class="slide">
+  <h1>More Droppings</h1>
+
+  <ul>
+    <li><code>Moose::Util::TypeConstraint</code> also needs cleanup</li>
+  </ul>
+
+  <pre><code>package Person;
+
+use Moose;
+use Moose::Util::TypeConstraints;
+
+subtype ...;
+
+no Moose;
+<span class="highlight">no Moose::Util::TypeConstraints;</span></code></pre>
+</div>
+
+<div class="slide">
+  <h1>Typed Methods (Low-tech)</h1>
+
+  <pre class="medium"><code>package Person;
+<span class="highlight">use MooseX::Params::Validate qw( validated_list );</span>
+
+sub work {
+    my $self = shift;
+    <span class="highlight">my ( $tasks, $can_rest ) =
+        validated_list(
+            \@_,
+            tasks    =&gt;
+                { isa    =&gt; 'ArrayRef[Task]',
+                  coerce =&gt;1 },
+            can_rest =&gt;
+                { isa     =&gt; 'Bool',
+                  default =&gt; 0 },
+        );</span>
+
+    ...
+}</code></pre>
+</div>
+
+<div class="slide">
+  <h1>Typed Methods (High-tech)</h1>
+
+  <pre class="medium"><code>package Person;
+
+<span class="highlight">use MooseX::Method::Signatures;</span>
+
+<span class="highlight">method work ( ArrayRef[Task] :$tasks,
+                        Bool :$can_rest = 0 )</span> {
+    my $self = shift;
+
+    ...
+}</code></pre>
+</div>
+
+<div class="slide">
   <h1>Digression: The Type Registry</h1>
 
   <ul>
@@ -2804,7 +2917,7 @@ has shouty_name =&gt; (
 </div>
 
 <div class="slide">
-  <h1>Fix #1</h1>
+  <h1>Namespace Fix</h1>
 
   <ul>
     <li>Use some sort of pseudo-namespacing scheme</li>
@@ -2841,37 +2954,82 @@ coerce 'MyApp::Type::ArrayOfInt',
     from 'Int',
     via  { [ $_ ] };</code></pre>
 </div>
-    
 
 <div class="slide">
-  <h1>Coercion Examples</h1>
+  <h1>Namespace Fix Pros and Cons</h1>
 
-  <pre><code>subtype 'My::DateTime',
-    as class_type 'DateTime';
+  <ul>
+    <li><span class="right">Relatively simple</span></li>
+    <li><span class="right">Already built into Moose</span></li>
+    <li><span class="wrong">Conflates type and module namespaces</span></li>
+    <li><span class="wrong">Type names are strings, so typos are easy to make and may be hard to find</span></li>
+  </ul>
+</div>
 
-coerce 'My::DateTime',
-    from 'HashRef',
-    via  { DateTime-&gt;new( %{$_} ) };
+<div class="slide">
+  <h1>MooseX::Types</h1>
 
-coerce 'My::DateTime',
-    from 'Int',
-    via  { DateTime-&gt;from_epoch(
-               epoch =&gt; $_ ) };</code></pre>
+  <pre><code>package MyApp::Types;
+
+use MooseX::Types
+    <span class="highlight">-declare =&gt; [ qw( ArrayOfInt ) ]</span>;
+use MooseX::Types::Moose
+    qw( ArrayRef Int );
+
+subtype <span class="highlight">ArrayOfInt</span>,
+    as ArrayRef[Int];
+
+coerce <span class="highlight">ArrayOfInt</span>
+    from Int,
+    via  { [ $_ ] };</code></pre>
+</div>
+
+<div class="slide">
+  <h1>MooseX::Types</h1>
+
+  <pre><code>package MyApp::Account;
+
+use MyApp::Types qw( ArrayOfInt );
+
+has transaction_history => (
+    is  => 'rw',
+    isa => ArrayOfInt,
+);</code></pre>
+</div>
+
+<div class="slide">
+  <h1>MooseX::Types</h1>
 
   <ul>
-    <li>Using a coercion to inflate a value</li>
+    <li>Type names are exported functions, catches typos early</li>
+    <li>Types must be pre-declared</li>
+    <li>Types are stored with namespaces internally, but externally are short</li>
+    <li>Import existing Moose types as functions from <code>MooseX::Types::Moose</code></li>
+    <li>Still need string names for things like <code>ArrayRef['Email::Address']</code></li>
   </ul>
 </div>
 
 <div class="slide">
-  <h1>Coercion Examples</h1>
+  <h1>MooseX::Types Pros and Cons</h1>
 
-  <pre><code>coerce 'ArrayRef[Int]',
-    from 'Int',
-    via  { [ $_ ] };</code></pre>
+  <ul>
+    <li><span class="right">Catches typos at compile time</span></li>
+    <li><span class="right">Automatic namespacing</span></li>
+    <li><span class="wrong">One more thing to install and learn</span></li>
+    <li><span class="wrong">Every name gets types twice (declared and then defined)</span></li>
+    <li><span class="wrong">Still stuck with strings when referring to class or role names</span></li>
+    <li><span class="wrong">Coercion gotcha from earlier still applies to types exported from <code>MooseX::Types::Moose</code></span></li>
+  </ul>
+</div>
+
+<div class="slide">
+  <h1>Recommendation</h1>
 
   <ul>
-    <li>Coerce instead of a union like <code style="white-space: nowrap">Int | ArrayRef[Int]</code></li>
+    <li>Use <code>MooseX::Types</code></li>
+    <li>Compile time error catching and automatic namespacing are huge wins</li>
+    <li>Docs from <code>Moose::Util::TypeConstraints</code> are 98% compatible with <code>MooseX::Types</code> anyway</li>
+    <li>A function exported by a type library works wherever a type name would</li>
   </ul>
 </div>