Re: Why no type hints for built-in types?
From: Terje Slettebų (tslettebo_at_hotmail.com)
Date: 01/25/05
- Next message: Terje Slettebų: "Re: Why no overloading in PHP5?"
- Previous message: Ivan Peevski: "Re: image gallery"
- In reply to: Chung Leong: "Re: Why no type hints for built-in types?"
- Next in thread: Terje Slettebų: "Re: Why no type hints for built-in types?"
- Reply: Terje Slettebų: "Re: Why no type hints for built-in types?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Terje Slettebų: "Re: Why no overloading in PHP5?"
- Previous message: Ivan Peevski: "Re: image gallery"
- In reply to: Chung Leong: "Re: Why no type hints for built-in types?"
- Next in thread: Terje Slettebų: "Re: Why no type hints for built-in types?"
- Reply: Terje Slettebų: "Re: Why no type hints for built-in types?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|