Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129578 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 528A51A00BC for ; Wed, 10 Dec 2025 07:42:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1765352567; bh=fdJn/UEVPiQEA7+djmbFV76PHrStRnHyrMGk0q8mogY=; h=From:Subject:Date:To:From; b=YCEZh/sr6UBKTidEKWB6F4bGqs/LkqC0YvzwsVKJm4FuRIVvnCFjbzmJsSIJ9D4+u wdEj6Qia2DrjE4/1qHYD0U+ACfdauOMiDBltV6OWrc+H+meBT6vWiTaGdXgFc1BSE/ VntjE/VT+KmlMwD75f/6Ynts4PcBJ/Q9xjtxUDGHjcgdtMjta+W9YgmIKfZyMJr6/z ALY8XuNVSJLqgwaqHwuho+LrLvj7RqLMnn6P9qsrj0kZ9MUZWaSwzg7cf8OUQUfc9B DOS+RS+ULr1SlkZgjdvwVDMH/8m6EzR2AnpCPO05PrSUZ3nazIoFJG3ihRArrDN8SD vCapvmd1requw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C9D43180057 for ; Wed, 10 Dec 2025 07:42:46 +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=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 ; Wed, 10 Dec 2025 07:42:46 +0000 (UTC) Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4779adb38d3so60780215e9.2 for ; Tue, 09 Dec 2025 23:42:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1765352560; x=1765957360; darn=lists.php.net; h=to:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=3h4oSrVbcvEIAg3fXanzQDGL72CVoAQt2koXMg67sKU=; b=fbhuvEUAHoXvlLQjgWLn/P1Pvnc3+ZeZAEyA7/ABFpKIw1Q/jv/ZyyGIELfFs0YT4a vo5vm4arCJdMuzBB5GBSQKCoUusUo6Hqb+wL3YhscBiPvocKu3NpBU9Fa7EXWX6kBT2F MwDnAw6HA4SuBdUkxwJ8EIRD3cL8Ab/4NV63gYTXfUncd885p3TPjM+17Wcg80pWg/qC siOJQHRxtWbyQB1vjwCxskGjcOk27y/NiSHeWjbLslL/ZM3xs1TW0/6ygLZUQEWHXnKM pYNPAkO0a2780ZTm3hj9bx/CiBBkGQoR7I3t5CZlI1RYexE6e1q/1J/goBwQ/+opdzne Xcng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765352560; x=1765957360; h=to:date:message-id:subject:mime-version:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3h4oSrVbcvEIAg3fXanzQDGL72CVoAQt2koXMg67sKU=; b=H6tzpAnb6Hk1LDepXEQ2eA+hQv1TsJc5S0oEXVIL0t4XieYP1vtMQZtsBi8ns2LNQi TM990vYtGnk50Pq9rUrxQWFib/T6u5p8clXjmVwMwF+tEaZtLuMn8ccel9YIS8IT//Y5 IXMrDo0STDGGn0dPEFas2VsTH+bSeMmQQHbJc5klTEB+rUhXV7w28haLi+dUaG27qZJn CPvcrIhnvctZoFXL9m/Hs4hXiz6dHgDUwXh/SqEPHq0yehoxsY+w2x6cajrB6TUF4p5k fe0LYWatcFHtk0qVVo7aaTAacrEkuB6Ddvloqtf8kHy61rY2QFceJ3wbLBW3wEAFhv0U uRUQ== X-Gm-Message-State: AOJu0Yxzhs7d21RavL77/ZUNjiFRugUo9gVK1/Szsgm3PctEZyx7jaPY jaizAQVWchzr/zhRlhXhTYhNmYNQAsl1qDIlnzGjStRv5FO/EsZJBk3yGsj6y16I X-Gm-Gg: ASbGnct/DAptTrS0Yjsqq3u3Ya9qLcinfWQk0x9W7bxWTKYVWpiNApAApVtemgjPk53 bWM9vPeYhHmMjb+9vuWzvJH2e1G3JA7w8LjPImMvwfj18c0AiHHc4uK5jTlvhokcnniw0VStJUp 829jIPLhw7uNgmzmBJwiTgktWzybhorQl472IxWlGuAFx6lncRPGow6CpZoY1nbZxMg9OYIQ2Pf ds09iKMFNddDW/kHehQPgVXxgzkyFWupHIEMDkp0pUOIZLcS+RCd/KgzVzd5iv3VXrp6u3Fuhd+ th+TZSU2ymtfuWlHlU41Ny/J1/k/+Yj72bxhPFmlrtns+wFP3k9N8mecAT9Fcdsle1I9xGrWJ3j L4vtt2NLoWUzx8KuduThJRtIbq7UpUnB4CgVDghefNkRwF6gASI7f+ja2o6x4+CfeqFA9LVrfX9 zPiz1YTg/BHhQnQ0IznzKNL+xO X-Google-Smtp-Source: AGHT+IGkZG72su4gM++uCaXTCdeBUEJHPpBJ+NnU72Iw+kzbT2/3a7ZfPv453TV8xr295e04DeFbDg== X-Received: by 2002:a05:600c:c165:b0:45d:d97c:236c with SMTP id 5b1f17b1804b1-47a837ac2d0mr11355675e9.21.1765352559856; Tue, 09 Dec 2025 23:42:39 -0800 (PST) Received: from smtpclient.apple ([178.134.17.116]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-42f7cbfee5bsm35941270f8f.16.2025.12.09.23.42.38 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 Dec 2025 23:42:39 -0800 (PST) X-Google-Original-From: Dmitry Derepko Content-Type: multipart/alternative; boundary="Apple-Mail=_9FF5BF80-E5B8-4299-939A-8854353F2305" Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81.1.3\)) Subject: [PHP-DEV] coerce() function Message-ID: <462A7CF0-71A6-4BFA-B5FE-CDF3E0CCA5E6@gmail.com> Date: Wed, 10 Dec 2025 11:42:27 +0400 To: PHP internals X-Mailer: Apple Mail (2.3826.700.81.1.3) From: xepozzd@gmail.com (Dmitry Derepko) --Apple-Mail=_9FF5BF80-E5B8-4299-939A-8854353F2305 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hello,=20 =46rom time to time I need to write a few lines of code that usually = look ridiculous: $var =3D $input =3D=3D=3D =E2=80=98yes=E2=80=99 ? =E2=80=98yes=E2=80=99 = : =E2=80=99no=E2=80=99 $var =3D $input !=3D=3D =E2=80=98yes=E2=80=99 ? =E2=80=99no=E2=80=99 : = $yes $var =3D $input > 10 ? 10 : $input $var =3D max(-10, min($input, 10) and many other variations. In Kotlin, I can use `coerceIn()` extension function with strings / = numbers / comparables, which allows me to write such lines more = readable.=20 I suggest to add such function to PHP. The examples above may look like = the following $var =3D coerce($input, variants: [=E2=80=9Cyes=E2=80=9D, =E2=80=9Cno=E2=80= =9D], default: =E2=80=9Cyes=E2=80=9D); $var =3D coerce($input, min: 0, max: 10, default: 5); $var =3D coerce($input, min: 0, max: 10, default: 5); For sure, that could be different functions not to introduce another = overloading function. Or range arguments could be replaced with = `range(0, 10)` function. This letter is an intent to start discussion of any possible ways of = developing this idea. If we agree that the function has enough value to be bundled I=E2=80=99ll = create RFC to push the idea further. So what do you think? ---------- Best regards, Dmitrii Derepko. @xepozz --Apple-Mail=_9FF5BF80-E5B8-4299-939A-8854353F2305 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Hello, 

