Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92532 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 20182 invoked from network); 20 Apr 2016 10:11:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Apr 2016 10:11:39 -0000 Authentication-Results: pb1.pair.com header.from=jesseschalken@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jesseschalken@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.181 as permitted sender) X-PHP-List-Original-Sender: jesseschalken@gmail.com X-Host-Fingerprint: 209.85.223.181 mail-io0-f181.google.com Received: from [209.85.223.181] ([209.85.223.181:33977] helo=mail-io0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D6/A6-14036-9D557175 for ; Wed, 20 Apr 2016 06:11:37 -0400 Received: by mail-io0-f181.google.com with SMTP id 2so47170379ioy.1 for ; Wed, 20 Apr 2016 03:11:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc; bh=yUtH+QHAH3rX3RGwrv60CNyAtc+Zmv0N0DGDcuh53ow=; b=IjBvRP8uBQMXA4XMe1zM4vub6q0tfRzDLDrbtQrgk+jFkulq+tS9A9iWbNv1UEp/wz 8vduqto3ZiZgo9rlG7QsYJxDf2a5s0cuSnHNrJCx7aBdfpYBwxjycxVAqSGZ/Uf6u9/R vLaxUgmZcy8YqyxEuCkBfWYlzawQtaon7CNjEkClgK246nr3Wv7nMHjP9OEf4UyvFggW jyffiXL2iWGl3ZFekGaDRR/mMmUye5mQYxewYKTb6gOgN7HCmpsY7a+8avNF7tO2bBJl WVz7LszVajMDgHnUS5QUmkbhNSyidhjx3Cocq7eQKvz//JvDatKeBt2VmV4cnf0Dq+Qm /Qbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:date :message-id:subject:from:to:cc; bh=yUtH+QHAH3rX3RGwrv60CNyAtc+Zmv0N0DGDcuh53ow=; b=D5nqCtGnJopqHc0/wFh1aiFPFdEt/ZhF6TZu/TKmcHQl1d0tYWbDqEiRi8gqeThII8 joywX/L4txRufmDPHyY5/pe1A70deAuwIZ52epK0iFQD2dNISBm+hhHLe0eBv0NKJ+V4 gbH1Ldj5i9dhQZcMaB0fNS4PxVMb5k5MQr1nGqWv/G+rS6rMtcRFmSE5wMypjTBuQmOc ktaP6cZA8ZNwA7Hch3a2RlDEdBIVhVhY6n/I4doFCa03unzRJaWVM4S2MjT9tTc64TsA 1t4qoj4nqigXb1bLnEdgRsZTskjNax75WsEXGkjO+/auBzlSqCxWyMfunmvKmans79M8 xtAw== X-Gm-Message-State: AOPr4FV/goaxcrWuaUMtyqTBBA9G7SUKV0LGDAJw30ivOmNtyPIqp9maGFtiz/REvq9l4gjijZWBSEqRdyTrNQ== MIME-Version: 1.0 X-Received: by 10.107.135.202 with SMTP id r71mr8872979ioi.151.1461147094213; Wed, 20 Apr 2016 03:11:34 -0700 (PDT) Sender: jesseschalken@gmail.com Received: by 10.79.139.133 with HTTP; Wed, 20 Apr 2016 03:11:33 -0700 (PDT) In-Reply-To: <1461145606.7556.11.camel@kuechenschabe> References: <570E99AC.3090804@fleshgrinder.com> <570EA5EB.8090501@fleshgrinder.com> <570EAB0D.6080706@gmail.com> <570EB67E.8010908@garfieldtech.com> <5B147E88-CC0A-4CBC-A49D-C7FE3BF557C0@zend.com> <6F.C3.12455.94C5F075@pb1.pair.com> <20160414094440.GF19347@phcomp.co.uk> <570FD94F.90703@fleshgrinder.com> <570FE8A9.4020809@gmail.com> <20.53.29891.17401175@pb1.pair.com> <1461145606.7556.11.camel@kuechenschabe> Date: Wed, 20 Apr 2016 20:11:33 +1000 X-Google-Sender-Auth: 7AJnMluTaLWQZJnHsxtIu8m1G5c Message-ID: To: =?UTF-8?Q?Johannes_Schl=C3=BCter?= Cc: Andrea Faulds , PHP internals Content-Type: multipart/alternative; boundary=001a113ea5f083ee130530e7d189 Subject: Re: [PHP-DEV] Re: Improving PHP's type system From: me@jesseschalken.com (Jesse Schalken) --001a113ea5f083ee130530e7d189 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 1. I have a function that converts values to SQL which have SQL equivalents, including decimal values represented by a special Decimal class. It accepts int|string|bool|null|float|Decimal. 2. I have a class Expr which represents an expression in SQL. Expressions can be composed of other expressions, and of literal values,= so the constructors for Exprs accept int|string|bool|null|float|Decimal|Exp= r. 3. I have a function that converts values to JSON that can be reliably converted back into their original. It accepts int|string|bool|null|floa= t. 4. I have a function that returns the first key in an array (or throws an exception if empty). It returns int|string. 5. I have a function that returns a value for a key in an array, and removes the key. The type of the key is things that are valid array keys (int|string|bool|null IIRC). 6. I have a set of functions which operates on numbers. They accept int|float. 7. All the PHP functions which say "returns ... or FALSE on failure" have their correct return type as T|false (where T is the return value o= n success) (or T|bool is "false" is not an allowed type). 8. Nullability is nothing but a special case of union type with T|null. If you need more examples, search some PHP codebases for PHPDoc blocks with union types with a regex, like "@(param|var)\s*(\$\w+\s*)?\w+\|". On Wed, Apr 20, 2016 at 7:46 PM, Johannes Schl=C3=BCter wrote: > On Wed, 2016-04-20 at 17:57 +1000, Jesse Schalken wrote: > > > > With unions: > > > > function foo(Bar|string $b) { > > > > if (is_string($b)) { > > // ... > > } else { > > // I know $b is a Bar here. I don't need to check. :) > > } > > } > > I' still missing a real-life use case for this. There's no operation I > can come up with which works with an object or string. > > Unions between object types should be handled via interface. > > The only relevant union types I found till now are > > array | Traversable > array | Countable > > and eventually > > array | Offset[Get|Set] > > The last one is a bit critical due to the fact that arrays are value > types, whereas objects are reference types so > > function ($a) { > $a[] =3D 42; > } > > behaves notably differently between objects and arrays. So that case > might need some thinking. For the others instead of a generic union type > I'd prefer a special array-solution. > > johannes > --001a113ea5f083ee130530e7d189--