Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:68093 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38046 invoked from network); 10 Jul 2013 18:31:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Jul 2013 18:31:07 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@golemon.com; spf=softfail; sender-id=softfail Authentication-Results: pb1.pair.com header.from=php@golemon.com; sender-id=softfail Received-SPF: softfail (pb1.pair.com: domain golemon.com does not designate 209.85.160.42 as permitted sender) X-PHP-List-Original-Sender: php@golemon.com X-Host-Fingerprint: 209.85.160.42 mail-pb0-f42.google.com Received: from [209.85.160.42] ([209.85.160.42:50028] helo=mail-pb0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 59/FE-50933-668ADD15 for ; Wed, 10 Jul 2013 14:31:07 -0400 Received: by mail-pb0-f42.google.com with SMTP id un1so6995219pbc.29 for ; Wed, 10 Jul 2013 11:30:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:x-originating-ip:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type :x-gm-message-state; bh=2leU3t6s9gNNneBoniDHGyX/oAmBopLUiDpN5nSJqYU=; b=DsqF/Fi7fDTZ0hhZOUnuyGsI7LaC3q/2x4kDjtbZm2OxH1EjDgYPWF86I9DMW24dpT gOBOwCB0O068gFo+sa5udgTBEcnQKZtqGTbu4Hlwzeai1bxHwrYGw0rMDtLLybfOxG30 93O3Tw+y4VzIpmyxZoqmgGtNldcdxcL5GPEfc+CnkQ4W37fPsoO6LjjDb9GLyxE47BxI PfdG8ASOgF9GjS4+j7vLnaYytYYz+V/5vm4ynZB5xBqjVAOMgQmrf3TsAXsEYC3cuKlY f8u9y+3xbyLzS0KqdSnbiZbBwJnZPG+jLOYvLWy3tzRWtidnOpVvG+WUHmyeTiQOGEju IEyg== MIME-Version: 1.0 X-Received: by 10.68.250.5 with SMTP id yy5mr33062909pbc.93.1373481059599; Wed, 10 Jul 2013 11:30:59 -0700 (PDT) Sender: php@golemon.com Received: by 10.70.132.105 with HTTP; Wed, 10 Jul 2013 11:30:59 -0700 (PDT) X-Originating-IP: [173.252.71.7] In-Reply-To: References: <1372258135.2410.22.camel@guybrush> Date: Wed, 10 Jul 2013 11:30:59 -0700 X-Google-Sender-Auth: ZzGiPRfcpg7sziSsxPip0q1sq30 Message-ID: To: RQuadling@gmail.com, PHP internals Content-Type: multipart/alternative; boundary=047d7b66f9f7a9dbf604e12c7a68 X-Gm-Message-State: ALoCoQkp3iEfsC4J+HFPrMem+sLshC1GYq6H/pMgnpdFsu1358sAgLA+Lstzp8wg+As+oId8u4fC Subject: Re: [PHP-DEV] Request for comments - new PHP feature: return typing From: pollita@php.net (Sara Golemon) --047d7b66f9f7a9dbf604e12c7a68 Content-Type: text/plain; charset=ISO-8859-1 > With regard to any sort of hinting, an issue of 'one of' return types > would need to be addressed (I hope). > > function foo(SomeClass $bar, int $baz) : bool, SomeClass {} > > Allowing for a failure (False) or a success (SomeClass). > > We (HHVM) deal with failure alternates by way of a "nullable" syntax: function foo(SomeClass $bar, int $baz) : ?SomeClass {} Means the function returns SomeClass or null. null being the "failure" case. We also do "mixed" for anything (basically, no type hint, but explicitly stated), and "numeric" for a less aggressive float/int/numeric-string (though that's more specific to arg types than return types). > > I think it would useful to have a Null hint (like C's void I suppose). > More to do with explicitly saying no return type, rather than saying > nothing which means an undefined return type. > > function foo(SomeClass $bar, scalar $baz) {} /** you can return > something, but who knows what will happen with it. **/ > vs > function foo(SomeClass $bar, scalar $baz) : Null {} /** This method does > not return anything and to do so would be wrong. **/ > > function foo() : void {} is our syntax. A null return value is an actual thing (a null thing), whereas no return is more explicit. I know that `return;` gets translated into `return null;` anyway, but we also look at the caller side (return_value_used) and warn about "attempt to use value from a function returning void" like C/C++ do. Another advantage of scalar is the ability to stop scalar only functions > from receiving objects, arrays, etc.. Currently no way to inhibit that > without type testing in the function. > > We don't currently do "any scalar", though we do have "numeric" (as mentioned above). I agree with you that "scalar" would be a nice meta-type. > And, if PHP evolves to allow for __toScalar(), then an object can > represent itself as a scalar for those situations where the scalar type > hint is required. I think this may be a bit much initially though (unless > some clever bod has already worked all this out). Being able to cast an > instance from type A to type B via __autocast_XXXX where XXXX is the > resultant type ... ha ha ... OK that really is just pushing things. Maybe. > > Going off-topic here, but I worry that __toScalar() would collide in a messy way with __toString() (and other, future __toXXXX() behavior). Not severely against it, mind you, but I think we (PHP) need to be cautious about going down that road. -Sara P.S. - Your reply only went to me, but I assume you wanted to include internals@ so I put them back in the To: line. --047d7b66f9f7a9dbf604e12c7a68--