p5pclub / ref-util

Ref::Util - Utility functions for checking references
6 stars 11 forks source link

[RFE] Add passthru functions akin to Params::Util #44

Open robrwo opened 6 years ago

robrwo commented 6 years ago

Params::Util has "passthrough" functions that return the reference or undef instead of a boolean. This is useful.

I wouldn't recommend changing the existing API, since that could break code that expects booleans.

Maybe add simple functions like arrayref or hashref etc:

sub arrayref {
  is_arrayref( $_[0] ) ? $_[0] : undef;
}

They could be grouped in a :passthrough export group.

xsawyerx commented 6 years ago

What's the value of returning an undef?

robrwo commented 6 years ago

Useful for the coercion functions in #43:

$x = arrayref($y) // [$y]

or basically, any instance where you want the reference, or an undefined value that you can use for something else, e.g.

$x = arrayref($y) || $default;
tobyink commented 6 years ago

I think if_arrayref might be a nice naming convention. Makes it pretty clear what it's doing.

my $y = if_arrayref($x) // [];
xsawyerx commented 6 years ago

I'm generally against returning explicit undef. I think your focus is the other side of it: returning the arrayref back so you could use it. That might be a possible change, but then the naming would be confusing:

return is_arrayref($x) || [$y];