Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have attempted to post the following reply to perl5-porters, but it doesn't seem to have appeared. (Spam trap? Moderation? I don't know. If it hasn't gone through in a few hours, I'll try responding again.) It's a response to the type constraint section.

This is all pretty over-engineered. It can be simplified to something that would work well with existing type implementations such as MooseX::Types, MouseX::Types, Type::Tiny, and Specio. First, make this: sub f ($x is Int) { ...; } effectively a shorthand for: sub f { my ($x) = @_; Int->check($x) or Carp::croak(Int->get_message($x)); } Second, define these: use Scalar::Util; sub UNIVERSAL::check { my ($class, $object) = @_; blessed($object) && !blessed($class) && $object->DOES($class); } sub UNIVERSAL::get_message { my ($class, $value) = @_; "$value is not $class"; } Third, nothing. There is no third. Once you've got those two things working, then the following will "just work": use MooseX::Types::Common qw(PositiveInt); sub add_counts ($x is PositiveInt, $y is PositiveInt) { return $x + $y; } This works because PositiveInt is just a sub that returns an object with `check` and `get_message` methods. And this will also "just work" (thanks to UNIVERSAL): sub fetch_page ($ua is HTTP::Tiny, $url) { my $response = $ua->get($url); $response->{success} or die $response->{message}; return $response->{content}; } Yeah, `check` and `get_message` are pretty generic-sounding methods to be adding to UNIVERSAL and there are arguments in favour of, say, `CHECK` and `GET_MESSAGE`. The advantage of `check` and `get_message` is that the following already works out of the box in MooseX::Types, MouseX::Types, and Type::Tiny, and can be made to work with Specio with a pretty small shim. Int->check($x) or Carp::croak(Int->get_message($x)); Shim for Specio is: sub Int () { return t("Int") } > Also, the nested HashRef[ArrayRef[Int]] form quickly becomes a > performance nightmare, with every element of the AoH having to > be checked for Int-ness on every call to the function. It's not as bad for performance as you might think. MouseX::Types and Type::Tiny are capable of checking HashRef[ArrayRef[Int]] with a single XS sub call. MooseX::Types and Specio will check it without XS, but it's still a single sub call, just with a lot of loops and regexp checks. That said, there are performance improvements that can be made. One would be at compile time, when Perl sees: sub f ($x is Int) { ...; } It would call: $code = eval { Int->inline_check('$x') }; The Int object would return a string of Perl code like: q{ defined($x) && !ref($x) && $x =~ /^-?[0-9]+$/ } And this would be inlined into the function like: sub f { my ($x) = @_; do { defined($x) && !ref($x) && $x =~ /^-?[0-9]+$/ } or Carp::croak(Int->get_message($x)); } (Note I'm using the block form of eval when Perl fetches the inline code, so Perl isn't evaluating the string of code at run time. It just allows Int to throw an exception if it's unable to inline the check. Some checks are hard or impossible to inline.) Once again, there's discussion to be had about the name of the method `inline_check`, but Type::Tiny and Specio already offer an `inline_check` method exactly like this. And Moose offers `_inline_check`. Mouse offers neither, so Perl would just fall back to doing Int->check($x) to check the value. Coercions are a whole different kettle of fish and how they interact with aliasing and read only parameters can get confusing. For this reason, I'd recommend simply leaving them out of signatures, at least while people get used to having type constraints in signatures. People can coerce stuff manually in the body of their sub. This is not hard, it's probably what they're doing already, and it's almost certainly more readable than any syntax you can squeeze into the signature.

In reply to Re: Jumbo Signatures extensions discussion by tobyink
in thread Jumbo Signatures extensions discussion by Corion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found