Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122724 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 98FF61A009C for ; Fri, 22 Mar 2024 10:05:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711101937; bh=bPwvt+jnEPT+rAgUrdXI7S0VqvMuAOV6wnLioXzJ0M4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=jXWPGuH9gUnBtSiJOd5FIaw6naGR1ShVXPiWqkA00Ddc3Okgbv5MRwqDRBVKTUYDe v3OOgJlezuBMk96lVOXavEq54PQj9Ja7G6VdhdYs09I2XGZHVgpxaArN5A2AUgEG7q Jz8/R5o4xhdIkYGtK9wf3VYhp2Owd3IJTFQ1GGz7tE0YzHLuzUSnQJSwebllUOc90v ECG2Mtb2IEQkP69TOb649kjIMOVPutqGasWW+cIIyps8+ok5p4Is/LnK5ioRsVzhKX Z0JVdR6go0VjZfSxf7CgK4Jirv0etH/3slgW2m1HjoDwSL5KYY8BvOY8pAki9l+Nva WYV+OVkeCVmng== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0DB6F18006C for ; Fri, 22 Mar 2024 10:05:36 +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-f45.google.com (mail-oa1-f45.google.com [209.85.160.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 ; Fri, 22 Mar 2024 10:05:35 +0000 (UTC) Received: by mail-oa1-f45.google.com with SMTP id 586e51a60fabf-229a90b7aa9so957131fac.1 for ; Fri, 22 Mar 2024 03:05:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711101913; x=1711706713; 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=13Y8sM+FJuUwngnjoqxzxlXLMVbYTpG50L2ANr53e7I=; b=msU4aX701K+CYDFLKs9pGC1OkQcQMEHPWGAoMdt7ZC/7PJzN0jW86Yrb93f1xvLUqI hBekgldEzh7Zogjz1rSvaudXD0txTNMOunn9uSyZf5X9V60xOS4Sv6tgifiYU+bn5WW3 rGhDGWHbn2cgH7qJ+HzdW+VE3mS+i6d9W9fdT5s6e+6RzMlT+/sL+AtVxH0VI/7mQaIE DpArsUZTO9I4o5TGParah7ChufrWVr0a3Sa9VczMdKUjRxcFqJu/rn82X2jwbaFTyflS 1Nyld7KkgkWhIgrmSQuFAHcvA80twXs62kyIVnithZ+nfmicTYnp+W6P1HQ6yPBddTeK l4aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711101913; x=1711706713; 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=13Y8sM+FJuUwngnjoqxzxlXLMVbYTpG50L2ANr53e7I=; b=n+h9T6uORwqU9TTNPanMJ72wKsJ9MTJ7jo85O04g4Qk/25m4COsfg8DZbiAml/S1LY /dyXZi1rJZdT8gkdmt+uZVSHR7aMrbOo3Al3XBqdxqVh1Y2B64UTpLwSa7nbsuSVOKci dJtl66q84WaTxdwcbW8vUWxfocT8uVN1tRDwHYI88kCMBTBT6U6pH9lFsu44u/EPcvz+ Ydepc+s+64b8/AuOi5l1kMTJewT7l5IR7kp9J1d9dZ7BIOJ1FMS1reGzjo6In2V+GfEM ON45rZebpEAuv/JyLDiUOSpqRpsTCQIs4QEUyxCMTwFSkmUzTHR8BPnqdYOrNc50qw5I PKYA== X-Gm-Message-State: AOJu0YzznnCk4mmz4ffWcueIq8JT4iptN1Isvj3Ot+ZAkiFZUnZN0VHN dH89fR0Jg7iN3Vk2ATuaQabsRQ2cXXyfAxNmLtzkB3A/xu5a9NxrEfyqIiwjz8wKsSlOFLpgr5U hgsr3RptTcGEIu3EUPwXNweSop8SqnJ+HyZI= X-Google-Smtp-Source: AGHT+IEaZaaRxZmMmEeW1Qem7F9IGXZcTRTquoOKPR/kKM9YSftUcdl8ofj53G8mR2M7oU+KvdePugUga7ajdAftDQ0= X-Received: by 2002:a05:6870:c348:b0:220:b713:77c1 with SMTP id e8-20020a056870c34800b00220b71377c1mr1865979oak.31.1711101912699; Fri, 22 Mar 2024 03:05:12 -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> In-Reply-To: Date: Fri, 22 Mar 2024 11:05:01 +0100 Message-ID: Subject: Re: [PHP-DEV] Proposal: AS assertions To: Jordi Boggiano 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 10:31=E2=80=AFAM Jordi Boggiano wrote: > > On 2024-03-21 16:02, Robert Landers wrote: > > $a as int|float > > would be an int, float, or thrown exception. > > $a as int|float|null > > would be an int, float, or null. > > Just a suggestion here which might be more palatable to Rowan's wish for = consistency (which I can totally relate to): > > 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 > > To me this reads way more intuitive what will happen, and achieves the sa= me in an also very concise way. > > The only catch I see is that it would also swallow errors about $a not be= ing defined at all. > > Best, > Jordi > > -- > Jordi Boggiano > @seldaek - https://seld.be Hey Rowan and Jordi, I did a bit of research into other languages to see how they handle "as": C#: as never throws, it either returns the type if it can be that type, or = null OCaml: fails if an alternative isn't given Typescript: doesn't actually do anything, just hints the type for the compi= ler After asking an AI for some examples and usages, the most compatible one would be C#'s. In actuality, I think it could be hugely simplified if we simply return null instead of throwing. There'd be no special case for |null, and it would move the decision making to the programmer: $x =3D $a as int ?? throw new LogicException(); It also still allows for concisely making calls: $x =3D ($a as MyType)?->doSomething(); What do you think?