Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121820 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 90865 invoked from network); 28 Nov 2023 09:55:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Nov 2023 09:55:13 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C03A8180038 for ; Tue, 28 Nov 2023 01:55:19 -0800 (PST) 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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) (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, 28 Nov 2023 01:55:19 -0800 (PST) Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-a0bdf4eeb46so378131266b.3 for ; Tue, 28 Nov 2023 01:55:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701165310; x=1701770110; darn=lists.php.net; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=01PB3ubGBFQA5AfcLVX4LzvwPs2X3NJ2vg/SE2Ddq0o=; b=nCKnsEK3a5XLhO2D9EzFqR750sb/6rVLR+9++EgrNzUAvodqZHApMxLNR8iqIz8iIk VtiuaOkQ0BvkGl579e8Efhc/wSd74Tn/R3y29nk5T5467kLmos9mQlMDfPX1CWDJg1Jk Hu16UpwvMxPFWZlLIo+E9ZZoUxUOir7Mh1bXM3TVCFRYIZNUgi6wr1laxJ8WMXQRsvXH FE5QmqWF0p1oSxm0xgTi+hi+Jbd2P0IgfQwlZRNEFin8oqolb9JDaN8aDx14JbkFvxL5 JxTOX2QeVQ6dMqGpy0fFGA1pc8Ob7JZgN07nZkBEf2dRkkEeeWKW2ziSyOG0geHzw8x9 3Q/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701165310; x=1701770110; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=01PB3ubGBFQA5AfcLVX4LzvwPs2X3NJ2vg/SE2Ddq0o=; b=R4azx7Yre2xBY7hloLtS7iUUONLL0E3zJjpFdapQUClx7EeAKIVgyAuhryalo9WPTV OSHqeLcyghxUT69qIPe+nuF2y2g9r+h+j708JUTaPZLMlu+1c161MjxyoWRiT+baiqNK mtOUOPYMYO6t7QeTrIkSlt2+ypQ04Rflkx5WxCx0tsgzthbEqtpW0cSC1u5D7YvcapGv eiirxTBBrHWkU4KBdu8k12dWQyP3es2B8hdxc4q/PXGVBStzlxjrsgVlYCMxcv2Q5Byn nGzwUjcbmxWDis/znExCknrViHZrRhwVzoM3kPl1Go4rAfmu/KPUBEPtRIdBYydXgfbU PoIQ== X-Gm-Message-State: AOJu0YyyGDY8Ir6R7xtWMbJxVgqulopl03FtFLSyCIwdjEA5TTU7zUOC KcbUqKuI4QlAnzqCCNIDwqyQMNBnZcg= X-Google-Smtp-Source: AGHT+IF2X2jrHzwnoELFosoa936oIQbPvrCNlXR9FPSdaKj1XG1AUj/fsaLHwWzFlX6oHXkp2+Qx5g== X-Received: by 2002:a17:906:c41:b0:a02:89cf:e8fd with SMTP id t1-20020a1709060c4100b00a0289cfe8fdmr10154023ejf.57.1701165310173; Tue, 28 Nov 2023 01:55:10 -0800 (PST) Received: from smtpclient.apple ([89.249.45.14]) by smtp.gmail.com with ESMTPSA id d9-20020a170906370900b009dd7097ca22sm6638157ejc.194.2023.11.28.01.55.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Nov 2023 01:55:09 -0800 (PST) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_D81AB27D-8FAB-4ECA-B287-AE4D8DF528E8" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.700.6\)) Date: Tue, 28 Nov 2023 10:54:58 +0100 In-Reply-To: Cc: internals To: Sergii Shymko References: X-Mailer: Apple Mail (2.3731.700.6) Subject: Re: [PHP-DEV] Callable arguments cannot have default value From: claude.pache@gmail.com (Claude Pache) --Apple-Mail=_D81AB27D-8FAB-4ECA-B287-AE4D8DF528E8 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Le 28 nov. 2023 =C3=A0 00:59, Sergii Shymko a = =C3=A9crit : >=20 > Hi, >=20 > Wanted to bring up an inconsistent behavior of callable arguments = compared to arguments of other types. > Callable argument cannot have a default value (tested string or array = types - both are not permitted). > The same exact value works perfectly fine when passed dynamically, it = just cannot be specified as a default. > The workaround is to remove the type annotation which is obviously = undesirable. >=20 > Here=E2=80=99s an example: > declare(strict_types=3D1); > function test(callable $idGenerator =3D 'session_create_id') { > $id =3D $idGenerator(); > // ... > } >=20 > The function/method declaration above produces the following error on = all PHP versions: > Fatal error: Cannot use string as default value for parameter = $idGenerator of type callable in /tmp/preview on line 4 >=20 > Note that the exact same string argument can be passed without any = issue: > function test(callable $idGenerator) {=E2=80=A6} > test('session_create_id=E2=80=99); >=20 > Is there a specific architectural limitation causing this that's = hard/impossible to overcome? >=20 > I=E2=80=99m aware that class properties cannot be annotated with = callable - another unfortunate limitation. > Callable is not a real type like other primitive types which causes = all these inconsistencies, correct? > Callable properties (separate topic) may be a challenge, but can at = least argument defaults be supported? >=20 > Regards, > Sergii Shymko Hi Sergii, The big problem with the `callable` type, is that it can be check only = at runtime. For instance: ```php function foo(callable $x) { } foo('strlen'); // ok foo('i_dont_exist'); // throws a TypeError ``` Another complication, is that a value of the form `[ $class, = $protected_or_private_method ]` may or may not be callable depending on = whether the method is visible from the current scope. In other words, contrarily to all other types, `callable` depends both = on runtime state and on context. Therefore, an argument of type `callable` cannot have a default value, = because it is not known in advance whether the default value will be = valid when used. For the case of class properties, see = https://wiki.php.net/rfc/typed_properties_v2#supported_types =E2=80=94Claude --Apple-Mail=_D81AB27D-8FAB-4ECA-B287-AE4D8DF528E8--