Re: [VOTE] Named arguments
| From: | Manuel Canga | Date: | Sun, 12 Jul 2020 14:05:45 +0000 |
| Subject: | Re: [VOTE] Named arguments | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-110965@lists.php.net to get a copy of this message | ||
Hi, Rowan,
---- En dom, 12 jul 2020 14:19:08 +0200 Rowan Tommins <rowan.collins@gmail..com> escribió
----
> The problem with this, whether built in or not, is that you have to express everything with
> strings rather than keywords. That means, for instance, that invalid values in the definitions
> themselves will only error at run-time. The definitions also tend to get verbose pretty quickly.
>
> To make it concise and checked at compile-time, you need it to be a construct that pairs
> identifiers to types without first representing them as strings, e.g. to write
>
?string $foo = 'hello' rather than
> 'foo' => ['type'=>'string',
> 'nullable'=>true, 'default'=>'hello']. It turns
> out we already have a construct for doing that: function signatures.
>
> If the input is an actual array or object coming from "outside", you're
> probably going to want a more extensible validation system anyway. Those are really hard to design,
> and probably best left to userland where people can choose the tradeoffs they prefer.
>
> Regards,
>
I see.
However, with named arguments you also have to implement changes in parser in order to can recognize
named arguments. You also have to change behavior about default parameters. I don't know if
this change affect to performance, maybe do.
By the other hand, editor like PHPStorm has "Inlay Hints":
https://www.jetbrains.com/help/rider/Inline_Parameter_Name_Hints.html
With a function which checks scheme, you also could use in other contexts, like forms:
```
$scheme = ['name' => 'string', 'surname' => 'string',
'?age' => 'int');
$is_valid = array_check_scheme( $_POST, $scheme );
```
You can use it even with JSON:
```
$request = json_decode( $request_json, true );
$scheme = ['name' => 'string', 'surname' => 'string',
'?age' => 'int');
$is_valid = array_check_scheme( $request, $scheme );
```
> e.g. to write ?string $foo = 'hello' rather than
> 'foo' => ['type'=>'string',
> 'nullable'=>true, 'default'=>'hello']
Maybe you don't need checked at compile-time. is it slow?, securely, but you don't use in
everywhere.
> you're probably going to want a more extensible validation system anyway.
Maybe, in that case, userland libraries have a starting point. With XML parsing is the same. PHP
provides basic functionality
and userland provide advanced.
Regards
--
Manuel Canga