Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128281 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 lists.php.net (Postfix) with ESMTPS id C705D1A00BC for ; Mon, 28 Jul 2025 18:31:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1753727400; bh=6zrKnfbJIEa83J/6y9ANzXqDdHfWphv4U/PnF3vfCXc=; h=Date:Subject:To:References:From:In-Reply-To:From; b=g0XqZkNmrdPUaRHlD+3wQn3Egz2PfG3kg84Ox0pEFrxhRJ7mu3Gu455bpQkKz2Rdz 42MCR5E9C0GMIzoy6aGiEqKgnLzYhTSRHEQy2ps9aNrcAUbdVctfY+EXZDJ8TPdvA7 jbM3qB9pKgr45PYrW0W17dSdkG3ZmYQYNrfkH4XUUFJuH0cTmLjT8IpvR6qhXftr7f r8J3jcURfrIMdQka9huw7qrNmL6E8fnjb8dszCdA2QjKr/in1UABomsTa6yqTu11rf RLmwsbL9vhTZKfCjCsdvcTPv7n9NdOwKwMmfhdkLFbBAS1+dh4pQ3aJWBlfJk2bafP Dn/eOyqTZIeVA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8AFAC18003B for ; Mon, 28 Jul 2025 18:29:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fout-a8-smtp.messagingengine.com (fout-a8-smtp.messagingengine.com [103.168.172.151]) (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 ; Mon, 28 Jul 2025 18:29:49 +0000 (UTC) Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfout.phl.internal (Postfix) with ESMTP id E937AEC20B6 for ; Mon, 28 Jul 2025 14:31:31 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Mon, 28 Jul 2025 14:31:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1753727491; x=1753813891; bh=T47InauP/0jC0FxIB2yjaq2EQkGHQAXEHxsW8mYghb0=; b= FYHUwxi7K2GU32cXIpzCbSvyjKUbaUsiHfvmC1bEI9lddCiovDY3kBZcU0hfvqu6 u8+I+n+QB9tNL14MLu6t71qmIerHCZs4CJ0WjF3hEqlx74doxocggJ365bARw7vZ g3p8zM4FTl8PGUxXCMq8WdhpDhGaG0/gZ9elHyR3a86RCTCKUMKXEekCe/X2zfog Af+8s1xtZ2UWWbEJZoHKQz2hFnX72lFDDEk2m+2/oi00Vz8Bqv9lr/CbwmCbdyi9 gAcp0HW1ly+VLFrQEQBBuVu5o4pMkzj3j/BcDYLAnuBtsdrand5IlfS9TG/Kmggv vmcNPEPtXdA0g6DfxGVNkg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1753727491; x=1753813891; bh=T 47InauP/0jC0FxIB2yjaq2EQkGHQAXEHxsW8mYghb0=; b=QogbWM8rCDUIe7Ovo pDegWUrYmHbgY2ESqmNnPZi+RygvgYVTt5+EG0TSqRIqkIqhnIYOb8FnV9dXtAA1 9gnWdy4HlnMhzw33f6nXKhqbDm5vZQTjqdIgGnVQ3Mq3sMM9drTtKrahUigPER3J x5h+l8Z/iu15zza0j4P022Gv7Pf5cUzp1ue66v2L89E5bPMJ4UsHHwUMx2P6s4T/ Igf7UIeRAOedyrjVaT2abwdNyWnOh/F4UCvmfgdNAyAa8x/SFikP37CIQFB/x02Z eR2+NyUd4Fn6TgtaMD6AUfRJZJoBINm3nmPY3bWeyScRsmynFhY4MqPBe+bkTPcR w5pzQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdelvdeltdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecunecujfgurhepkfffgggfuffvfhfhjggtgfesthekredttd dvjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhsucglkffoufhorfgnfdcuoehi mhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecuggftrfgrthhtvghrnhepffekve duffduvdehjedvfeekleeftddugeefheejudehgeeiudffgeeggeevfeehnecuvehluhhs thgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepihhmshhophdrphhhph esrhifvggtrdgtohdruhhkpdhnsggprhgtphhtthhopedupdhmohguvgepshhmthhpohhu thdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Mon, 28 Jul 2025 14:31:31 -0400 (EDT) Message-ID: Date: Mon, 28 Jul 2025 19:31:30 +0100 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [DISCUSSION] Adding the "is_integer_safe()" function Content-Language: en-GB To: internals@lists.php.net References: <75b17924-d0d9-46e9-94d0-9c18d1cb9192@app.fastmail.com> In-Reply-To: <75b17924-d0d9-46e9-94d0-9c18d1cb9192@app.fastmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 28/07/2025 16:32, Larry Garfield wrote: > Even without the precision loss question, such a function would be very useful when parsing input data of unknown type (and thus is a string). "Can I cast this string to an int to use with an int parameter without any loss" is currently not as simple as it ought to me. Providing a single standardized function for that would be most welcome. > > The name is quite long though. 🙂 This is basically where I was going with "can_lossless_cast". The reason I went with a generic-like syntax is because it makes it exntendible to any representable type, without an ever-growing list of long names like "is_representable_as_int_or_float". I also see it as part of a wider set of missing cast functions; for any value V and type T: 1. Is V of type T? 2. Can V be safely converted to T (for some definition of "safe")? 3. If V can safely be converted to T, give that result; otherwise, throw an Exception|Error 4. If V can safely be converted to T, give that result, else return null 5. If V can safely be converted to T, give that result, else return a default value of type T Given (5), you can implement (2), (3), and (4), but not elegantly, particularly if null is a valid output, e.g.: ( cast($v, default: false) !== false ) ? cast($v, default: false) : throw new TypeError('Not null, and not castable to int'); vs cast_or_throw($v) I've got a half-drafted RFC around this, but stalled on a) naming; and b) what a "safe" cast means for different types. It seems like both problems have already come to light in this thread. -- Rowan Tommins [IMSoP]