in reply to Re: Bring back the smartmatch operator (but with sane semantics this time)!
in thread Bring back the smartmatch operator (but with sane semantics this time)!

The string/number heuristic is responsible for some of the oddest behaviour of smart match. Let's take for example the use of $x ~~ @array as an "in" operator.

sub as_bool { $_[0] ? "true" : "false" } my @greetings = qw( Hi Bye ); say as_bool("Hi" ~~ @greetings); # true say as_bool("Hiya" ~~ @greetings); # false

OK, everything is working as expected so far. Now let's do something a little more complex...

push @greetings, 0, 1; say as_bool(0 ~~ @greetings); # true say as_bool(1 ~~ @greetings); # true say as_bool(2 ~~ @greetings); # false say as_bool("Hiya" ~~ @greetings); # true ?!?!
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^3: Bring back the smartmatch operator (but with sane semantics this time)!
by Anonymous Monk on Jun 12, 2014 at 22:14 UTC

    Not having memorized the smartmatch table, that looked pretty strange to me at first, but looking at the table, at least it's well-defined... and when I tried it I got a "Argument "Hiya" isn't numeric in smart match" warning, which (although it's not much) is better than it silently matching. But the problem of when to match with == vs. eq remains...