Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created October 25, 2016 14:19
Show Gist options
  • Save zoffixznet/726f02a766e62af45910b1c1b7918935 to your computer and use it in GitHub Desktop.
Save zoffixznet/726f02a766e62af45910b1c1b7918935 to your computer and use it in GitHub Desktop.
(I describe int/Int, but same applies for other natives that have
a boxed Perl 6 equivalent):
0) Literal: treat as Int
1) Int:
1.1) Use as Int, if have candidate
1.2) Use as native, if:
1.2.1) have native candidate
1.2.2) size fits
2) Native:
2.1) Use as native, if have candidate
2.2) Use as Int, if have candidate
And so we have:
my int $i = 2; # used in all examples to indicate native int
multi foo (int $x) {say "native" };
foo 2; # native; 0 => 1 => 1.2
multi foo (Int $x) {say "other Int" };
foo 2; # other Int; 0 => 1 => 1.1
multi foo (int $x) {say "native" };
foo $i; # native; 2 => 2.1
multi foo (Int $x) {say "other Int" };
foo $i; # other Int; 2 => 2.2
multi foo (int $x) {say "native" };
multi foo (Int $x) {say "other Int" };
foo 2; # other Int; 0 => 1 => 1.1
foo $i; # native; 2 => 2.1
multi foo (int $x) {say "native" };
multi foo (Str $x) {say "other Str" };
foo 2; # native; 0 => 1 => 1.2
foo $i; # native; 2 => 2.1
multi foo (Int $x) {say "other Int" };
multi foo (Str $x) {say "other Str" };
foo 2; # other Int; 0 => 1 => 1.1
foo $i; # other Int; 2 => 2.2
multi foo (int $x) {say "native" };
multi foo (Int $x) {say "other Int" };
foo 2; # other Int; 0 => 1 => 1.1
foo 2**100; # other Int; 0 => 1 => 1.1
multi foo (int $x) {say "native" };
foo 2; # native; 0 => 1.2
foo 2**100; # X::Multi::NoMatch (no Int candidate);
# 0 => 1 => 1.2 => 1.2.2 (fails to match at this point)
multi foo (int $x, int $y) {say "native" };
multi foo (Int $x, Int $y) {say "other Int" };
foo 2, $i; # other Int; (0 => 1 => 1.1), (2 => 2.1)
foo 2, 2; # other Int; (0 => 1 => 1.1), (0 => 1 => 1.1)
foo $i, $i; # native; (2 => 2.1), (2 => 2.1)
multi foo (int $x, int $y) {say "native" };
multi foo (Int $x, int $y) {say "Int + native" };
multi foo (Int $x, Int $y) {say "other Int" };
foo 2, $i; # Int + native; (0 => 1 => 1.1), (2 => 2.1)
foo 2, 2; # other Int; (0 => 1 => 1.1), (0 => 1 => 1.1)
foo $i, $i; # native; (2 => 2.1), (2 => 2.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment