develooper Front page | perl.perl6.language | Postings from July 2006

Run time dispatch on ~~

Thread Next
Aaron Sherman
July 13, 2006 12:45
Run time dispatch on ~~
Message ID:
I'm told that I did a terrible job of making my point in the === thread,
and nothingmuch asked me on IRC to re-state my concerns. I'll do so
briefly, and then give examples. Please do have a look at the examples,
just in case I'm not clear.


~~ is great. It matches on all kinds of useful right-hand-sides like
regexes, strings, methods, your neighbor's kids, you-name-it.

The only problem that I have is ~~ matching against an unknown type
(Any~~Any). In this case, the programmer cannot know what it is that
they're getting themselves into, and for the life of me, I can't see why
Perl would do anything other than an only slightly smart comparison
there, since that's obviously what the programmer had in mind.

However, S03 specs that ~~ will do a run-time dispatch based on the type
of that value at the time. Matching Arrays (@~~@) is just a
hyperoperated case of the same problem, except for the case where the
array on the right-hand-side can be typed at compile-time or is a list
that can be coerced painlessly into such a typed array. Then there's no
dispatch at run-time.


Ok, so first let me re-propose my solution: Any~~Any and @~~@ are
compile-time dispatched to =~= and >>=~=<< respectively (no, I'm not
married to the name, and perhaps eqv is better for this, I don't know),
which has some smarts about comparing values in meaningful ways, and
which programmers know to override if they really need special matching
semantics, but basically is just a very slightly smarter ===. Regexes
are compared, not executed. Code is compared, not executed.


Now, let's look at some of the good that ~~ does for us:

	$a ~~ "Some string" # sameness
	$a ~~ 5             # sameness
        $a ~~ ->{...}       # test
        $a ~~ /.../         # regex matching

That's great, and we don't want to mess with any of it.

But, then we have:

	$a ~~ $b            # uh... something

We can't even say what it does, much less why it's useful. It does
"match" whatever that is. Sure, it's great for implementing your own ~~,
but that's about it.

The even worse:

	@a ~~ @b            # uh... lots of something

looks like array comparison to the casually misinformed user, but is
actually a run-time, hyperized vector dispatch...

Now, I do think people will want to:

	@a ~~ ( /^\d+$/, /^\w+$/, /^\s+$/ )

but we can coerce that into a typed array at compile time, so there's no

Going with my suggestion would mean that ~~ is still very powerful, just
not opaque. You would have to ask for it to activate a particular
superpower by giving it a right-hand-side that has a type, or expect a
fairly mundane comparison to be performed.

The _security_ implications will likely get sorted out, but I still
forsee some major end-user bogglage when $a~~$b is true, even though
they have no relationship to each other that was obvious to the
programmer who wrote the statement.

Keep in mind that if you have an Any and you want to match against it
smartly, you can always request your poison:

	$a ~~ ($b as Regex)

Aaron Sherman <>
Senior Systems Engineer and Toolsmith
"We had some good machines, but they don't work no more." -Shriekback

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About