Rod Adams
Mon, 28 Mar 2005 12:07:31 -0800
Markus Laire wrote:
The real "fun" in determining what should happen with units comes to when you do operations that _change_ the units.Larry Wall wrote:
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 $Current = 5`Amps; my $Resistance = 10`Ohms; my $Power = $Current * $Resistance; # Do I get 50`Watts here?
my $theta = 45`Degrees; my $x = cos($theta); # no units on $x my $theta2 = acos($x); # in radians? or does $x carry a # "used to be Degrees" property?
my $distance1 = 100`Meters; my $distance2 = 0.25`Kilometers; my $timeinterval = 5'Seconds; my $velocity1 = $distance1 / $timeinterval; my $velocity2 = $distance2 / $timeinterval; my $acceleration = ($velocity2-$velocity1)/$timeinterval; # is $acceleration something like 30`Meters/Second/Second ?