DEV Community

Toby Inkster
Toby Inkster

Posted on • Originally published at toby.ink on

1

Type::Tiny 2.8.0 Released

What’s new?

  • The BoolLike type constraint accepts boolean.pm booleans.
  • Type::Params offers some improvements for DWIM named parameter processing.
  • More shortcuts are provided for exporting parameterized versions of type constraints.

Improvements to Type::Params

If your function or method takes named arguments, the list_to_named option allows these arguments to optionally be provided positionally as a shortcut:

  package My::Company;

  use v5.36;
  use Moo;
  use builtin qw( true false );
  use Types::Common qw( PositiveOrZeroNum InstanceOf );
  use Type::Params qw( signature_for );

  ...;

  signature_for pay_money => (
    method => true,
    named => [
      amount => PositiveOrZeroNum,
      employee => InstanceOf['Local::Person'],
    ],
    list_to_named => true,
  );

  sub pay_money ( $self, $arg ) {
    $self->payroll_account->withdraw( $arg->amount );
    $arg->employee->bank_account->deposit( $arg->amount );
    return $self;
  }

  ...;

  my $co = My::Company->new( ... );

  # Standard usage is named arguments:
  $co->pay_money( amount => 3000, employee => $alice );
  $co->pay_money( { amount => 3000, employee => $bob } );

  # Or provide them as positional arguments in the same order
  # they were declared in (amount then employee):
  $co->pay_money( 3000, $carol );

  # Or if the types are unambiguous, switch it up and provide
  # them in the wrong order instead. Still works!
  $co->pay_money( $dave, 3000 );

  # Or mix and match:
  $co->pay_money( $eve, amount => 3000 );
  $co->pay_money( $eve, { amount => 3000 } );
  $co->pay_money( 3000, employee => $eve );
  $co->pay_money( 3000, { employee => $eve } );
Enter fullscreen mode Exit fullscreen mode

Exporting Parameterized Types

For certain parameterizable types, there are now shortcuts to export parameterized versions of them.

For example, supposing you need to deal with numeric arrayrefs quite a lot. That is, arrayrefs containing only numbers. Previously, you’d probably do something like this:

  use Types::Common qw( Num ArrayRef );

  ...;

  has favourite_numbers => ( is => 'ro', isa => ArrayRef[Num] );

  ...;

  if ( ArrayRef->of( Num )->check( \@my_array ) ) {
    ...;
  }
Enter fullscreen mode Exit fullscreen mode

Now you can easily create a Nums type constraint and use it:

  use Types::Common qw( Num );
  use Types::Standard::ArrayRef Nums => { of => Num };

  ...;

  has favourite_numbers => ( is => 'ro', isa => Nums );

  ...;

  if ( is_Nums \@my_array ) {
    ...;
  }
Enter fullscreen mode Exit fullscreen mode

Not all parameterizable types support this, but many of the common ones do.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay