Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92660 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25146 invoked from network); 23 Apr 2016 16:58:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Apr 2016 16:58:36 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.66 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.220.66 mail-pa0-f66.google.com Received: from [209.85.220.66] ([209.85.220.66:34412] helo=mail-pa0-f66.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 86/82-07837-BB9AB175 for ; Sat, 23 Apr 2016 12:58:36 -0400 Received: by mail-pa0-f66.google.com with SMTP id yl2so8874658pac.1 for ; Sat, 23 Apr 2016 09:58:35 -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=nBcK+1Kxf50LW6Pau8MmT02d268uzGPOD28siKpsVwo=; b=pz6c8YZdQTayNlL4VCkK90rbygDlbZ67ub4k4Z51omuoBXPBTOCHHcL7GJvMyyclGe Sarmm7OOB2UOrg92lprlum+Mwm5wzuL9NLSJVpGnNCFXnKSj/QXmU9CGWmCl9lw1+PQz +unyY20jb2+nh7u+BIyYcPhvz6ogErwgKA5UZharbY+mgUqpP+qt2jg+ndxh8zoFEuNX OMMUFv4aQn9w/SfXqtfr5K8lHF10sSDp43tMG6PpApGl0e/VdG2em2vzFYRRuUtSsFF8 kLo5lUddzjDRpFV16zLF/MDEN+LLzOT1T4+UimBq1WCZJ7Re6Qq2wWKVDGVFLN5eb0Kh nz/g== 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=nBcK+1Kxf50LW6Pau8MmT02d268uzGPOD28siKpsVwo=; b=X1Fe8w0+3e04yGl7v+0ggHFXSmB1r1yb8VAE++jpCeNpMkcCWpUiyb8ebweoY3JUZD zDDcj363hIvuv6nt3C2htNA83D6xs36Sm46UvfJ3r2vkpYQQ/lkIai/tVvBvGAszAyZO mW+ou1+beIuYv99v+jFVmjlLCwE0ld+I9nM481hrzPVxpRqXXK0iAgCMN69rR26AP+jB rBiAlPDjXC9zFCIpVwQ2ozjjQV1+xlCvwzxGSpXK8KuZvZu5VXdZbMADT2YLa7Bo+HiB +rMrhnGyRTEcEe3BEifyOPRrijYFBPv1RLYOcAfKzfS9XCCNWRWbdUIAgj8e0TCrj9+n kilw== X-Gm-Message-State: AOPr4FUjrczOSsTsTy7FeqkXUb0acy99mJ774xDjv0MjhpGkfd+BUz2/pSwnYIp8r1wWCDmHKvpTHYKX8K+UYQ== MIME-Version: 1.0 X-Received: by 10.66.40.236 with SMTP id a12mr37229725pal.58.1461430712983; Sat, 23 Apr 2016 09:58:32 -0700 (PDT) Sender: morrison.levi@gmail.com Received: by 10.66.132.79 with HTTP; Sat, 23 Apr 2016 09:58:32 -0700 (PDT) In-Reply-To: References: <57192F87.4080902@fleshgrinder.com> <571B6AF6.5030001@fleshgrinder.com> Date: Sat, 23 Apr 2016 10:58:32 -0600 X-Google-Sender-Auth: T1TjFzpgORxqwsndyU9GtuoacE0 Message-ID: To: Quim Calpe Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] [RFC] Nullable Types From: levim@php.net (Levi Morrison) On Sat, Apr 23, 2016 at 10:40 AM, Quim Calpe wrote: > Hi Richard, > > On Sat, Apr 23, 2016 at 2:30 PM, Fleshgrinder wrote: > >> On 4/22/2016 11:42 AM, Quim Calpe wrote: >> > 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. >> > >> >> IMOH, the point of ?T is the intention, if you get a null from a method, >> you have to deal with a null branch. Of course you can just ignore it >> and go on, but it's a developer decision to do that, not an oversight as >> using a Option as an object directly. >> >> Sorry, but this works in both directions. The problem is not null, the >> problem is that there is no system that warns you (e.g. a compiler) >> about the missing null check. I think that Ceylon solved this problem >> extremely nicely without the introduction of something special. >> > > Of course, this works in both directions, but I see a value in Option types: > > function getByEmail(string $email) : ?User {} > $user = getByEmail("foo@bar.com"); > echo $user->getName(); > > I neglect the nullable return but works at first , all fine... > .. a week later... "Warning: Call to a member function getName() on null" > > With Option type: > function getByEmail(string $email) : Option[User] {} > $user = getByEmail("foo@bar.com"); > echo $user->getName(); > > IDE could warn me and It crashes right away, an option type must be > unwrapped so I get the "intention" immediately :) > > >> >> function fn(): ?T {} >> >> $x = fn(); >> if (is $x T) {} >> else {} >> >> Not doing as above is a compiler error in Ceylon. However, I already >> wrote multiple times that there are already statical code analysis tools >> available that can find exactly such things in your code. One just needs >> to use them. >> > > That's really nice > >> >> -- >> Richard "Fleshgrinder" Fussenegger >> >> > Option types are nice, but I feel we are going a bit off-topic. Option > types work better with other niceties like for comprehensions, pattern > matching... And I don't see PHP going that route in the near future, and > probably It's not okay for PHP to go that route... > > Nullable return types is a better fit for PHP, null has been in the > language from the beginning, I agree here Option is no better than a union type with null[1]. If a language requires an option to be unwrapped then it can do the same with some type or null. This is what Swift does. These things are exactly equivalent. However in PHP we do not have generics, which means a nullable type is actually better because we can express the type that participates with null. With an Option type we cannot. [1] At least with the behaviors here. If Option is a Traverable that returns 0 or 1 items then we can use that behavior to our advantage. Of course, we lose the ability to express the underlying option type.