Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126309 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 B62A71A00BC for ; Thu, 6 Feb 2025 14:48:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1738853146; bh=yfOJsHDuoz+8CW8Nl5U1Y0VdX/nr5NyC9quOyIm+GU0=; h=References:In-Reply-To:From:Date:Subject:To:From; b=UH6LDGEsvJZ9WXgL+XHA6k0E9PTEVw4ddD4s+S6F9FKtHeN4eRNgPa6CWItU+R4pa G5ykwmn/QaF2A5wBJTl7GU3hTnXEbPJOWogSY5SqiB9CGso+mb5cscJrIR0BMT8503 VN1KGFLpI5VwD2RwVJ6JcLZGfdZAJjs0uCQTM4f6+80G1F/GYgw42wc0tDYrtm/6W3 cN+oFD2tqFQS09P8LuLdAwvYCS++exjN/2uMvxIlhstq6Cq4mlmEaB4aKiu/wHyhfV esODrSPbrux6sivs5i++B56y11JR7Wy5EtUD7xdfh7Ke7QxkoqaHlkTwhx0ZLqArqR mZpV+4SSoW0gg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8B2161801DE for ; Thu, 6 Feb 2025 14:45:45 +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=-2.0 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-qv1-f41.google.com (mail-qv1-f41.google.com [209.85.219.41]) (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, 6 Feb 2025 14:45:45 +0000 (UTC) Received: by mail-qv1-f41.google.com with SMTP id 6a1803df08f44-6e440e64249so4067626d6.3 for ; Thu, 06 Feb 2025 06:48:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738853309; x=1739458109; darn=lists.php.net; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=yfOJsHDuoz+8CW8Nl5U1Y0VdX/nr5NyC9quOyIm+GU0=; b=LDdEJu3Z0r3rxktbKZB/rVjAVIgc+3IpcXz5gAxmBGSv/SmxnFQr2UFrINXZjqhj9+ LlXwOvIPKdYLet6ksFblWtqyp6gzOmUFa6dXNM3j4FJfiph8nIGbzWvkANVqfGHCQJrZ herR6fvVcnhxl3CPTtJN+EFyFJvV2QZSfXdfC6UgjuLzxX7xlsF3cfSjB8bf1yrKkt9B sUCAu46i4X+6DBpEMWwe57PxsR/SexYsTH8bQi17yUC/dt4hN8GMuTBvm0Pl0coZC+Rz fG+dasCBwbouDWE/CsmubcIoQNuK2xNfZMAjd0EUq1tC3caud6QNXahm3CVl+f+A1HJ7 sKGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738853309; x=1739458109; h=content-transfer-encoding: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=yfOJsHDuoz+8CW8Nl5U1Y0VdX/nr5NyC9quOyIm+GU0=; b=bWLfERJRo5oxrKu+l+Vi+jffWV6OAEpaN0swFPF3VF+wG4s2YNC/K4Ouwf01AI2PyZ pvVouDYrdWsjO9IJbk/X0P0+xONrkdhg98pDMw4tSc5Fq9OI6Sd54dlpk1MPz9kHuGsp jhhGjjnm2cAkD+QLZzB1ylAHIV9L1A0+ZO00jOw/dU7F3QHi80J+OwmbgZESQXuEJvpa l0YjJKBuETg7jzgmMt2fCWFI+gDhh9N8Wl7LG62nID5zVMVhrsPC6yrDpsty8APrv8dt gXFaS6zznwydBjUyWS+PDufWiLTyj5Xk3azWG08zNq8+ozIyXVu2/7iZclG6ku8eIbQi pAPA== X-Gm-Message-State: AOJu0YyGbqtPLkeHUJGgl43XWj4UAPW/jd2enCwNpIkZkg7sK2VZRISz 9rqgsdujvT2Glsh30yhvWP1Td3s+jlLlQt2C4Ax6j/n7F1/b5ZHlLl+d5fkCJe23zc1cGCCx1O4 H2KBqoCY1GqXBYXtevnDhrNfVcXHpwBEkLPnenw== X-Gm-Gg: ASbGncv40C6qBQLAnj3rmIubI57KzZT2YAnEBS3jM0YPJprgiE8n4dFIqAN/VvtYVQC xdEiE4ZRm0uUg/JKVqLn8sChj6JLO8DZdePH8BX1iPc5P89IkaVHG79kJtoXUjtbysBKUeW9wzz x+HAdfrC6KMGxpDA0Tdglr+maGPHwgigc= X-Google-Smtp-Source: AGHT+IFB/xfVK9FERsls6p7mY/xTWIO8sCF1Jw/VqQnKwPHaZrA4O3gLZFSXMuv3pB3rcpEj6uMgOjzWl+57/PMldrM= X-Received: by 2002:a05:6214:21e3:b0:6e2:497f:1ec7 with SMTP id 6a1803df08f44-6e42fc9d752mr90863286d6.41.1738853309450; Thu, 06 Feb 2025 06:48:29 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 6 Feb 2025 15:48:18 +0100 X-Gm-Features: AWEUYZnpO5vL0jJHh_SmArRw0F8sr30XKZTb-ALCbx99K3mAnDhn_Tw6WfkLSyI Message-ID: Subject: Re: [PHP-DEV] RFC: Not Null Assertion Operator To: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Mihail On Thu, Feb 6, 2025 at 1:51=E2=80=AFPM Mihail Liahimov <91liahim@gmail.com>= wrote: > > Examples of potentially using in PHP: > Without this operator we writing this code: > > $foo!->bar > $foo!->someProperty->method(); > $foo!->someProperty->anotherProperty!->method(); > > I think the postfix operator would be illogical in PHP because my operato= r > is similar to the existing nullsafe operator in syntax. And it would be > strange if its syntax were different. > Or we can implement both operator syntaxes: prefix for accessing > properties, and postfix for use with variables, as in your example. > > Nullsafe: > $foo?->bar; > Not null assertion: > $foo!->bar; With a postfix operator this would all work automatically, assuming that $foo!->bar can be parsed correctly as ($foo!)->bar. The, $foo! will check that $foo is not null, and if not, access the property bar on it. > This operator will be useful in cases where a null value in a specific > place will violate the domain logic. Usually we write either assertions o= r > checks and throw our exceptions for this. But it seems to me that the not > null assertion operator will help avoid writing unnecessary code. The > problem, of course, will be catching errors. It is not clear how to catch > errors by a specific value. I will think about it. I think, if such an operator were introduced, it should throw an Error rather than an Exception. Errors should generally not be caught, as they indicate logical errors in your program. There are two things to consider: * Is assert($foo !=3D=3D null); $foo->bar; really much worse? I understand it requires two statements, so it cannot be used in all contexts. * Are we ever going to promote the (null)->prop warning to an Error? If so, is there still any appeal for this feature? E.g. it would automatically work on array access ($array!['foo']). Array access on null may be more controversial to promote to an Error. There are some other potential contexts, like foo($bar!), but you could argue this is already covered by parameter types. Ilija