X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FFunction%2FParameters.pm;h=a39f93bbbf273190ef46388f4c29f59062e13258;hb=42e595b0d7841bfda100f6af7115ea41cc46b818;hp=51679df1e661f38c04fd8ec8ad6f8359c664c323;hpb=ff265988561375d3cf480004e29e3891094c0afb;p=p5sagit%2FFunction-Parameters.git diff --git a/lib/Function/Parameters.pm b/lib/Function/Parameters.pm index 51679df..a39f93b 100644 --- a/lib/Function/Parameters.pm +++ b/lib/Function/Parameters.pm @@ -64,43 +64,30 @@ sub _delete_default { my @bare_arms = qw(function method); my %type_map = ( - function => { - name => 'optional', - default_arguments => 1, - check_argument_count => 0, - named_parameters => 1, - types => 1, - reify_type => \&_reify_type_default, + function => {}, # all default settings + function_strict => { + defaults => 'function', + strict => 1, }, - method => { - name => 'optional', - default_arguments => 1, - check_argument_count => 0, - named_parameters => 1, - types => 1, - reify_type => \&_reify_type_default, - attrs => ':method', - shift => '$self', - invocant => 1, + method => { + defaults => 'function', + attributes => ':method', + shift => '$self', + invocant => 1, }, - classmethod => { - name => 'optional', - default_arguments => 1, - check_argument_count => 0, - named_parameters => 1, - types => 1, - reify_type => \&_reify_type_default, - attributes => ':method', - shift => '$class', - invocant => 1, + method_strict => { + defaults => 'method', + strict => 1, + }, + classmethod => { + defaults => 'method', + shift => '$class', + }, + classmethod_strict => { + defaults => 'classmethod', + strict => 1, }, ); -for my $k (keys %type_map) { - $type_map{$k . '_strict'} = { - %{$type_map{$k}}, - check_argument_count => 1, - }; -} our @type_reifiers = \&_reify_type_default; @@ -134,31 +121,36 @@ sub import { my ($name, $proto_type) = @$item; _assert_valid_identifier $name; - unless (ref $proto_type) { - # use '||' instead of 'or' to preserve $proto_type in the error message - $proto_type = $type_map{$proto_type} - || confess qq["$proto_type" doesn't look like a valid type (one of ${\join ', ', sort keys %type_map})]; - } + $proto_type = {defaults => $proto_type} unless ref $proto_type; my %type = %$proto_type; + while (my $defaults = delete $type{defaults}) { + my $base = $type_map{$defaults} + or confess qq["$defaults" doesn't look like a valid type (one of ${\join ', ', sort keys %type_map})]; + %type = (%$base, %type); + } + my %clean; - $clean{name} = delete $type{name} || 'optional'; + $clean{name} = delete $type{name} // 'optional'; $clean{name} =~ /^(?:optional|required|prohibited)\z/ or confess qq["$clean{name}" doesn't look like a valid name attribute (one of optional, required, prohibited)]; - $clean{shift} = delete $type{shift} || ''; + $clean{shift} = delete $type{shift} // ''; _assert_valid_identifier $clean{shift}, 1 if $clean{shift}; - $clean{attrs} = join ' ', map delete $type{$_} || (), qw(attributes attrs); + $clean{attrs} = join ' ', map delete $type{$_} // (), qw(attributes attrs); _assert_valid_attributes $clean{attrs} if $clean{attrs}; $clean{default_arguments} = _delete_default \%type, 'default_arguments', 1; $clean{named_parameters} = _delete_default \%type, 'named_parameters', 1; $clean{types} = _delete_default \%type, 'types', 1; - $clean{check_argument_count} = _delete_default \%type, 'check_argument_count', 0; $clean{invocant} = _delete_default \%type, 'invocant', 0; + $clean{runtime} = _delete_default \%type, 'runtime', 0; + $clean{check_argument_count} = _delete_default \%type, 'check_argument_count', 0; + $clean{check_argument_types} = _delete_default \%type, 'check_argument_types', 0; + $clean{check_argument_count} = $clean{check_argument_types} = 1 if delete $type{strict}; if (my $rt = delete $type{reify_type}) { ref $rt eq 'CODE' or confess qq{"$rt" doesn't look like a type reifier}; @@ -191,11 +183,13 @@ sub import { $type->{name} eq 'required' ? FLAG_NAME_OK : FLAG_ANON_OK | FLAG_NAME_OK ; - $flags |= FLAG_DEFAULT_ARGS if $type->{default_arguments}; - $flags |= FLAG_CHECK_NARGS | FLAG_CHECK_TARGS if $type->{check_argument_count}; - $flags |= FLAG_INVOCANT if $type->{invocant}; - $flags |= FLAG_NAMED_PARAMS if $type->{named_parameters}; - $flags |= FLAG_TYPES_OK if $type->{types}; + $flags |= FLAG_DEFAULT_ARGS if $type->{default_arguments}; + $flags |= FLAG_CHECK_NARGS if $type->{check_argument_count}; + $flags |= FLAG_CHECK_TARGS if $type->{check_argument_types}; + $flags |= FLAG_INVOCANT if $type->{invocant}; + $flags |= FLAG_NAMED_PARAMS if $type->{named_parameters}; + $flags |= FLAG_TYPES_OK if $type->{types}; + $flags |= FLAG_RUNTIME if $type->{runtime}; $^H{HINTK_FLAGS_ . $kw} = $flags; $^H{HINTK_SHIFT_ . $kw} = $type->{shift}; $^H{HINTK_ATTRS_ . $kw} = $type->{attrs}; @@ -273,9 +267,9 @@ sub info { my $info = $metadata{$key} or return undef; require Function::Parameters::Info; Function::Parameters::Info->new( - keyword => $info->{declarator}, + keyword => $info->{declarator}, invocant => _mkparam1($info->{invocant}), - slurpy => _mkparam1($info->{slurpy}), + slurpy => _mkparam1($info->{slurpy}), (map +("_$_" => _mkparams @{$info->{$_}}), glob '{positional,named}_{required,optional}') ) } @@ -397,11 +391,12 @@ This is just a normal block of statements, as with L|perlsub>. No surpris =head3 Name If present, it specifies the name of the function being defined. As with -L|perlsub>, if a name is present, the whole declaration is syntactically -a statement and its effects are performed at compile time (i.e. at runtime you -can call functions whose definitions only occur later in the file). If no name -is present, the declaration is an expression that evaluates to a reference to -the function in question. No surprises here either. +L|perlsub>, if a name is present, by default the whole declaration is +syntactically a statement and its effects are performed at compile time (i.e. +at runtime you can call functions whose definitions only occur later in the +file - but see the C flag below). If no name is present, the +declaration is an expression that evaluates to a reference to the function in +question. =head3 Attributes @@ -606,12 +601,49 @@ a reference to a hash with the following keys: =over +=item C + +Valid values: One of the predefined types C, C, +C, C, C, C. +This will set the defaults for all other keys from the specified type, which is +useful if you only want to override some properties: + + use Function::Parameters { defmethod => { defaults => 'method', shift => '$this' } }; + +This example defines a keyword called C that works like the standard +C keyword, but the implicit object variable is called C<$this> instead +of C<$self>. + +Using the string types directly is equivalent to C with no further +customization: + + use Function::Parameters { + foo => 'function', # like: foo => { defaults => 'function' }, + bar => 'function_strict', # like: bar => { defaults => 'function_strict' }, + baz => 'method_strict', # like: baz => { defaults => 'method_strict' }, + }; + =item C Valid values: C (default), C (all functions defined with this keyword must have a name), and C (functions defined with this keyword must be anonymous). +=item C + +Valid values: booleans. If enabled, this keyword takes effect at runtime, not +compile time: + + use Function::Parameters { fun => { defaults => 'function_strict', runtime => 1 } }; + say defined &foo ? "defined" : "not defined"; # not defined + fun foo() {} + say defined &foo ? "defined" : "not defined"; # defined + +C<&foo> is only defined after C has been reached at runtime. + +B A future version of this module may enable C<< runtime => 1 >> by +default for methods. + =item C Valid values: strings that look like scalar variables. This lets you specify a @@ -646,8 +678,16 @@ automatically check that they have been passed all required arguments and no excess arguments. If this check fails, an exception will by thrown via L|Carp>. -Currently this flag is overloaded to also enable type checks (see -L below). +=item C + +Valid values: booleans. If turned on, functions defined with this keyword will +automatically check that the arguments they are passed pass the declared type +constraints (if any). See L below. + +=item C + +Valid values: booleans. This turns on both C and +C. =item C @@ -668,10 +708,11 @@ The default type reifier is equivalent to: The predefined type C is equivalent to: { - name => 'optional', - invocant => 0, - default_arguments => 1, - check_argument_count => 0, + name => 'optional', + default_arguments => 1, + strict => 0, + invocant => 0, + runtime => 0, } These are all default values, so C is also equivalent to C<{}>. @@ -679,29 +720,24 @@ These are all default values, so C is also equivalent to C<{}>. C is equivalent to: { - name => 'optional', - shift => '$self', - invocant => 1, - attributes => ':method', - default_arguments => 1, - check_argument_count => 0, + defaults => 'function', + attributes => ':method', + shift => '$self', + invocant => 1, + # runtime => 1, ## possibly in a future version of this module } C is equivalent to: { - name => 'optional', - shift => '$class', - invocant => 1, - attributes => ':method', - default_arguments => 1, - check_argument_count => 0, + defaults => 'method', + shift => '$class', } C, C, and C are like C, C, and -C, respectively, but with C<< check_argument_count => 1 >>. +C, respectively, but with C<< strict => 1 >>. =back @@ -746,7 +782,7 @@ types. That is, before each parameter you can put a type specification consisting of identifiers (C), unions (C<... | ...>), and parametric types (C<...[...]>). Example: - fun foo(Int $n, ArrayRef[String | CodeRef] $cb) { ... } + fun foo(Int $n, ArrayRef[Str | CodeRef] $cb) { ... } If you do this, the type reification function corresponding to the keyword will be called to turn the type (a string) into a constraint object. The default @@ -796,6 +832,12 @@ generated code corresponds to: # ... turns into ... sub bar :method { my $self = shift; my ($x, $y, @z) = @_; sub bar; ... } +=head1 BUGS AND INCOMPATIBILITIES + +A future version of this module may enable C<< runtime => 1 >> by default for +methods. If this would break your code, please send me a note or file a bug on +RT. + =head1 SUPPORT AND DOCUMENTATION After installing, you can find documentation for this module with the