in reply to Re^8: rough approximation to pattern matching using local (Multi Subs)
in thread rough approximation to pattern matching using local

Quick reminder for any visitors reading this:

»»» This post is about the immature Perl 6, not the rock solid Perl 5 «««

If Perl6 is to retain scalars; but people write their modules using Ints & Strs etc. for efficiency; then it either forces their users to also use Ints & Strs etc. or multi-subs will have to use runtime resolution.

No. (Or, rather, the user doesn't need to know they're using Ints, Strs, etc.)

multi sub a ( Int $, Int $ ) {} my ($foo, $bar) = 1, 2; say $foo.VAR; says 'Any' say $foo.WHAT; # says '(Int)' a($foo, $bar); # call is resolved at compile time

No type is declared for the container $foo. So it's `of` type defaults to Any. This serves as a constraint on what can be assigned to $foo -- anything that is of type Any or a subtype (anything but Junction or Mu).

The type of the contained value is distinct. In this case the compiler infers that it's an Int. An Int is a subtype of Any so the assignment is OK.

Both of these types are known at compile time.

The type info available at compile-time includes:

Even if the coder calls multisubs without adding type info to their args the call will still be resolved at compile-time if there's enough type info available at compile-time.

  • Comment on Re^9: rough approximation to pattern matching using local (Multi Subs)
  • Download Code

Replies are listed 'Best First'.
Re^10: rough approximation to pattern matching using local (Multi Subs)
by BrowserUk (Patriarch) on Feb 09, 2015 at 05:47 UTC

    Warning: Pseudo-P6 :)

    multi sub a ( Int $, Int $ ) {} multi sub a ( Int $, Str $ ) {} multi sub a ( Str $, Int $ ) {} multi sub a ( Str $, Str $ ) {} my( $x, $y ) = <>.split' '; a( $x, $y ); ## How do you determine which multi-sub to invoke at comp +ile time?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      If the '<>' is file input then the compiler obviously can't determine types (and thus have a chance of figuring out which sub to call) until run-time. This would be true for any language and compiler so I'm guessing you meant something else.

      If the '<>' is quote words; and Rakudo implemented the `val` feature from the spec; and it was applied (either implicitly or explicitly) to the quote words after splitting; and if at least one of the values was recognized as an Int, and the other as either an Int or a Str; then the result would be compile-time resolution to the more fitting candidate.

      (Read the first paragraph of Literals and maybe the :val modifier etc.)

        If the '<>' is file input then the compiler obviously can't determine types ... so I'm guessing you meant something else.

        No. You had it right the first time. It stands for file input; but could equally be any other source of untyped data: a database; a web form; and IPC from another program.

        More importantly, in "Perl" and other dynamic languages, it could simply be a key/value pair from a hash: while( my( $key, $value ) = each %hash ) { a( $key, $value ); }.

        In an early version of my current project, I wanted to do two-way lookup of pairs of values: lookup( name ) -> integer & lookup( integer ) -> name; and the cheapest, most efficient solution was to store each integer/name pairing both ways in a single hash.

        This would be true for any language and compiler

        Well no. Most languages supporting pattern matching subs force you to type inputs at source.

        But in Perl, (Python, Ruby, ...), we're not hamstrung by a static typing system; and it is perfectly natural and normal to have scalars, and whole data structures of scalars that contain a mix of integers, reals and strings, and use them as strings or integers or reals willy-nilly and have the language DWIM.

        So once again I say; Perl6 will either: a) fail if we pass generic scalars to library multi-subs defined in terms of specific types; or it b) will need runtime dispatching to DWIM.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked