Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122715 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 ABAB51A009C for ; Thu, 21 Mar 2024 19:03:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711047837; bh=1Jxv34cyhPpLtvb1VCaY/hQjMLEq72cKeeLKvtAydGc=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=iCf4C+lvazVZRul/5p9crQb6tHub2Yh/+CYtSW/wEVVoT6ZvSuHpvATx+XUmlJTpu W7Ccd6btu/pB3x87UVcbB81q+C+ZshISrlp8/mwvbnQ2p7qDmWJmc5vq0sYMPXUvPk zEkLKarDMIPp/oC/T/MKeEdgtQGtHb4CMez0WbMj+KQC+zotef635YWOOQ8P2BoXFV U9wWHcF+yXnWY+vhh2uYU+jav+rlWpUdwHWwJOzygQdCbL/K19VhiyIPX143p5L53K X4/uRUQF7xDaf48lq5CnA8+ppxOHU7qy2n3DaWA/ZnPglxXTqDrlIxrO02IEkj0twq gBO/71S6Uqnpw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 442E718007E for ; Thu, 21 Mar 2024 19:03:57 +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-oo1-f45.google.com (mail-oo1-f45.google.com [209.85.161.45]) (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 ; Thu, 21 Mar 2024 19:03:56 +0000 (UTC) Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-5a470320194so684687eaf.3 for ; Thu, 21 Mar 2024 12:03:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711047814; x=1711652614; 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=1Jxv34cyhPpLtvb1VCaY/hQjMLEq72cKeeLKvtAydGc=; b=eb0Gi7BYZXxIRA2TFrCBa/MQ1UeSuZTON0DTuPFsEGhJdii9Esvgin9YfPoSWoDPIj TTD4t5lcuK1N2o42H/QkyateiMvcJ7hgCEHlA3ni3nzb0TPjIVlhUXzza/YX31ppaek9 4pYC/zLAWXWbvfoSiHkRI5RE0jFp2sdl9xVYGs725qwfj6uxZFpixrKfarc595MJijaM wgtQn33uitPrdB7UeWuSID0U6Yc8X5PdXkRln2TEuVfo54GPc89WH98RNEZOwnL9T9lm JSx/+jiMgUiJwadqzGuMdVCiAYamS49mM9rV9yvp+zSKZnIETVlzJQkEHH9m7g5XQyza nvhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711047814; x=1711652614; 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=1Jxv34cyhPpLtvb1VCaY/hQjMLEq72cKeeLKvtAydGc=; b=Lg5MmixaxjnUrEKOYNQBS54oUk74eVKXLxapVRAqGLTJ2pj2a2NAqeQ7ACq5hi5t3M LfmRjkLpONFM+hHKkgFtSpjJV135rN2EK9DAZPf5ETE2Fa0kWgJ3vSmFvqBqvJdDPXPz uL4vWD2wZ+gGRGC0WS/Z0PgsK+fHQmazMYqAYJiNcmd+stie4NcBfca+Dcb5nr/lbdaR eeXtEdZm5p8wEM3fBrlIXkBF5fiWZQBIa4iJrD7e24MTg44hZjCL1LdLP3g2jGIxbkBu gioUwV0BV8Xuil9lth2Kn7Zzg7grylsL2EYt5/q+SrAa8Xn0aKXA1AOSGaiFjt0r0jBy ixlg== X-Gm-Message-State: AOJu0YwuAd6KT3DjTWaIUkQHQT79csGBRLxstbPgeXrXnwW8RDfF+058 YyJ5eEDUg2KTqCKTgMXgnleUSrNuONa898HiqkpoTcc5PNbEEYB4g8L5Aiw2lKwGcpbdniOlwXK dzkdgqlCZmaEYHhj+gfFdlG0AA64= X-Google-Smtp-Source: AGHT+IEEUVNJHsJg8PMVfaX3CqpD2Mi2mc1WH9LRgfT46VKHsdQVJbBeJKhRSJt8BM0fe3yz9XT3jzaa5VyZTkYWaIA= X-Received: by 2002:a05:6820:1f06:b0:5a1:c19d:bd39 with SMTP id dl6-20020a0568201f0600b005a1c19dbd39mr436976oob.3.1711047813996; Thu, 21 Mar 2024 12:03:33 -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> <40d5c22f-5c06-403d-afc2-84c202277a29@rwec.co.uk> In-Reply-To: <40d5c22f-5c06-403d-afc2-84c202277a29@rwec.co.uk> Date: Thu, 21 Mar 2024 20:03:22 +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 Thu, Mar 21, 2024 at 7:01=E2=80=AFPM Rowan Tommins [IMSoP] wrote: > > On 21/03/2024 15:02, Robert Landers wrote: > > I don't think you are getting what I am saying. > > $a as int|float > > would be an int, float, or thrown exception. > > $a as int|float|null > > would be an int, float, or null. > > > I get what you're saying, but I disagree that it's a good idea. > > If $a is 'hello', both of those statements should throw exactly the same = error, for exactly the same reason - the input is not compatible with the t= ype you have specified. > > > > Another way of thinking about is: > > $x =3D $a as null > > What do you expect $x to be? > > > The same as $x inside this function: > > function foo(null $x) { var_dump($x); } > foo($a); > > Which is null if $a is null, and a TypeError if $a is anything else: http= s://3v4l.org/5UR5A > > > Regards, > > -- > Rowan Tommins > [IMSoP] I suppose we are taking this from different viewpoints, yours appears to be more of a philosophical one, whereas mine is more of a practical one. $x =3D $a as null; (or any other value, such as true|false) appears to have no practical purpose in this particular case. This is better checked with `=3D=3D=3D`, o= r even in_array(). Values are easy to check in PHP and there are already lots of great and simple ways to check a value. Further, reading "$x =3D $a as null", as a native English speaker, appears to be the same as "$x =3D null". As I mentioned in the beginning, I see this mostly being used when dealing with mixed types from built-in/library functions, where you have no idea what the actual type is, but when you write the code, you have a reasonable expectation of a set of types and you want to throw if it is unexpected. Right now, the best way to do that is to simply set a function signature and pass the mixed type to the function to have the engine do it for you; or write out a bunch of instanceofs when that isn't worth it. However, this is cumbersome. I'd also like to say that I'm not strongly attached to the |null behavior I'm proposing, but there are better ways to assert a variable is equal to a value. It makes more sense, from a practical programming point-of-view, to simply return the value given if none of the types match. Robert Landers Software Engineer Utrecht NL