Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:44900 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98475 invoked from network); 10 Jul 2009 11:20:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Jul 2009 11:20:18 -0000 Authentication-Results: pb1.pair.com smtp.mail=lewiswright@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=lewiswright@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.220 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: lewiswright@gmail.com X-Host-Fingerprint: 209.85.220.220 mail-fx0-f220.google.com Received: from [209.85.220.220] ([209.85.220.220:54450] helo=mail-fx0-f220.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 45/75-23850-1F3275A4 for ; Fri, 10 Jul 2009 07:20:18 -0400 Received: by fxm20 with SMTP id 20so756249fxm.23 for ; Fri, 10 Jul 2009 04:20:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=+3J01pQfgjq3kWVqhSjakifxwdMXUqAYQ0Z0AeIsxYw=; b=UY/CBKcqk14KnnfVVBVCyIdmsG3759DJYX1h/GSU/A07LINDGchj28h5Y1sse97c4i rzdWVJp9lKWnC79ce0o+xK8OXpiOvMb0HAGHwPpIzeSGzQ3ZNnenN4b3e8SAg9UUAkVR E3nyWdW5mHLVzXScxy0iF8w7QZnlCaQQLO71U= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=FKC6+HJXoaOwls21UNYSp31OxSQVHm2UPeZpsxqIyO+SsSCzVMm8QEwL7/65ugO0rT gMNQwrJF38tFE4ShegwFqACGYRzoLQM72nXp2AQkiNUOmedRGPo3PXRJTjmAh2kYDJt+ Ss3AQr7GUOU8USNyK6llnI9mvqYls21c8wtm0= MIME-Version: 1.0 Received: by 10.103.239.10 with SMTP id q10mr1033312mur.36.1247224814535; Fri, 10 Jul 2009 04:20:14 -0700 (PDT) In-Reply-To: <20090710110522.GD19636@mint.phcomp.co.uk> References: <7.0.1.0.2.20090708224156.0ac5a438@zend.com> <312025EF-84D0-4411-A0A7-4F0379C3105F@pooteeweet.org> <22CAD497EA2F476DAECEF9302D0B087A@pc> <98b8086f0907091150r47e7a103jaa50fcf7d5b32186@mail.gmail.com> <4A568B33.9000501@gmail.com> <98b8086f0907091744w7ab598dckc427d0a9484664b2@mail.gmail.com> <20090710102302.GC20283@gerbil.thgnet.it> <4A571981.80900@gmail.com> <20090710110522.GD19636@mint.phcomp.co.uk> Date: Fri, 10 Jul 2009 12:20:14 +0100 Message-ID: To: Alain Williams Cc: "Ionut G. Stan" , internals@lists.php.net Content-Type: multipart/alternative; boundary=001636765b690686ff046e5828f6 Subject: Re: [PHP-DEV] Type hinting - Request for Discussion From: lewiswright@gmail.com (Lewis Wright) --001636765b690686ff046e5828f6 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 2009/7/10 Alain Williams > On Fri, Jul 10, 2009 at 01:35:45PM +0300, Ionut G. Stan wrote: > > On 7/10/2009 13:23, Giovanni Giacobbi wrote: > > >On Fri, Jul 10, 2009 at 02:44:52AM +0200, troels knak-nielsen wrote: > > >[...] > > > > > >>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. > > >> > > >> > > I like it too. Not only it solves the initial problem, but it also > > allows userland extensions. For example, > > the current patch does not provide checks for callables, but we already > > have is_callable in the core. > > Hmmm, but it makes simple cases more complicated and slower. But I do like > the > idea of generalising it with a function to handle 'strange' cases. > > So, we have 3 syntaxes: > > 1) function Foo(int $x) { > > $x is tested to be integer, if it isn't an error is raised - either by > exception > or just a fatal error. > > A problem with this is that it could yeild some surprising results, eg: > > Foo(4/2); -- OK > Foo(4/3); -- FAIL since 4/3 yeilds a float > > In this last case people would learn to: > > Foo((int)(4/3)); > > > 2) function Foo((int) $x) { > > $x is cast to int, ie converted iff possible - otherwise an error is > raised: > int -> int -- OK > float -> int -- OK if in the range for integer > string -> int -- OK iff it is 100% clean: > '10' - OK > '10.1' - OK as string -> float -> int > '10ten' - FAIL/ERROR > etc > > What happens when someone tries to use this syntax where he is casting to > an object ? > I suggest that this fails unless the object has a __cast method, in which > case that is invoked: > > function Foo((MyObject) $x) { > ... > } > > class MyObject { > function __cast($x) { > if( ..... ) > return new Foo('abcd', $x); > .... > } > ... > } > > > 3) function Foo(is_int($x)) { > > Function is_int is called, an error is raised if it returns false. > But then you're complicating it to the point where it's no longer much more useful than just calling the is_numeric method in the function body. Plus there's no longer the advantage of optimisers knowing the data-type. Lewis. --001636765b690686ff046e5828f6--