From: Dave Rolsky Date: Sun, 28 Jun 2009 21:17:08 +0000 (-0500) Subject: Finished (mostly?) the types section X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b59551bc92b3af33b02c0b3ffa4a2d0e1227be3d;p=gitmo%2Fmoose-presentations.git Finished (mostly?) the types section --- diff --git a/moose-class/slides/index.html b/moose-class/slides/index.html index c0b9aae..b11b1c6 100644 --- a/moose-class/slides/index.html +++ b/moose-class/slides/index.html @@ -2599,7 +2599,8 @@ undef

Value (and subtypes)

@@ -2641,12 +2642,14 @@ undef
-

Union Types

+

Type Union

@@ -2784,6 +2787,94 @@ has shouty_name => (
+

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  { [ $_ ] };
+ + +
+ + +
+

Using Types with Attributes

+ +
package Person;
+
+use Moose::Util::TypeConstraints;
+
+has height => (
+    is  => 'rw',
+    isa => 'Num',
+);
+
+has favorite_numbers => (
+    is     => 'rw',
+    isa    => 'ArrayRef[Int]',
+    coerce => 1,
+);
+
+ +
+

Typed Methods (Low-tech)

+ +
package Person;
+use MooseX::Params::Validate qw( validated_list );
+
+sub work {
+    my $self = shift;
+    my ( $tasks, $can_rest ) =
+        validated_list(
+            \@_,
+            tasks    =>
+                { isa    => 'ArrayRef[Task]',
+                  coerce =>1 },
+            can_rest =>
+                { isa     => 'Bool',
+                  default => 0 },
+        );
+
+    ...
+}
+ +
+

Typed Methods (High-tech)

+ +
package Person;
+
+use MooseX::Method::Signatures;
+
+method work ( ArrayRef[Task] :$tasks,
+                        Bool :$can_rest = 0 ) {
+    my $self = shift;
+
+    ...
+}
+ +

Digression: The Type Registry

    @@ -2804,7 +2895,7 @@ has shouty_name => (
-

Fix #1

+

Namespace Fix

  • Use some sort of pseudo-namespacing scheme
  • @@ -2841,37 +2932,81 @@ coerce 'MyApp::Type::ArrayOfInt', from 'Int', via { [ $_ ] };
-
-

Coercion Examples

+

Namespace Fix Pros and Cons

-
subtype 'My::DateTime',
-    as class_type 'DateTime';
+  
    +
  • Relatively simple
  • +
  • Already built into Moose
  • +
  • Conflates type and module namespaces
  • +
  • Type names are strings, so typos are easy to make and may be hard to find
  • +
+
-coerce 'My::DateTime', - from 'HashRef', - via { DateTime->new( %{$_} ) }; +
+

MooseX::Types

-coerce 'My::DateTime', - from 'Int', - via { DateTime->from_epoch( - epoch => $_ ) };
+
package MyApp::Types;
+
+use MooseX::Types
+    -declare => [ qw( ArrayOfInt ) ];
+use MooseX::Types::Moose
+    qw( ArrayRef Int );
+
+subtype ArrayOfInt,
+    as ArrayRef[Int];
+
+coerce ArrayOfInt
+    from Int,
+    via  { [ $_ ] };
+
+ +
+

MooseX::Types

+ +
package MyApp::Account;
+
+use MyApp::Types qw( ArrayOfInt );
+
+has transaction_history => (
+    is  => 'rw',
+    isa => ArrayOfInt,
+);
+ +
+

MooseX::Types

    -
  • Using a coercion to inflate a value
  • +
  • Type names are exported functions, catches typos early
  • +
  • Types must be pre-declared
  • +
  • Types are stored with namespaces internally, but externally are short
  • +
  • Import existing Moose types as functions from MooseX::Types::Moose
  • +
  • Still need string names for things like ArrayRef['Email::Address']
-

Coercion Examples

+

MooseX::Types Pros and Cons

-
coerce 'ArrayRef[Int]',
-    from 'Int',
-    via  { [ $_ ] };
+
    +
  • Catches typos at compile time
  • +
  • Automatic namespacing
  • +
  • One more thing to install and learn
  • +
  • Every name gets types twice (declared and then defined)
  • +
  • Still stuck with strings when referring to class or role names
  • +
  • Coercion gotcha from earlier still applies to types exported from MooseX::Types::Moose
  • +
+
+ +
+

Recommendation

    -
  • Coerce instead of a union like Int | ArrayRef[Int]
  • +
  • Use MooseX::Types
  • +
  • Compile time error catching and automatic namespacing are huge wins
  • +
  • Docs from Moose::Util::TypeConstraints are 98% compatible with MooseX::Types anyway
  • +
  • A function exported by a type library works wherever a type name would