Replacing the … wildchar in configuration files

David Grudl
Nette Core | 8227
+
0
-

The ... is used in the configuration file to skip a parameter that DI then autowires:

class Facade
{
	function __construct(Database $db, int $id)
	{
	}
}
services:
	- Facade(..., 123)

	# alternative way:
	- Facade(id: 123)

This wildchar was created ten years ago (yes, DI is really that old). At that time there were no variadics parameters to be confused with. PHP 8.1 comes with a first-class callable syntax that looks exactly the same but means something completely different.

Now the three dots symbol must be changed to something else.

I suggest using either -- or * or @:

services:
	- Facade(--, 123)

	- Facade(*, 123)

	- Facade(@, 123)

Single - has special meaning in Neon, cannot be used. The * is common wildchar. And the @ already has meaning as the service reference.

Update: added _:

services:
	- Facade(_, 123)
jspetrak
Member | 15
+
+7
-

Kotlin uses underscore _ character as placeholder for parameters you don’t care that are passed, at least in lambdas.

Rick Strafy
Nette Blogger | 81
+
+6
-

I didn't even know about that syntax, is there any real use-case where is Facade(..., 123) preferred over Facade(id: 123)? Btw, * seems more intuitive.

//EDIT: I thought * will escape all previous parameters, if the usage is that it will ignore only that one parameter, then _ is the way to go Facade(_, _, 123), although I think it should be deprecated, since id: 123 is more readable, and relying on the parameter order is just stupid idea.

Last edited by Rick Strafy (2021-09-21 16:39)

Milo
Nette Core | 1283
+
+6
-

Wouldn't be better to deprecate the ... at all in a favour of named parameters Facade(id: 123)?

Jan Tvrdík
Nette guru | 2595
+
+4
-

Both Scala and Kotlin use underscore.

services:
	- Facade(_, _, 123)

But in most cases Facade(id: 123) should be preferred.

David Grudl
Nette Core | 8227
+
+3
-

Named arguments mean you rely on name constancy, dots mean you rely on order constancy. Both have a sense imho.

vojtamares
Member | 26
+
+3
-

Disclaimer: I write some PHP for Czech Scouts in my freetime, my day job is Go and YAML.

From the perspective of PHP, it has support for named arguments since 8.0, IMHO it is the “native” way" to solve this.

In Go (and other languages like Rust if I remember correctly), underscore _ is used for dropping a value while declaring multiple variables (e.g. function returns a value and an error, we care only about the error atm.) or dummy imports.
Therefore underscore is IMHO acceptable for this case.

  • * is no go, since it's a common wildcard as mentioned before
  • @ refers services in neon and has a functionality in PHP itself → no
  • -- it is just weird, for me same weirdness as proposed attributes with @@ prefix (pls don't)

TL;DR

  • named attributes FTW
  • _ is ok
Felix
Nette Core | 1245
+
+1
-

My 2 cents.

  1. _
  2. named attributes
  3. *
srigi
Nette Blogger | 558
+
+7
-

Maybe inspiration from EcmaScript 6 world could be valuable here to:

services:
	- Facade(, 123)
Slava.Aurim
Member | 19
+
+5
-

I vote for the consistency of the neon-config syntax with other PHP constructs. Compare:

[,,$banana,$pear] = fruits();
services:
	- Facade(,, 123)
boschcb
Gold Partner | 3
+
0
-

I vote for named attributes or simply nothing like Facade(,,123). Why there shloud be some wildcard character?

trejjam
Backer | 65
+
0
-

I wote for the _, C# use it also for “do not care” parameters.

Pavel Kravčík
Member | 1195
+
0
-

(,,123)

Arziel12
Member | 1
+
0
-

_ also used in Rust lang as “ignore” operator

David Grudl
Nette Core | 8227
+
+3
-

So I used the character _ that got the most responses https://github.com/…a719326ab6d8