Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:68771 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 29471 invoked from network); 31 Aug 2013 00:01:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Aug 2013 00:01:23 -0000 Authentication-Results: pb1.pair.com header.from=bobwei9@hotmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=bobwei9@hotmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain hotmail.com designates 65.55.111.104 as permitted sender) X-PHP-List-Original-Sender: bobwei9@hotmail.com X-Host-Fingerprint: 65.55.111.104 blu0-omc2-s29.blu0.hotmail.com Windows 2000 SP4, XP SP1 Received: from [65.55.111.104] ([65.55.111.104:6424] helo=blu0-omc2-s29.blu0.hotmail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6F/B5-00443-15231225 for ; Fri, 30 Aug 2013 20:01:22 -0400 Received: from BLU0-SMTP303 ([65.55.111.72]) by blu0-omc2-s29.blu0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Fri, 30 Aug 2013 17:01:19 -0700 X-TMN: [oyX52Z3eoEntr5N8IWkguU6kh3u2hFXX] X-Originating-Email: [bobwei9@hotmail.com] Message-ID: Received: from [192.168.178.42] ([87.240.210.93]) by BLU0-SMTP303.phx.gbl over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Fri, 30 Aug 2013 17:01:16 -0700 Content-Type: text/plain; charset="iso-8859-1" MIME-Version: 1.0 (Mac OS X Mail 6.5 \(1508\)) In-Reply-To: <52212D25.6070707@sugarcrm.com> Date: Sat, 31 Aug 2013 02:01:12 +0200 CC: Developers PHP Mailing List Content-Transfer-Encoding: quoted-printable References: <5220CEDE.8080600@sugarcrm.com> <5220DA50.9020306@sugarcrm.com> <522113B1.8000605@sugarcrm.com> <52212D25.6070707@sugarcrm.com> To: Stas Malyshev X-Mailer: Apple Mail (2.1508) X-OriginalArrivalTime: 31 Aug 2013 00:01:16.0471 (UTC) FILETIME=[36B80C70:01CEA5DD] Subject: Re: [PHP-DEV] [RFC] Argument unpacking From: bobwei9@hotmail.com (Bob Weinand) Hi! Am 31.8.2013 um 01:39 schrieb Stas Malyshev : > Hi! >=20 >> function short (...$args) { >> if (count($args)) >> return long(...$args, "some value"); >> } >=20 > This is exactly the problem. Since $args has undefined number of > arguments, there's no way to determine where "some value" ends up. = Which > means it's impossible to understand what's going on here. Now, if we = had > named arguments, like python had, then using "some value" as a named > argument might work (and we'd need to see what to do with named > arguments in this case), but as positional argument this just doesn't > make much sense beyond some very esoteric things that nobody uses > directly, without wrapping libraries (like partially applied = functions) > - at least in PHP. That's why there is an if (count($args)) (I just forgot to add an =3D=3D = 7) It should read: function short (...$args) { if (count($args) =3D=3D 7) return long(...$args, "some value"); } And when you name the example with the "esoteric things", I'd like to = have them in the most readable format possible, which isn't achievable with = func_get_args(). >> And I think you are really arguing about non-issues. >> Example: Multiple uses of unpacking syntax makes sense when you call = a >> function with a variadic parameter: >>=20 >> function variadic (...$args) { >> // do something >> }=20 >>=20 >> variadic(...$array1, ...$array2); >=20 > Again, since parameters in PHP are positional, they have meaning > depending on position. If you just wanted to pass an array, pass an > array, you don't need to use variadic syntax for that. Variadic syntax > makes sense only if positions there have meanings and you want to give > specific meanings to specific arguments in specific positions. In that > case, ...$array1, ...$array2 doesn't work since you can not have > meaningful positions. Only case where it works if you do functional > operations like partial application, where the meaning of the function > is not important but only the fact that it is a function is important. > But I don't think we need special syntax to do such things. I mean, it looks way cleaner to use this instead of: call_user_func_array("variadic", array_merge($array1, $array2)); And no, the order isn't always important. Example: function variadic (...$args) { return array_reduce($args, function ($a, $b) { return $a*$b; }, 1); } Yes, this is a very simple example, but just to show the idea of it. And even when the order is important, it may be useful: // according to implementation, arrays are expanded in insertion order = here // https://github.com/nikic/php-src/compare/variadics...splat#L11R3263 $short_default_options =3D [ "arg1" =3D> 1, "arg2" =3D> 3, "arg3" =3D> 7, ]; function short (...$extra_options) { global $short_default_options; // in classes, this would be a = $this->short_options if (count($args) <=3D 3) return long(...$short_default_options, ...$extra_options); } function long ($arg1, $arg2, $arg3, $arg4 =3D 0, $arg5 =3D 0, $arg6 =3D = 0) { // do something } > --=20 > Stanislav Malyshev, Software Architect > SugarCRM: http://www.sugarcrm.com/ > (408)454-6900 ext. 227 Bob Weinand