Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105231 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 36098 invoked from network); 11 Apr 2019 10:55:06 -0000 Received: from unknown (HELO mail-lf1-f48.google.com) (209.85.167.48) by pb1.pair.com with SMTP; 11 Apr 2019 10:55:06 -0000 Received: by mail-lf1-f48.google.com with SMTP id r25so3893319lfn.13 for ; Thu, 11 Apr 2019 00:52:26 -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; bh=TCd/jKCCk8T0t9qE95K9P998aJnVzjlSrfsapGlUiZs=; b=FzQ4tAiZHNyTc9Zo9niUrGveNDubbTXDjl+M2V8hP4sXzoWxoe5FWi7TCg4Gqy+DuC p4yGRa4mKGMA/ITG4hxP2oipLjRMP64mqp6e3D1ci+PicvvzDkUe+QHwEOe0t499/DzF IwmhE2O0r6pJdqs1CsmnoQ2l5dXN/DyefVRjam4LshDcJTUc9RK27B+Xj3rNSJmfe5GF VRr6/i/1a2KJFTfEgcMfWHUY5tb0Z8cFnJEApIW2FamA+OZn3MeT4EtSm0o2nXOMb+UM fub+kB06MCShswRnBIjPfpFmgQBpMMxIR0e/DcEutDsmAOL2WkudqlQAnQo6ARghfYmA 8fNw== 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; bh=TCd/jKCCk8T0t9qE95K9P998aJnVzjlSrfsapGlUiZs=; b=BsuMWHavpD4GKwuPVbPwSI4CWyzFaHox9DTbHGTDKIk9BD6M4lyHvvABp1ycK+ZU6N 1CIcxb+8EYKTVSdJ/zixLQmfetUJCko568CiZ1yprwg+n7p6wOvyg0mfNoHS+ncgWCt9 ognHfjYxj/b3bwa/r//1C2k+lecYV1Q8cT+xt/Oj/0eYyph1UactEWT4MABl6rgt203I tn/pKcvfh9ObWTjpvsM8+HR/hRry4m5TpawBgNzpdh5e9Js48rS7DvoKn4f4amjYf6b1 gnaYxCjNt6S56fLCYCf+O1NCUeJoPJExfsAJdYNKo/vh3zcwU3ZRfwcyhPEIDiJstWth PpjQ== X-Gm-Message-State: APjAAAWXKVuKC3m3R8orKz8F/s2/T2ACd0HfX8F9NZgD+xOPnMBy5xfi 90GwAeeG0c92HG7x/m9+k7o+KyyLrMSwkm7NRIqa3QU= X-Google-Smtp-Source: APXvYqz+pxorpOOQkxj4s6qcEdNPl44cQRLEX2QURz9mGbymKA7la1Zho+6Daj36DGihBTN9NhMrLkVo6rtXcmnEP7c= X-Received: by 2002:a19:5f1e:: with SMTP id t30mr2610617lfb.17.1554969145742; Thu, 11 Apr 2019 00:52:25 -0700 (PDT) MIME-Version: 1.0 References: <5cadfed8.1c69fb81.31f7d.1c49SMTPIN_ADDED_MISSING@mx.google.com> <5cae100d.1c69fb81.548c.48a4SMTPIN_ADDED_MISSING@mx.google.com> <5cae1fec.1c69fb81.d2407.c20eSMTPIN_ADDED_MISSING@mx.google.com> <5cae2f86.1c69fb81.ce442.006eSMTPIN_ADDED_MISSING@mx.google.com> <5cae8010.1c69fb81.af1d.9b9bSMTPIN_ADDED_MISSING@mx.google.com> In-Reply-To: <5cae8010.1c69fb81.af1d.9b9bSMTPIN_ADDED_MISSING@mx.google.com> Date: Thu, 11 Apr 2019 09:52:16 +0200 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] Nullable Casting From: guilliam.xavier@gmail.com (Guilliam Xavier) OK, I'll try to sum up a bit the recent messages (since Nikita's one): Mark Randall wrote: > As is implied from the name, an implicit cast is done because the code > needs it to, not because it has been told to by the caller. It is > natural to be more conservative when doing something that has not been > explicitly specified. > > I expect a difference in the behaviour of function arguments vs casts > for the reason that although they share a type syntax, they do have two > very different behaviours. That makes sense! > I don't think it would go down very well to have non-nullable casts > throwing TypeErrors for common cases, without an elegant > could-not-convert fallback that doesn't require a 5 line try-catch block. I agree (and even for nullable casts). I'm also fine with the current `(int)null === 0`, `(string)null === ""`, `(array)null === []` etc. (Now maybe things like `(int)"abc" === 0` *could* emit some Notice like already the case for `(string)[] === "Array"`, but that's another discussion...) Benjamin Morel wrote: > Of course there could be helper functions with different semantics, that > would perform a cast-or-null-on-failure, such as `to_int("abc") === null` There was a "Safe Casting Functions" RFC, which was rejected (but that was in 2014, before PHP 7 and scalar type declarations, so who knows how it would go if re-proposed today...) Mark Randall wrote: > $nullable_number = $maybe_null ^?? (int)$maybe_null; That's an... "interesting" idea ^^ (though I would not propose it). > $nullable_number = (null|int)$input; This is explicit, but I worry that `null|int` and `int|null` will be different (since you propose *ordered*) whereas they're the same in phpDoc as well as in the (rejected) "Union Types" RFC. (I also fear things like `(null|float|int|string)$x`...) *** The initial motivation behind the RFC was mainly to have an explicit equivalent of "nullability-preserving" implicit *valid* conversions in symmetry with nullable scalar type declarations and weak-typing mode (e.g. `(?string) $int_or_null` or `(?int) $numeric_string_or_null`) for use in strict-typing mode. But there are indeed various combinations of "invalid" conversions (although one could argue they are programming errors or bugs to fix), and people seem to expect different things from a new "(?type)" cast syntax (not to speak of the existing "(type)" cast) as how to handle those "failures"... Honestly as a newbie here I'm feeling more tired than I had expected and I'm not sure if we'll find a syntax+semantics pair that will reach a consensus... Suggestions and comments very welcome! -- Guilliam Xavier