Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some difference between .is-prime and is-prime() in Perl 6?

Tags:

raku

It seems is-prime and .is-prime treat their arguments differently:

> is-prime('11')
True
> '11'.is-prime
No such method 'is-prime' for invocant of type 'Str'
  in block <unit> at <unknown file> line 1
> is-prime(2.5)
False
> (2.5).is-prime
No such method 'is-prime' for invocant of type 'Rat'
  in block <unit> at <unknown file> line 1
like image 971
Eugene Barsky Avatar asked Oct 18 '22 01:10

Eugene Barsky


1 Answers

Here's the routine definition from the Int class

proto sub is-prime($) is pure  {*}
multi sub is-prime(Int:D \i) {
    nqp::p6bool(nqp::isprime_I(nqp::decont(i), nqp::unbox_i(100)));
}
multi sub is-prime(\i) {
    i == i.floor
     && nqp::p6bool(nqp::isprime_I(nqp::decont(i.Int), nqp::unbox_i(100)));
}

In the second multi the isprime_I converts its argument with .Int. Anything that has that method can then return an integer that might be prime.

This unbalance one of the things I don't like about Perl 6. If we have a routine that can do it this way we should move the method higher up in the class structure.

like image 150
brian d foy Avatar answered Oct 21 '22 09:10

brian d foy