- The initializer method receives an alias to the pad entry of $x in $_[1]
- and the original type name ("Foo") in $_[2]. You can either edit $_[1]
- in place, in which case you should return an empty list, or return a new
- scalar that will be copied into $x.
+ use Lexical::Types as => sub {
+ return $_[0] =~ /Str/ ? @_ : ()
+ };
+ my Str $y; # calls Str->TYPEDSCALAR
+ my Int $x; # nothing special
+
+ * or the desired package and method name, in that order (if any of
+ those is "undef", the default value will be used instead).
+
+ use Lexical::Types as => sub { 'My', 'new_' . lc($_[0]) };
+ my Str $x; # the coderef indicates to call My->new_str
+
+ Note that if the type is a constant, $_[0] will be set to the
+ *value* of constant and not to its name.
+
+ use Lexical::Types as => sub { $_[0] => 'new' };
+ use constant Str => 'MyStr';
+ my Str $x; # calls MyStr->new
+
+ This means in particular that you can't both use constant types and
+ redirect several types to different methods of the same package,
+ because then you can't distinguish between the original types with
+ $_[0].