=46rom time to time = I need to write a few lines of code that usually look = ridiculous:

$var =3D $input =3D=3D=3D =E2=80=98ye= s=E2=80=99 ? =E2=80=98yes=E2=80=99 : =E2=80=99no=E2=80=99
$var = =3D $input !=3D=3D =E2=80=98yes=E2=80=99 ? =E2=80=99no=E2=80=99 : = $yes
$var =3D $input > 10 ? 10 : $input
$var =3D = max(-10, min($input, 10)

and many other = variations.
In Kotlin, I can use `coerceIn()` extension = function with strings / numbers / comparables, which allows me to write = such lines more readable. 

I suggest to = add such function to PHP. The examples above may look like the = following

$var =3D coerce($input, = variants: [=E2=80=9Cyes=E2=80=9D, =E2=80=9Cno=E2=80=9D], default: = =E2=80=9Cyes=E2=80=9D);
$var =3D coerce($input, min: 0, max: = 10, default: 5);
$var =3D coerce($input, min: 0, max: = 10, default: 5);

For sure, that could be = different functions not to introduce another overloading function. Or = range arguments could be replaced with `range(0, 10)` = function.

This letter is an intent to start = discussion of any possible ways of developing this idea.
If we = agree that the function has enough value to be bundled I=E2=80=99ll = create RFC to push the idea further.

So what do you = think?

----------

Best regards,
Dmitrii = Derepko.
@xepozz

= --Apple-Mail=_9FF5BF80-E5B8-4299-939A-8854353F2305--