On Fri, Jul 10, 2009 at 2:28 AM, Josh
Thompson<spam.goes.in.h...@gmail.com> wrote:
> troels knak-nielsen wrote:
>  - How do you know if it is a contract or the current object type hint?

The simplest solution would be to make one take precedence. You're not
likely to have both a class and a function with the same name, and if
you do, you kind of had it coming to you. For backwards compatibility
it would probably be most fitting to let the current behaviour
(class/interface) take precedence, although the other way around makes
for some quite interesting possibilities.

>  - It doesn't allow a type to be forced (casted) to the correct type.

As far as I can tell, this is a moot point. The whole discussion about
casting comes from the fact that hinting to a primitive type is more
restrictive than php's weak typing system. With a contract system this
problem doesn't exist. You could test that something conforms to your
specifications and then simply let the type system do its thing.

For example, instead of:

    function addFive(int $x) {
      return $x + 5;
    }

You would simply do:

    function addFive(is_numeric $x) {
      return $x + 5;
    }

Since $x is guaranteed to be numeric, it is safe to to arithmetic on
it. No reason to explicitly type cast here.

--
troels

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to