Re: Why no type hints for built-in types?

From: Terje Slettebų (tslettebo_at_hotmail.com)
Date: 01/25/05


Date: Tue, 25 Jan 2005 20:27:03 +0100


"Chung Leong" <chernyshevsky@hotmail.com> wrote in message
news:__6dnbTwLZ1vCGncRVn-3g@comcast.com...
> "Terje Slettebų" <tslettebo@hotmail.com> wrote in message
> news:41f2d69b$1@news.broadpark.no...
> >
> > In PHP5, you can provide "type hints" for functions, like this:
> >
> > class Person {...}
> >
> > function f(Person $p)
> > {
> > ...
> > }
> >
> > Since this is optional static typing for objects, why not make the same
> > capability available for all types, built-in types included?
>
> One reason you can't do it is PHP will automatically promote an int to a
> float when it gets too large.

Ok, so we have the possibility of type change depending on the value. That
makes it a little trickier. Maybe an alternative to "int" and "float" hints
could be something like "number", accepting both. However, if you know your
algorithm won't require larger numbers than int, and that you require
absolute accuracy - no rounding - this might actually be useful, as you may
enforce that using "int" as type hint.

Of course, like in C++ and Java, one could still provide
promotions/conversions, so a function taking int, could be called using a
float, and vice versa, for example.

> I don't think you should put the burden of supplying the correct type on
the
> caller anyway. For fundamental types, an implementation should try its
best
> to coerce arguments to the correct type.

Are you speaking in the general sense, here, or "scripting languages" in
particular? There are very good reasons for strong type checking, in
general, and there may also be good reasons for not having it, but given
that some of the most popular languages (C/C++/Java, etc.) have strict
typing (to a varying degree, the newer ones being more strict than the older
ones), I don't think you'll find the majority of people agreeing with you on
this, in general.

The problem with "too eager" conversion - which also happens in C, even
though it may be a little more dangerous, and a little safer, than PHP,
anyway (more dangerous in that you may use invalid casts, getting nonsense
results - much of this has been fixed in C++, and more safe in that it does
have a kind of strong typing - you have to explicitly convert between
numbers and strings, for example).

In any case, as mentioned, promotions/conversions could still be used with
type hints, or overloading. After all, that's how it is in C++ and Java. To
take some examples:

void f(int i) {} // #1
void f(double f) {} // #2
void g(int i) {} // #3
void h(double f) {} // #4

int main()
{
  f(1); // Calls #1
  f(1.0); // Calls #2

  g(1); // Calls #3
  g(1.0); // Calls #3 (double->int conversion)

  h(1); // Calls #4 (int->double conversion)
  h(1.0); // Calls #4
}

As you can see, overloading and conversions can coexist quite happily. The
key is finding a function call that is the "best match". If there are none,
you get an ambiguity.

Regards,

Terje



Relevant Pages

  • Re: Why no type hints for built-in types?
    ... Maybe an alternative to "int" and "float" hints ... enforce that using "int" as type hint. ... general, and there may also be good reasons for not having it, but given ... typing (to a varying degree, the newer ones being more strict than the older ...
    (comp.lang.php)
  • Re: Maintance of c++ code
    ... piotr5@xxxxxxxxxxxxxxxxx (Piotr Sawuk) wrote: ... I too use comments to document the reasons why I made one choice ... int get ... Bar* b = new Bar; ...
    (comp.object)
  • Re: How about the future of VC++/MFC
    ... Whichever will get the job done with the least typing is the ... call GetDlgItem() once and be done with it. ... don't use completely silly reasons such as the amount of typing as ... power, and C# doesn't. ...
    (microsoft.public.vc.mfc)
  • Re: dynamic vs. static: the age-old debate
    ... int addx+y; ... orthogonal to static/dynamic/soft typing. ... $ dynamic-compiler program.source -o program ... will overflow, leaving the "overflow behavior is undefined" ...
    (comp.lang.misc)
  • Re: Sql injecting
    ... DECLARE @X INT ... Now there are two scalar types returned instead of sql ... For these reasons why MS decided to ... the ideas that the relational model is based on that is important. ...
    (comp.databases.ms-sqlserver)

Loading