Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122726 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 46B9C1A009C for ; Fri, 22 Mar 2024 12:59:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711112369; bh=K2XWrcvm8/zTGFEEUGtNHGHVutZwyubBNJqefyRDXNA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=nBPENCRScOvx53xn/odVUVZjeeHMM8EyvJ1dy8AuxKJluNumi6mTg1t9txGtXhdBJ uPhG6EWX3c8tcczsBBrqI+E7EZcg6hOWyBt9lB0Z4mi0OYMTlzXy4ppIKxfSqwFvEP CWWdNakwUOAdQZEtoMJhKT1avCIL0dMINiu6wnlsLnTLKt2dhCc6HucrCuOuVRzFBv fZqepYH00e7GcwCeBPdYNB3HGzNHpTAedMayMcovIRgKwlWVO3QNXQ0IFQ8p22v5bT pz/7sYuukhfCd1wEGE+vTA9nKGmRLHzClJ3onzJ35HzyVrjZYKiZkGs3FSTvYO+wD/ Cqu6Eoj6EU+ew== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 293C2180680 for ; Fri, 22 Mar 2024 12:59:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, 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=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-oa1-f50.google.com (mail-oa1-f50.google.com [209.85.160.50]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 22 Mar 2024 12:59:28 +0000 (UTC) Received: by mail-oa1-f50.google.com with SMTP id 586e51a60fabf-221e6bf537dso1277216fac.1 for ; Fri, 22 Mar 2024 05:59:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711112346; x=1711717146; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=zzAV+oV+ZaeCozwlxuFTDVYmN8wVrf1CGiH9GeM/0f8=; b=hiE3+CbWidb1oJ5jN9nJDhlRcNFCZ4fXPESFQevBdh2s6gusj/chQZjegqF5T3JnZV 12vSNeAmcWEDnOmjuftmYP1eyTMuLkX+NdyXJEXVIDjOLASxr9FhvXMfBEx9TKJUz1J6 y7fT5HyfSGLmhy6aAnQihWjmFIUODJ2woAroKi96ORrd1zOicxSuLNhp2j11ToQWcMcO B0BnUC23mfhXRzwvaOKN0672pf08clfIKAnbR0h90DY3oc4MpkbcZlfTnshZ+G4yZwVo HAArUstbneB7RDmIM1JVRI/ZeQWZ3xwV7hPWHKQ7q2cYGK//t7vxXC/W18gMpfANM5EL BLlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711112346; x=1711717146; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zzAV+oV+ZaeCozwlxuFTDVYmN8wVrf1CGiH9GeM/0f8=; b=vacdQhV+1zL9hm8KjcF57LDCcm4UyRjtfyOqn8P+nEWO+Ld/5CmWYxytDpF1bdjIVc oPhRPbr1LOpC0MVO+MuC2CtMsyFl5Sd5PkGw0BMde4jEJU4Eo7leBGRxyhXhOkMeSum5 DaaG80IEMakUEXh9t/Jg8ZnIo6fyX/TFCY0Ri1RNo5OO6UBWtlhGcUTnoB5bZpK2KNtZ FTWI0CkhfHOSCfKKWh3wqX+8e0rO7hPPu4Rb8QFMTu3NLPboX8FcfYX+e60uYLE7mUTz DQbWYOcQ+rj93EvZ1S0P8xe3O/4vA1kuE+2LWq7jrh44ALHK0ZwnBtW8k51eXtSjFjWO 8FEg== X-Gm-Message-State: AOJu0YweO1DLvxjXLASuoHirwXp8S1vlOmzn6Ot/zbGpmbU97p2HLK1k Dp2/VqsrA/LxUcW6TfsM4r4q6gLEos73NoGhzZ1HWwRaWJGrelkHeWzasBt3QDE5+A5iZJnSobV RtkmcwsIbuPfMc97eE5lS7iZlmRQiYVUJ+PoniA== X-Google-Smtp-Source: AGHT+IHxu5bpxzmSlP3goVjhoN+6JIq2xwXmo7bUWgpxlVR7ASo4mQ5i7p1DhGcGCfAXFiu41m8JJBybSLK4573yhpM= X-Received: by 2002:a05:6870:2042:b0:222:6240:6e72 with SMTP id l2-20020a056870204200b0022262406e72mr2342947oad.0.1711112345827; Fri, 22 Mar 2024 05:59:05 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <3F78A125-1946-42E2-A4F5-A2B282BE2107@rwec.co.uk> <2d7ec203-6e80-445c-94f4-d29ef58743b1@rwec.co.uk> <7d53f2e4-46f3-46f1-89b0-6e1d3b0b2e32@app.fastmail.com> In-Reply-To: <7d53f2e4-46f3-46f1-89b0-6e1d3b0b2e32@app.fastmail.com> Date: Fri, 22 Mar 2024 13:58:54 +0100 Message-ID: Subject: Re: [PHP-DEV] Proposal: AS assertions To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: landers.robert@gmail.com (Robert Landers) On Fri, Mar 22, 2024 at 12:01=E2=80=AFPM Rowan Tommins [IMSoP] wrote: > > On Fri, 22 Mar 2024, at 08:17, Jordi Boggiano wrote: > > We perhaps could make sure that as does not throw if used with `??`, or t= hat `??` catches the type error and returns the right-hand expression inste= ad: > > So to do a nullable typecast you would do: > > $a as int|float ?? null > > > While this limits the impact to only expressions combining as with ?? it = still has the same fundamental problem: you can't meaningfully use it with = a nullable type. > > > As a concrete example, imagine you have an optional $description paramete= r, and want to ensure any non-null values are converted to string, but keep= null unchanged. > > At first sight, it looks like you could write this: > > $descString =3D $description as string|null ?? (string)$description; > > But this won't work - the ?? swallows the null and turns it into an empty= string, which isn't what you wanted. You need some syntax that catches the= TypeError, but preserves the null: > > $descString =3D $description as string|null else (string)$description; > // or > $descString =3D $description as string|null catch (string)$description; > // or > $descString =3D $description as string|null default (string)$description; > > > I actually think there are quite a lot of scenarios where that idiom woul= d be useful: > > $optionalExpiryDateTime =3D $expiry as ?DateTimeInterface else new DateTi= meImmutable($expiry); > $optionalUnixTimestamp =3D $time as ?int else strotime((string)$time); > $optionalUnicodeName =3D $name as ?UnicodeString else new UnicodeString( = $name ); > etc > > And once you have that, you don't need anything special for the null case= , it's just: > > $nameString =3D $name as ?string else null; > > Regards, > -- > Rowan Tommins > [IMSoP] I'm not sure I can grok what this does... $optionalExpiryDateTime =3D ($expiry =3D=3D=3D null || $expiry instanceof DateTimeInterface) ? $expiry : new DateTimeImmutable($expiry) Maybe? What would be the usefulness of this in real life code? I've never written anything like it in my life. Personally, this is much more readable (assuming I got the logic right): using always null if not match, and handle the case for when $expiry isn't a string: $optionalExpiryDateTime =3D $expiry =3D=3D null ? $expiry : $expiry as DateTimeInterface ?? new DateTimeImmutable($expiry as string ?? "now") But I can't think of why you'd want null ... null would apply to all types and have a dedicated branch, no matter what any other type is. Robert Landers Software Engineer Utrecht NL