On Wednesday, November 20, 2002, at 12:11  PM, Larry Wall wrote:
On Wed, Nov 20, 2002 at 11:57:33AM -0800, Michael Lazzaro wrote:
: .... and _I'm_ trying to promote the reuse of the old "oct/hex"
: functions to do a similar both-way thing, such that:

What's a two-way function supposed to return if you pass it something
that has both a string and a numeric value? Convert it both directions?
Well, if hex() is a multimethod w.r.t. context:

In numeric context, it expects a string argument.
(Converting from a number is a noop)

my int $i = hex '10'; # 16
my int $i = hex 16; # noop, 16

In string context, it expects a numeric argument.
(Converting from a string is a noop.)

my str $s = hex 16; # '10';
my str $s = hex '10' # noop, '10';

.... so with duals, it should try for the 'expected' type. If necessary, you could always say "hex num $x" if you wanted to extract just the num part of a dual value, for example.

Of course, if you can't tell if you're in num or str context, it's completely screwed. So any multimeth like this only works if you're explicitly typing things, which is a strike against it.

MikeL



Reply via email to