I think I would argue that this example is not quite the same thing as a Nullable Type. I see the Nullable Type as a special case involving nulls, but not as *every* case involving nulls. I see it as a method signature type hint equivalent of `AnyClass|null`, so that the *signature* can still provide runtime type enforcement of AnyClass on a passed argument, *but* will still accept *no argument* (literal null of course being treated equivalent to no argument).
Your example does *not* allow for no argument at all... you are absolutely requiring an array. So this is not the use case of a Nullable Type. Your example should lean on the Union operator "|" to denote that the values in the required array should be strings or literal nulls: @param (string|null)[] $stringsOrNulls you MUST send me an array, but inside it, your values can be strings and/or literal nulls CRB *about.me/ashnazg <http://about.me/ashnazg>* On Thu, Nov 8, 2018 at 9:59 PM Adrien Crivelli <[email protected]> wrote: > I don't think "?" at the beginning is fully enough for the Nullable Type > use case. > > You can easily express a "null or an array of elements that must be > string" with "?string[]". But how can you express "an array of elements > that must be string or null" ? > > In code that would be: > > /** > * @param ?string[] $strings > */ > function nullOrArrayOfString(?array $strings) { > } > > /** > * @param WHAT SHOULD I WRITE HERE ? $stringsOrNulls > */ > function arrayOfStringOrNull(array $stringsOrNulls) { > } > > nullOrArrayOfString(null); > nullOrArrayOfString(['foo', 'bar']); > nullOrArrayOfString(['foo', null]); // Invalid call ! > > arrayOfStringOrNull(null); // Invalid call ! > arrayOfStringOrNull(['foo', 'bar']); > arrayOfStringOrNull(['foo', null]); > > > I think something like "(?string)[]" would be necessary to cover all null > cases. > > -- > You received this message because you are subscribed to the Google Groups > "PHP Framework Interoperability Group" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/php-fig/c0ba7fdf-5c45-493c-8f66-ff9bccbd7a14%40googlegroups.com > <https://groups.google.com/d/msgid/php-fig/c0ba7fdf-5c45-493c-8f66-ff9bccbd7a14%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/CANsgjnuiU0vy7Z4wE4LsTT0goFzLoVS6fArbftETo_UgaQLV3w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
