Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Trouble with, or misunderstanding of, Types::Standard ArrayRef[elements] enforcement in Moo

by Your Mother (Archbishop)
on May 01, 2019 at 03:43 UTC ( [id://1233213]=note: print w/replies, xml ) Need Help??


in reply to Re: Trouble with, or misunderstanding of, Types::Standard ArrayRef[elements] enforcement in Moo
in thread Trouble with, or misunderstanding of, Types::Standard ArrayRef[elements] enforcement in Moo

Thanks. I ended up an around add_topping => …; but your suggestions are worth looking at I think and made me remember the types can be checked manually too. Thanks also for the great software.

Replies are listed 'Best First'.
Re^3: Trouble with, or misunderstanding of, Types::Standard ArrayRef[elements] enforcement in Moo
by tobyink (Canon) on May 01, 2019 at 10:49 UTC

    In the latest development releases of Types::Standard, you can do this:

    use Types::Standard is_InstanceOf => { of => "FishTaco::Topping", -as +=> "is_Topping" }; is_Topping( $something ); # returns a boolean

    If you've got Type::Tiny::XS installed, this will be an XS function, so pretty darn fast to call. I haven't done a lot of benchmarking on it, but I'd wager a lot faster than a blessed+isa check. If you don't, then it's still not going to be slow.

    (Older versions, you can do something like this for the same effect...

    BEGIN { require Types::Standard; *is_Topping = Types::Standard::InstanceOf->parameterize("FishTaco::T +opping")->compiled_check; };

    Newer versions just provide support for of in the import method.)

      Terrific. By manually I meant this more than isa/blessed

      my $type = InstanceOf['FishTaco::Topping']; … if ( $type->($topping) ) …

        $type->($value) is a live-or-die function rather than returning a boolean. It returns $value if it lives. Makes it easy to do something like:

        sub set_name { my $self = Object->( shift ); my $value = Str->( shift ); $self->{name} = $value; } sub get_name { my $self = Object->( shift ); return $self->{name}; }

        $type->check($value) returns a boolean.

        Exporting an is_Topping (boolean) or assert_Topping (live or die) function will be faster because it will avoid overhead from things like overloading, method lookups, etc. Though what you've already got is unlikely to be a major bottleneck for your application, so I'd mostly say go with what you feel is more readable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1233213]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found