Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92634 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75886 invoked from network); 22 Apr 2016 09:42:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Apr 2016 09:42:57 -0000 Authentication-Results: pb1.pair.com smtp.mail=quim@kalpe.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=quim@kalpe.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain kalpe.com from 209.85.223.170 cause and error) X-PHP-List-Original-Sender: quim@kalpe.com X-Host-Fingerprint: 209.85.223.170 mail-io0-f170.google.com Received: from [209.85.223.170] ([209.85.223.170:35747] helo=mail-io0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EC/A0-14036-022F9175 for ; Fri, 22 Apr 2016 05:42:57 -0400 Received: by mail-io0-f170.google.com with SMTP id d62so21057807iof.2 for ; Fri, 22 Apr 2016 02:42:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kalpe-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=egbBACbWnIdizkcO6i9XfL0bPvgTjaGzAk6f2BtXGU8=; b=Oi661NcNVmh2x42sW+WtGVZRjidE+KdaOLPV25RUGN9WRPXP3AlYkPHLGP2YTmcl1V 42BPqEDIdPv/dCHlEdNrK/tIQLLFQc7ypmvK6uCl5XxjPDSJZnbMy9WbYbo4AXDmbWHl 5B89ZJmwi8SVfg0/28hY4ItucfxPRLUHV5fyVEyVorYMMBW17rBoKF0X6h3XgVBFbKDB ZRAAa5YiDGtL7yfC6H5LjpcNOv+H20uLfUJQhG3tDO3dbiLwhS7lFqQ5lYOxV1VD2Eht to+aS7AxQHZzsMouHka6+hH2dBsroGMTpRunJybGYC6P/idhFtSTiFzu5kpLZw0yC2IA nJHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=egbBACbWnIdizkcO6i9XfL0bPvgTjaGzAk6f2BtXGU8=; b=bMF0sOU9rmnfyNOjFAb+pakJyUj/lj7gLqn0sIn9Qr2tLmm8/DvFjMYs7RqZTsIWjc EukvTXrBDKoxJiWDD7hXXOvhtpQHce5xeZYHIdzRIOyGh/h+3FsXZXYn3DWDHSLHXCtu p1Rg7Vxm7ZvuuCRTKwBR8OwxCr/E2oy/NBl1J1w/2cTRFGO+1EXsVzzP0RdDvrqyv0Yl enDwpB8Nt+JWMKDnsH6SbxEqfJPH5eqi0qe/JlZFHOA8wZZj8FnySjQ2yUyhXHgKKS4W lY/mqb2jtJwojbzWJNuQipGJ9+xuWt/qkwnztaZ/vXIKiCwRMRbuiRHoprrNnorYl9L9 lX+g== X-Gm-Message-State: AOPr4FU512BugPyzTscK/kdu3Q5rUdiMmAHgQ9uCW72V7FdKD0IurlW4gRKuXWSqIR3uU+JVqGVlWMRjhywMgQ== X-Received: by 10.107.134.151 with SMTP id q23mr22833608ioi.25.1461318174138; Fri, 22 Apr 2016 02:42:54 -0700 (PDT) MIME-Version: 1.0 Received: by 10.36.220.194 with HTTP; Fri, 22 Apr 2016 02:42:34 -0700 (PDT) In-Reply-To: <57192F87.4080902@fleshgrinder.com> References: <57192F87.4080902@fleshgrinder.com> Date: Fri, 22 Apr 2016 11:42:34 +0200 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary=001a113ec300ac7b2d05310fa64c Subject: Re: [PHP-DEV] [RFC] Nullable Types From: quim@kalpe.com (Quim Calpe) --001a113ec300ac7b2d05310fa64c Content-Type: text/plain; charset=UTF-8 On Thu, Apr 21, 2016 at 9:52 PM, Fleshgrinder wrote: > 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. > > > > Here is a discussion from Haskell community: > > https://mail.haskell.org/pipermail/haskell-cafe/2011-April/091269.html > > > > Why is it /better/? > > final class None {} > > final class Some { > > public $value; > > public function __construct($value) { > $this->value = $value; > } > > } > > final class Maybe { > > private static $none; > > private $value; > > private function __construct($value) { > $this->value = $value; > } > > public static function NOTHING() { > if (self::$nothing === null) { > self::$nothing = 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 !== static::$none; > } > > public function isNone(): bool { > return $this->value === static::$none; > } > > public function unwrap() { > if ($this->value === static::$none) { > trigger_error('NullPointerException', E_USER_ERROR); > } > return $this->value->value; > } > > } > > // ---------------------------------------- > > function f1(): Option {} > > $x = f1(); > if ($x->hasSome()) { > echo $x->unwrap(); // 42 > } > > $x = f1(); > if ($x->isNone()) { > echo -1; > } > > echo f1()->unwrap(); // error: NullPointerException :P > > // ---------------------------------------- > > function f2(): ?int {} > > $y = f2(); > if (isset($y)) { > echo $y; // 42 > } > > $y = f2(); > if ($y === 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 > > -- > Richard "Fleshgrinder" Fussenegger > > IMHO, the point of Optional types is the intention, if you get an Option from a method, you have to deal with a None branch. Of course you can just unwrap and go on, but it's a developer decision to do that, not an oversight as using a Foo|null (or ?Foo) as an object directly. --001a113ec300ac7b2d05310fa64c--