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

It seems that is-prime and .is-prime handle 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 
+5
source share
1 answer

Here is the definition of the subroutine 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 isprime_I converts its argument to .Int . Everything that this method has can then return an integer, which can be prime.

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

+3
source

Source: https://habr.com/ru/post/1274307/


All Articles