Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111025 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31938 invoked from network); 15 Jul 2020 17:50:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Jul 2020 17:50:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6BD481805A2 for ; Wed, 15 Jul 2020 09:43:34 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f180.google.com (mail-il1-f180.google.com [209.85.166.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 15 Jul 2020 09:43:33 -0700 (PDT) Received: by mail-il1-f180.google.com with SMTP id q3so2544592ilt.8 for ; Wed, 15 Jul 2020 09:43:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=QFwiik9Fe7By+fglCzI10wJXTaWQo+UlvHuBvcKk7HE=; b=MangF8Tcs9CtM745XjgB5oc0fDbjPgD0cMgckTmc7ut5SKi48uP9BxGJvZTR2TmE2Z sQhSUE3A+2v8aRDSbc3gyLPSRTH2mL98ZWvx//jUoUVD3cuWD4SIzutVEGbAB2tQHmuD ufmC470cOumjxgqAm/p4zcbmCjI/35iqLStuQCQ6hZxRFlOSR7LpD3hOT8sQ3C57adZM +HBn7KV5GJ5BWq0SXCxcLzVRCIAeroGfpdsoAJ64cmRfgncoQAHj1eRbjvu1Jim8A19T 5JjMjExh8U2IgGIPz8GsVA7SSuxMcq+SbaA8Fw6NJGcIyGG83jxwi2SArrEmbXyVUYTB DFPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=QFwiik9Fe7By+fglCzI10wJXTaWQo+UlvHuBvcKk7HE=; b=M0B4RyWSv+1QfBgd53B9dVQA+IRYsr61qCKo3UUHELYwbsR+5gAgP4nKe8lMVJs8Xu LyKjeitk+N8PIUaRcUHMs7Lwcjmsbp4vQazx0WB+C32j5SW6vPSQ/gEJyumQdNeLSLUo dgN2uhPRWYgjXOVxKqarwxOt9yrmQs8IDaQIvkKEBjiumlxepohzj7nzZ+lSCU2miLhH V9hd82eMW1pOolHEC39EFufWl7ANnadsryI1Mr5OHtPKNcyYt8+itBuCw0ZGFlAVqUfL tEYhMmTMK03SjlXOrjVAgk9lk70I0xLzkhA9K+6Y48El04G9sUeDX5PUY5X5vG32l0O/ rqbg== X-Gm-Message-State: AOAM5306U1QURdynoNozwxWjLpnNYF70dyXnlfozK4oalRHEKYkssSiq WnhORw26KmCkQ6OYhxdY3Kg1b1oAJcUyQPFVmgFyiQ+tIUM= X-Google-Smtp-Source: ABdhPJzM7Tr3hGXxX4AbaAyTOG5GZht21faxQTim2VZ6KUexiHOSH3SzzOeVAdlH/XtSD+/elfU1pTJRezVGzAQLM9c= X-Received: by 2002:a92:cec1:: with SMTP id z1mr352384ilq.282.1594831410307; Wed, 15 Jul 2020 09:43:30 -0700 (PDT) MIME-Version: 1.0 References: <21C5073D-3F39-49DA-8686-E027AE780793@joshbruce.dev> In-Reply-To: Date: Wed, 15 Jul 2020 18:43:18 +0200 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000b0274105aa7d9fac" Subject: Re: [PHP-DEV] [RFC][Discussion] Objects can be declared falsifiable From: ocramius@gmail.com (Marco Pivetta) --000000000000b0274105aa7d9fac Content-Type: text/plain; charset="UTF-8" Hey Larry, On Wed, Jul 15, 2020 at 5:32 PM Larry Garfield wrote: > 1) return null, which is a non-type, and thus you need to make the return > type ?User or User|null, which means the caller *must* always check it's > nullness. > > Allowing an object to falsify itself is a 4th, more type-safe option. It > lets you return an object that both fails a boolean check (like null) but > also has default values in it for the base case. A user object likely > wouldn't use that, but a value object like an Address very well could. > > Until we can support for-reals monads (which would require enums and > generics to do properly; the former is possible the latter is very hard), > it's the best option we have for more type-safe returns. > Adding a "falsey" state for code that expects `is_object($foo) === (bool) $foo` seems to be a massive BC break to me, and reduces type safety of boolean operators (and checks) by expanding the `object` type and all operators and expressions that can interact with it. In general, `?T` is very much equivalent to `Maybe T` Where `instance Monad Maybe` is implemented in type-safe langs as (quoting https://en.wikibooks.org/wiki/Haskell/Understanding_monads/Maybe - I omitted `return` because the lifting bit is not really used in PHP): ```hs (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b (>>=) m g = case m of Nothing -> Nothing Just x -> g x ``` `?T` is very much the same in PHP (pseudo-code, since nullability is not represented the same way in Haskell): ```hs (>>=) :: ?T -> (T -> Maybe T2) -> ?T2 (>>=) m g = case m of null -> null x -> g x ``` Let's not confuse Java nullable issues with PHP nullability, which is quite healthy :-) I see adding a "falsey" evaluation to something that has been assumed (for a looooooong time) as universally "truthy" is a very major issue. We need code examples where this is a clear advantage over previous logic: as it stands, I don't see why I would ever prefer `(bool) $object` over `$object !== null`, or `$object->valid()` (which I believe to be an anti-pattern) Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ --000000000000b0274105aa7d9fac--