in reply to Re: RFC: Inline::Blocks or inline as a keyword?
in thread RFC: Inline::Blocks or inline as a keyword?

Hi Toby

> I'm the author of Type::Tiny which internally does a lot of inlining. I mean, seriously a lot.

May I ask how you solved this?

> This is because Perl is able to optimize the do case to the plain case.

Are you sure?

DB<17> sub test { print do { 5+4 } } DB<18> print B::Deparse->new("-p")->coderef2text(\&test) { use feature 'current_sub', 'say' ... # yadda shortened print(do { # still here 9 }); }

update

this is the mentioned do-case

{ use warnings; use strict; my($total); foreach $_ (1 .. $end) { ($total += do { (1 / $_) }); } $total; }

DB<27> print $] 5.024001

hence the do-opcode is still present.

I suppose you meant to say that it has no performance penalty.(?)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^3: RFC: Inline::Blocks or inline as a keyword?
by tobyink (Canon) on Sep 09, 2018 at 20:04 UTC

    Each type constraint knows how to generate a string of Perl code for validating a value, given a variable name for the value.

    For example:

    $ perl -MDevel::Hide=Ref::Util::XS,Type::Tiny::XS -MTypes::Standard=-t +ypes -E'say HashRef->inline_check(q/$myvar/)' Devel::Hide hides Ref/Util/XS.pm, Type/Tiny/XS.pm (ref($myvar) eq 'HASH')

    So the ArrayRef[HashRef] type constraint can use that when generating a longer bit of code to validate an arrayref of hashrefs:

    $ perl -MDevel::Hide=Ref::Util::XS,Type::Tiny::XS -MTypes::Standard=-t +ypes -E'say ArrayRef->of(HashRef)->inline_check(q/$myvar/)' Devel::Hide hides Ref/Util/XS.pm, Type/Tiny/XS.pm do { (ref($myvar) eq 'ARRAY') and do { my $ok = 1; for my $i (@{$myvar +}) { ($ok = 0, last) unless (ref($i) eq 'HASH') }; $ok } }
      Are you saying you are dynamically generating code for your type checks which is eval'ed?

      Not sure if that's the meaning of "inlining" in this thread.

      Probably I'm missing your point...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        The ArrayRef and Int type constraints are smart enough to know how to combine to make an ArrayRef[Int] type check which inlines the Int check inside a loop over the array. This inline code is evaled within a sub { ... } to create a single coderef which checks that a value is an arrayref, and all elements in the arrayref are integers.

        That code can also be retrieved as a string for inlining into larger coderefs like constructors, etc.