DEV Community

kobaken
kobaken

Posted on

3 1

Released types for subroutines

It is useful to have a subroutine type, so I made one.

use Types::Sub -types;
use Types::Standard -types;

my $Sub = Sub[
    args    => [Int, Int],
    returns => Int,
];

warn $Sub->validate(sub {})
# =>
# Reference sub { "DUMMY" } did not pass type constraint "Sub[sub(Int, Int) => Int]"
#    Reason : invalid parameters: invalid args length. got: 0, expected: 2
#    Expected : sub(Int, Int) => Int
#    Got      : sub(*) => *
Enter fullscreen mode Exit fullscreen mode

If you can find the meta-information from the subroutine, the test will pass as follows.

use Types::Sub -types;
use Types::Standard -types;

my $Sub = Sub[
    args => [Int, Int],
];

use Function::Parameters; 
fun add(Int $a, Int $b) { return $a + $b }
ok $Sub->check(\&add);

use Sub::WrapInType qw(wrap_sub);
ok $Sub->check(wrap_sub([Int,Int] => Int, sub {}));

use Sub::WrapInType::Attribute;
sub add2 :WrapSub([Int,Int] => Int) {
    my ($a, $b) = @_;
    return $a + $b
}
ok $Sub->check(\&add2);

sub add3 {}
my $meta = Sub::Meta->new(
    args    => [Int, Int],
    returns => Int,
);
Sub::Meta::Library->register(\&add3, $meta);
ok $Sub->check(\&add3);
Enter fullscreen mode Exit fullscreen mode

Internally, Sub[...] coerces subroutines into Sub::Meta and compares Sub::Meta against each other.

Give it a try!
https://metacpan.org/release/KFLY/Sub-Meta-0.14/view/lib/Types/Sub.pm

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay