Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92606 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5468 invoked from network); 21 Apr 2016 19:53:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Apr 2016 19:53:01 -0000 Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 212.232.28.125 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 212.232.28.125 mx204.easyname.com Received: from [212.232.28.125] ([212.232.28.125:37138] helo=mx204.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6B/35-14036-C9F29175 for ; Thu, 21 Apr 2016 15:53:01 -0400 Received: from cable-81-173-133-226.netcologne.de ([81.173.133.226] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1atKem-0002bR-NW; Thu, 21 Apr 2016 19:52:57 +0000 Reply-To: internals@lists.php.net References: To: Lin Yo-An , "guilhermeblanco@gmail.com" Cc: Dmitry Stogov , Tom Worster , internals Message-ID: <57192F87.4080902@fleshgrinder.com> Date: Thu, 21 Apr 2016 21:52:39 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="IA31mDPCLQqwVEJo4XHp7N6pMRJEW1K9J" X-ACL-Warn: X-DNSBL-BARRACUDACENTRAL Subject: Re: [PHP-DEV] [RFC] Nullable Types From: php@fleshgrinder.com (Fleshgrinder) --IA31mDPCLQqwVEJo4XHp7N6pMRJEW1K9J Content-Type: multipart/mixed; boundary="Kmg9csIK6oGCnpDkOP6Sq94qxag8Aaska" From: Fleshgrinder Reply-To: internals@lists.php.net To: Lin Yo-An , "guilhermeblanco@gmail.com" Cc: Dmitry Stogov , Tom Worster , internals Message-ID: <57192F87.4080902@fleshgrinder.com> Subject: Re: [PHP-DEV] [RFC] Nullable Types References: In-Reply-To: --Kmg9csIK6oGCnpDkOP6Sq94qxag8Aaska Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 4/21/2016 1:00 PM, Lin Yo-An wrote: > I think this is not to make PHP like Java, and it totally makes sense -= > Nullable should be a type of a type instead of a state. In Haskell it's= > named Maybe or Option, and It's better than NullPointerException. >=20 > Here is a discussion from Haskell community: > https://mail.haskell.org/pipermail/haskell-cafe/2011-April/091269.html >=20 Why is it /better/? final class None {} final class Some { public $value; public function __construct($value) { $this->value =3D $value; } } final class Maybe { private static $none; private $value; private function __construct($value) { $this->value =3D $value; } public static function NOTHING() { if (self::$nothing =3D=3D=3D null) { self::$nothing =3D new Nothing(); } return new static(self::$nothing); } public static function SOME($value) { return new static(new Some($value)); } public function hasSome(): bool { return $this->value !=3D=3D static::$none; } public function isNone(): bool { return $this->value =3D=3D=3D static::$none; } public function unwrap() { if ($this->value =3D=3D=3D static::$none) { trigger_error('NullPointerException', E_USER_ERROR); } return $this->value->value; } } // ---------------------------------------- function f1(): Option {} $x =3D f1(); if ($x->hasSome()) { echo $x->unwrap(); // 42 } $x =3D f1(); if ($x->isNone()) { echo -1; } echo f1()->unwrap(); // error: NullPointerException :P // ---------------------------------------- function f2(): ?int {} $y =3D f2(); if (isset($y)) { echo $y; // 42 } $y =3D f2(); if ($y =3D=3D=3D null) { echo -1; } echo f2(); // null You can easily build your own Option or Maybe and add the additional bookkeeping to your runtime but it will not make your program more secure or anything. It just adds more method calls and makes it more verbose. You can already use static code analyzers to detect if null is being ignored in your code, however, right now they mainly have to rely on PhpDoc that is often not properly documented. Adding an explicit syntax to PHP for documentation would make PhpDoc obsolete and allow static code analyzers to perform their job better and PHP to error out during runtime if something else is being returned. Honestly, null is not our problem. Every language has its null, just because they wrap it or rename it does not make null suddenly vanish from the universe. :P --=20 Richard "Fleshgrinder" Fussenegger --Kmg9csIK6oGCnpDkOP6Sq94qxag8Aaska-- --IA31mDPCLQqwVEJo4XHp7N6pMRJEW1K9J Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJXGS+KAAoJEOKkKcqFPVVrLhwQAJcMDrQMrXU0hYT46KX7++tu GVvr2qVm+C+IJRWuu77i4Qt2uN+IaO+XSvroSkl5So+xkVExToF6dq9X1joJ6TcE bgzaScrb6pF2vMyPg6XR0iBunKumH6fsfn3jwSml9IuwpBTuERVR5fGkOaWba1Sa AvBbbit9YtX95Y3W9Q7gNteAz+01xBdL9nW8Ved8TS+XYVaCOC41LgHtqSMzNs3j OUfFNcACUHWSzl9DrHZDhMbj3cAR5D1/TOQaHxbp8udc7HXtCkSnjn8yCDwdrO+A slMd1nuY4EaPjmchSCVeNo1eSiBOw5T1p6YhT0cc+2RDAGxLYh3Ewulmu8n1LkCF eoC6M5u9GZytqnDtXWExCxzNWpK57+4xKyp/AFeLwG4YoiyyJymr6j90EITCjDAz eHxcDvBU+g8ccAgwL7OKJ/CIloqQPZtRre9vz20PtLfkQmS6L3cCPBpniACpWNJ1 uiLuKV6bLrzp6a/GftP/YnczwX46QNgfcZVmMsEvfyeZ19L/e74dGOe1Lypg8Luv B+BVh+PaC4kh5GL6XnRVtAiPCY3uDgfW/M//in7E0F5AqSmHkwLMMrcKKWm4jcvm OPRakmb+zJZu11ZXtipcUhEyLnqhAy3MZhz27VBN0Nw4TzcYoPJgDwCzW+oXU84X zn99ZEEiMFWqhcvNIAFS =KDvx -----END PGP SIGNATURE----- --IA31mDPCLQqwVEJo4XHp7N6pMRJEW1K9J--