Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122687 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 8F4661AD8F6 for ; Tue, 19 Mar 2024 16:24:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1710865494; bh=mS3jK74gVNVwk4Qm65Ty3oMn4F6My+WPsqMeyKEIBqU=; h=From:Date:Subject:To:From; b=BZjRS0V1Rpm6/MX5aT6wr8opAF2ddLUCxFX79OjK8DtPgAfzEmgYHAmUE9EMCjiKB TiZ0kuXa1BgyJjGIgB7vG5jGUNXgxTSblsz9nH2kc3zoVaEi8f47cYLZzD/oKwqXWd GohVlIzk4Eid3v+hyRZfss7Ik1jnhBs2ouF1G374OuVkBwKohWsYsbbeubYrqleNyI S6eXgW5X7sONg15M3XY5OQw78FNDVCF5bcAHYa+gWJHn8Xpnw3FJdUzIXkPZIDTrtV njjL2SA2M61xqgiaFd7xWSysvXDTdW1puvRpkJpgrv7tuJbM9/UElyVKErrrtryIyN Sl7STou38sRag== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E602B180032 for ; Tue, 19 Mar 2024 16:24:53 +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-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (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 ; Tue, 19 Mar 2024 16:24:53 +0000 (UTC) Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-5a4716cfbbcso3038470eaf.2 for ; Tue, 19 Mar 2024 09:24:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710865472; x=1711470272; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=kzDF+trWFQYyGpaeDqyaLFIpKrW2nUl1zOYwXp4BBxU=; b=em2rdOm7ymfU6WAsYuqjBxSwYAxfgj3fZdHLb/+wqAV66BxI/RboazaLeNFrX6ZPom V0zXsrm/P8RDkzlrzHAbd1eGpR2yPNGsNEjCxeaMCwV7cCXTOWMyedQBNLIeHNc/b8TT cApuEMrDyhi/WOZp2xa6iSMDy6J80niyrZgg2rwYmEVsEkZVtqsFs1+HTi/Y6PvsnYk1 uoN+kVhUE2OOsGACCqchTolqaEik1WV7uzdoNiV8KbEA8ysCHBaP09odWdO5qcMZO8ll wqaHGRAGUXkgVCsz6W3DRiUOpHJBaY3wvDybyvfoAteoW5iXaShJ0Z5Q7AHiWzpaIdKT nncQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710865472; x=1711470272; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=kzDF+trWFQYyGpaeDqyaLFIpKrW2nUl1zOYwXp4BBxU=; b=XUc+1UwZt8tWULa4oB3i4IwlSa7OVVbga7bUwJnu/mw+5+VdP0R0+wDK7iFnLmImKk AerE0GpeZUm2uYicbYf+KMh1FQq0pM3Tj0V8wxEEoxYCLYEyv4/hORDmSm/Y8fruhf9i r52cJWktfeiz4dVSrCsamLQeOiYRh1G01hUB4nQJrGmiHHGJjPpSwZqTnokbmbjSPu9t 3XN0MHu9q3YENGnDHjI1QtuLOAIic8MT3Vt2jNOaLwxCK51GSuDmQH3Lrux19Yknfmc/ x7eNUCjcrkIZ3cIwc0hSI2cG0L2VN/mulJ/qPRfkfkysU1oqzoR6Vyjxfody4vmZJcNk nvCQ== X-Gm-Message-State: AOJu0YzFPvXCzB9UnH92+2V/SFvJef3RuDu7FFb3pmhcSuiYIwLS5nLh nNJsq+AV7KvPpQfu7dNDrvczsTwZJ4joJFArEvYM7mDV4zPqQ1BeWkxaHqhLOeqcORL41ZQEpmU U4hI/c7riC0IJtgwOYbaDHZYJ1eH85JK/Htu0lA== X-Google-Smtp-Source: AGHT+IEZA8KYlwGSVXnLGdsEB6zXAZ8JLK95lyRujaiDUy1XGbAYVDglxnzYyKRbGREplEVh0aZ7vA3N23qV5P3wSng= X-Received: by 2002:a05:6820:212:b0:5a1:bb9d:56a7 with SMTP id bw18-20020a056820021200b005a1bb9d56a7mr3105206oob.8.1710865471589; Tue, 19 Mar 2024 09:24:31 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Date: Tue, 19 Mar 2024 17:24:18 +0100 Message-ID: Subject: [PHP-DEV] Proposal: AS assertions To: internals Content-Type: text/plain; charset="UTF-8" From: landers.robert@gmail.com (Robert Landers) Hello internals, I've been thinking about this as an RFC for awhile, but with generics being far off (if at all), I'd like to propose a useful idea: reusing the AS keyword in a different context. Example: $x = $attributeReflection->newInstance() as MyAttribute; This would essentially perform the following code: assert(($x = $attributeReflection->newInstance()) instanceof MyAttribute); but would work even if assertions are disabled, and would provide some sanity when working with mixed return types, or even dealing with interfaces where you want to be sure you are dealing with a concrete type: class Query implements QueryInterface {} function getQuery(string $sql): QueryInterface {} $x = getQuery("select 1 = 1") as Query; which is more like: assert(($x = getQuery("select 1 = 1")) instanceof Query); It'd also be nice to have a non-throwing version where we simply specify that the type is nullable: $x = $attributeReflection->newInstance() as ?MyAttribute; if ($x === null) // do something since the attribute isn't MyAttribute which is more like: try { assert(($x = $attributeReflection->newInstance()) instanceof MyAttribute); } catch { $x = null } Or a more complex type: $x = $attributeReflection->newInstance() as PretttyAttribute|(UglyAttribute&UtilityAttribute); Essentially, by using "as", you can be 100% sure that the type is the expected type signature, null (if the type signature includes null), or an error to be thrown. Note that this isn't casting from one type to another, but asserting that this type is the type you expect. It'd significantly help with static analysis, IDE code completion, etc. What do you think? Robert Landers Software Engineer Utrecht NL