Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118758 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 61887 invoked from network); 5 Oct 2022 18:01:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Oct 2022 18:01:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 51FD7180504 for ; Wed, 5 Oct 2022 11:01:32 -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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 5 Oct 2022 11:01:31 -0700 (PDT) Received: by mail-ej1-f46.google.com with SMTP id nb11so37147505ejc.5 for ; Wed, 05 Oct 2022 11:01:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=JoPqBFwXGbxzVfOyKUNTi1TBPCcj/4YmLo7Q5fyZW5A=; b=IMOmyV0H8GF8fnIwIrXvv5hiBPIJBurtn9uvPoVVdgrGmMaNUR0wl876V5bVUomS7d tNQu/Bb6jscrEzVkGXEME6pTslW1L/v1c5CsUNoH9SvYDQFhtj1qSw1E9J7+405dmNwH Wb/Yyfgx7JqOjYtBz7QK8LxglcgF1T60RJmj2/XSRjmP8hr4PhGl+v4GjLeWwVTmEpjW /gNhJwEPaxvAq94YNKU9Ri8chp/mjo5g2N6d9tXvRn+2zJVdNXwLvREz+t350Wa+leaU 2M7vr2bUBLIBcSmeUKsyEgTN++wdecnTBG4F166GP6S/xSOjCqA/52+nzDadnN2e8Khg EeKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=JoPqBFwXGbxzVfOyKUNTi1TBPCcj/4YmLo7Q5fyZW5A=; b=ZDtNpjIlvOxpB41gG9Fhv40pn4jDchO8kQd0hv2IGtBfAUKnk1OMPazomt22oRjLOh SlXe82sc13TflNX6p2foG34mrtbpQV24BEr6Fbkvc5jDw11DK6T6AF7h5LT19ETalqAr 8IWMp8G++WqHK1/2NSB4/7GFIXCxL6e/9atwoIDv1oKeo2qKwI9OHcAIJPH9JvJthADD BmJYElcDcb/YnY8bw9341xpjw9PhmSMM0L8jEJDtykEQv55+nmiDFOwjS48Ndi9NuZeH +6bgFp+qqRyhvRP9VMdjFHsOixaF1TLakuhuL55GZSS9T1YeAsYNcQ/pB0hS4tQ+0S57 /Kmw== X-Gm-Message-State: ACrzQf3zC9OHqCyx2Z/VcfsD9bcW1hBiwhM1I63NXtttNzoa1SqyYM9N 9HVzegaVnCYm53gDYr3VtP+cjtegjalhDJ1GrMMW/VHKZFQ= X-Google-Smtp-Source: AMsMyM5ApjAPjb1hpKXyb+BxfIlyLMcGnzEVbOa7SbOKwQTAOvREFVdVgDsnUJbkrBkg/4BAI5zej8BYh2NDoP+7CRs= X-Received: by 2002:a17:907:75ec:b0:78d:2492:d8af with SMTP id jz12-20020a17090775ec00b0078d2492d8afmr673369ejc.654.1664992890285; Wed, 05 Oct 2022 11:01:30 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 5 Oct 2022 20:01:19 +0200 Message-ID: To: Eugene Sidelnyk Cc: juan carlos morales , Claude Pache , PHP Internals Content-Type: multipart/alternative; boundary="000000000000c774bb05ea4d5de6" Subject: Re: [PHP-DEV] Union type casts From: ocramius@gmail.com (Marco Pivetta) --000000000000c774bb05ea4d5de6 Content-Type: text/plain; charset="UTF-8" On Wed, 5 Oct 2022 at 19:53, Eugene Sidelnyk wrote: > I used to have an awkward feeling when it was necessary to convert the > nullable integer into a nullable string. It would be kind of ternary > operator anyway. Either `$b = (null !== $a) ? (string)$a : null;` or `$b = > (string)$a ?: null;`. > > A lot easier to read would it be if written this way: `$b = (?string)$a;`. > The intentions are clear when doing explicit type-cast. > > Currently, I don't see any other situations when this feature may be useful > I don't think that this tiny edge case warrants a new language feature. I can endorse using [`azjezz/psl`]( https://github.com/azjezz/psl/blob/34391b78f097e7d489bd618d2cb15ef474937896/src/Psl/Type/TypeInterface.php#L21-L26) for this though: ```php use Psl\Type; $myValue = Type\union(Type\int(), Type\null()) ->coerce($input); ``` This will also throw an exception (**GOOD**) if the cast is not safe to perform. Marco Pivetta https://twitter.com/Ocramius https://ocramius.github.io/ --000000000000c774bb05ea4d5de6--