On 04/30/2018 06:20 AM, Elizabeth Mattijsen wrote:
On 30 Apr 2018, at 15:04, ToddAndMargo <toddandma...@zoho.com> wrote:
Hi All,
I am confused.
With this sub:
sub odd( $Num ) { return $Num % 2; }
Why does this work:
if odd $LineNum
{ $PartsStr ~= '<font color="#006600">'; } # Green
else { $PartsStr ~= '<font color="#663366">'; } # Purple
And this one throw an error:
if $LineNum.odd
{ $PartsStr ~= '<font color="#006600">'; } # Green
else { $PartsStr ~= '<font color="#663366">'; } # PurpleNo
No such method 'odd' for invocant of type 'Int'. Did
you mean 'ord'?
What is my misunderstanding?
I think you’re confusing Perl 5 with Perl 6. In Perl 5, a method is just a sub
that happens to accept the object as the first parameter.
Although this is also true on a much lower level in Perl 6, at this level this
is *not* true.
So, this is *not* a compilation error:
class Foo {
sub bar {}
method bar {}
}
So the error is correct: there *is* not method “odd” in the class Int. There’s
only a sub called “odd” in your package.
Now, if you wanted to, you *could* mix in a “odd” method into an Int, but that
is cumbersome because it needs to be done to each value you want to work with.
You can also use the subset approach for ad-hoc type checking:
subset Even of Int where * %% 2;
say 42 ~~ Even; # True
say 43 ~~ Even; # False
Use the power :-)
Thank you!
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~