Larry Wall wrote:

> ... SKIP ...

Okay, that looks scary, but if as in my previous message we define
"chars" as the highest Unicode level allowed by the context and the
string, then we can just write that in some notation resembling:

    substr($a, 5`Chars, 10`Chars);

or whatever notation we end up with for labeling units on numbers.
Even if we don't define "chars" that way, they just end up labeled with
the current level (here assuming "Codes"):

    substr($a, 5`Codes, 10`Codes);

or whatever.

But this is all implicit, which is why you can just write

    substr($a, 5, 10);

and have it DWYM.

Now, I admit that I've handwaved the tricksy bit, which is, "How do
you know, Larry, that substr() wants 5`Codes rather than 5`Meters?
It's all very well if you have a single predeclared subroutine and
can look at the signature at compile time, but you wrote those as multi
methods up above, so we don't know the signature at compile time."

Well, that's correct, we don't know it at compile time.  But what
*do* we know?  We know we have a number, and that it was generated
in a context where, if you use it like a string position, it should
turn into a number of code points, and if you use it like a weight,
it should turn into a number of kilograms (or pounds, if you're NASA).

In other words, the effective type of that literal 5 is not "Int",
but "Int|Codes|Meters|Kilograms|Seconds|Bogomips" or some such.
And if MMD can handle that in an argument type and match up Codes as
a subtype of Ptr, and if we write our method signature only in the
abstract types like Ptr, we're pretty much home free.  That certainly
simplifies how you write S29, though I don't know if the MMD folks
will be terribly happy with the notion of dispatching arguments with
junctional types.  But it does fall out of the design rather naturally.

> ...

So do you actually envision perl6 to allow a junction of units on numbers? This would have huge implications, depending on what exactly is possible with these units...

Would/could some of these DWIM in perl6?

    # import proper MMD-subs for + - * etc...
    use MyUnitConversions;

    my $length = 1.3`Meters + 4.6`Yards;
    my $weight = 4`Pounds - 1'Kilograms;
    my $money = 12`€ + 5.78`£ + 12`US$;

Then, how would I specify the unit of the result?

    my $unit = 1`Minutes|Kilograms|Meters;

    my $time_mins`Minutes = $unit * 5.45; # 5.45 Minutes
    my $time_secs`Seconds = $unit * 5.45; # 327 Seconds

    my $weight`Weight = $unit * 2.34;
    my $length`Length = $unit * 12.56;

But these would need MMD based on return-type which is out IIRC?
So perhaps just simple:

    my $time_mins = unit_conversion($unit * 5.45, Minutes);
    my $time_secs = unit_conversion($unit * 5.45, Seconds);

Or using some infix-op:

    my $time_mins = ($unit * 5.45) ` Minutes;
    my $time_secs = ($unit * 5.45) ` Seconds;

And what about hex/dec/oct/bin? Could these be included in this system?

    my $number`Dec = 12`Oct + 010101`Bin + 0cab`Hex;

And then we of course definitely need

    my $length = 12`Oct&Miles + 0c`Hex&Kilometers;    :)

Not sure if I'm on right track at all - but Units on numbers with MMD gives some really nice ideas to a mathematically minded person...

    my $speed_a = 78`Kilometers / 2`Hour;
    my $speed_b = 50`(Miles/Hour);  # or 50`Miles_per_Hour
    my $delta = $speed_a - $speed_b;

-- Markus Laire <Jam. 1:5-6>

Reply via email to