Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92661 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27234 invoked from network); 23 Apr 2016 17:14:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Apr 2016 17:14:26 -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.213.175 cause and error) X-PHP-List-Original-Sender: quim@kalpe.com X-Host-Fingerprint: 209.85.213.175 mail-ig0-f175.google.com Received: from [209.85.213.175] ([209.85.213.175:36454] helo=mail-ig0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AB/E2-07837-17DAB175 for ; Sat, 23 Apr 2016 13:14:25 -0400 Received: by mail-ig0-f175.google.com with SMTP id f1so42720563igr.1 for ; Sat, 23 Apr 2016 10:14:25 -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 :cc; bh=dfbLscQLMU4n1IimZoAAhQUjjF7Q7zRyUqiRZRWtlvE=; b=i0P1t9hF085szQK5kckZMoJO2aNHmFjZhjTOJVfAuuBTtQ+j1ftcj3TzokFx7ubmB4 BpE8Yi+fapCK8c8IPhqqDm80oTqrgttOjAH31/MZPuAMXN/4yZt3gXxqNlG0ym5n4XUQ AHSICi43tLp6p6LHKdX0pV+zXhYhdr6j38tf3r0zDIwXFUf8qQ/JSPYFQrbS+5fuPeqf R9GN1lI0DEhgsweeu6LW6xNCT/1nJCA6yJSFJrGnFG4XgzF0XF5cSZp/Tk2H1FkdlM5i u0e5gLmjiLLYTNjc3s8D4DUMyGa+3fI4mcRvqgHRbF8BKTlIAPF75Q44innOxOfbp0qs EEzw== 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:cc; bh=dfbLscQLMU4n1IimZoAAhQUjjF7Q7zRyUqiRZRWtlvE=; b=V2xIOsj7tvctyEvcZZnD25FRlWzXegQXX7so7iLOumoXuo7MTGJji1Fu/Rlg6WYsBy 4PIL4jxn+UI8MD5jG6vclSCWeH7CHdRY5qh15V9UL5Az15eDy7JUx3qCQCKStiCoW/eN /3OhkIvFHzmLu/sABDCh+bUpQhh85RfR8/lLk2hpYpXLyCg+xWrXVbmrZU+LTBuEdryN MDKSxzuat4x9rQqsIZGooNpLKkm91o2HkFoK1tdMggyYk3hm8LmjmT5laAayccKTQM0L FTNhaQILAlVFa+PVM4KlhU7fN3LQASNpmD8n+JDpcBCyLQolEofF1Nuh/cycZFYcuppw Sg2Q== X-Gm-Message-State: AOPr4FXU9tJOhxZjnFRthl1fMcdZdIly+udF0hBYOKCIbWDNHJZAd8ijUSZI2v5XgnMvOXeLoWhfBtEi+/4I7Q== X-Received: by 10.50.150.107 with SMTP id uh11mr3393454igb.1.1461431662762; Sat, 23 Apr 2016 10:14:22 -0700 (PDT) MIME-Version: 1.0 Received: by 10.36.220.194 with HTTP; Sat, 23 Apr 2016 10:14:03 -0700 (PDT) In-Reply-To: References: <57192F87.4080902@fleshgrinder.com> <571B6AF6.5030001@fleshgrinder.com> Date: Sat, 23 Apr 2016 19:14:03 +0200 Message-ID: To: Levi Morrison Cc: PHP internals Content-Type: multipart/alternative; boundary=001a11c2a0901f874205312a13b5 Subject: Re: [PHP-DEV] [RFC] Nullable Types From: quim@kalpe.com (Quim Calpe) --001a11c2a0901f874205312a13b5 Content-Type: text/plain; charset=UTF-8 On Sat, Apr 23, 2016 at 6:58 PM, Levi Morrison wrote: > 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. > My point was about the developer receiving an Optional type, being less error-prone than a nullable type. Not a language advantage. > > 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. > Yes, I was talking generically here, Option Types without generics-like behaviour are a lot less useful Nullable return types is indeed a better fit for PHP as I said [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. > --001a11c2a0901f874205312a13b5--