Re: Design Team Issues: Numeric Types
[email protected] (Michael Lazzaro) Tue, 19 Nov 2002 14:38:49 -0800
| Newsgroups | perl.perl6.documentation |
|---|---|
| Message-ID | <[email protected]> |
On Monday, November 18, 2002, at 04:54 PM, Larry Wall wrote:
> : (B) Need to know the root of the numeric types
> :
> : Option 1:
> : numeric (mostly abstract base class)
> : - num
> : - int
> :
> : Option 2:
> :
> : num (floating point 'num' is the base class)
> : - int
>
> I'd lean towards option 1, insofar as low-level types can be considered
> objects at all. I suppose there might also be an abstract Numeric
> type...
.... IIR, Damian previously recommended option 2. Do you want to fight
it out with him, or overrule? :-)
If it isn't obvious to everyone else, the main (only?) reason to care
about this is when checking/specifying context/args. Assume num means
a double-precision float.
Simply put: (a) if you pass an <int> to a function defined as taking a
(floating-point) <num>, does the function always convert your <int> to
a <num> before doing anything with it, or does it know it doesn't have
to? And (b) are you allowed to discriminate between 'floating point'
and 'integer' context, and how?
Option 1:
sub foo(num $n; int $i) # _always_ forces $n to a double
# _always_ forces $i to an int
sub bar(numeric $n) # allows both int or double, doesn't care
if want numeric {
if want int { ... } # both are subclasses of numeric,
if want num { ... } # but only siblings to each other
}
Option 2:
sub foo(num $n; int $i) # allows $n as int or double
# $i always forced to an int
sub bar(num $n) # allows both int or double, doesn't care
if want numeric { # (maybe still want abstract base anyway)
if want num { # num is the base class
if want int { ... } # int is special case of a num
}
}
.... Nasty issue. #1 allows you to be more precise, important if
constantly converting nums <--> ints would notably slow things. But #2
more obviously does WYM most of the time.
Hmm, here might be an answer (thinking aloud). Perhaps <num> is the
base class, just like it is now (you can put both floats and integers
in it, it doesn't care). <int> is for explicit integer, <float> is for
explicit double.
num
- int
- float
Such that the literal 1.234 is born as type <float>, which is trivially
type <num>. Literal 1234 is treated as an <int>, same deal.
So we'd still use <num> and <int> for most things, but we'd have
<float> hanging out as a way to explicitly type/cast/contextify*:
sub foo(float $i)
.... in cases where it really mattered. Would that better solve the
issues in options 1 and 2(?)
MikeL
*(contextify? contextification?) :-)