Re: [RFC] [DISCUSSION] Scalar Pseudo-type

From: Date: Fri, 29 Dec 2017 17:29:56 +0000
Subject: Re: [RFC] [DISCUSSION] Scalar Pseudo-type
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-101447@lists.php.net to get a copy of this message
Hi all, On 26.12.17 16:56, Sebastian Bergmann wrote:
Am 26.12.2017 um 16:46 schrieb lists@rhsoft.net:
would you mind to explain this?
"Foo|Bar", "array|string", etc. (still) make no sense to me. "scalar" makes sense to me although it is but an alias for "bool|float|int|string".
I followed the discussion and found it interesting how strong the focus shifted about the discussion of the practical use cases. I would be for union types or scalars in ab instant, but I never had reason to scan our code-base and figure what would make really sense. Mind you this is a private codebase (Laravel based, as can be seen by some of the class names) but one where we tend to be very explicit about documenting everything, thus I knew I could just go ahead and extract the type hints using the '|' character and get back a pretty complete picture: $ grep -r '@param' *|grep \| | awk '{ print $4 }'|sort -u Attachment[]|Collection Attachment[]|null Builder|null Channel|Post|int|null Channel|int|null ClientInterface|null Client|int|null Collection|Attachment[] Collection|Channel[] Collection|Comment[] Collection|JsonapiModel[] Collection|Model[] Collection|Post[] DateTime|Carbon|string|null Dispatcher|null EloquentCollection|JsonapiModel[] Exception|null Group|Group[]|Client|Client[]|Channel|Channel[]|null Group|int|null JsonapiModel|null Model|int|null Post[]|null Post|int|null Profile|int|null ResponseInterface|null Throwable|null User|int|null \ArrayAccess|array|JsonapiModel[]|null array|false array|null int[]|int int|int[] int|null mixed|null null|LoggerInterface null|Model null|Model[]|Collection null|string string[]|Expression[] string|int string|null This is extracted from a codebase with ~400 files and ~43k LOC (incl. comments). I'm not saying anything like this is relevant codebase, but it's a project whose code quality I know well and I know the reason for every piece there. Since the discussion focuses on scalars here and ignoring all the nullables (they're auto-documented that way via PhpStorm) as well as ignoring the pseudo array-type hints, this leaves us really with only: - array|false Only 1 occurrence, used for crafting a remote HTTP call which has an interface which accepts either an array or false 🤷‍♀️ - string|int Only 1 occurrence, used for error codes which may come in either flavor, send back via HTTP somewhere I was surprised how less of a use we would really have for a) scalar itself and b) scalar unions. OTOH, typed array hints seem to be very useful as are class based union types. Although if one looks closely, it's clear this isn't a "union" problem, but rather an emulation due to the absence of Generics, e.g. Attachment[]|Collection Is clearly a replacement for something like Collection<Attachment> I hope I didn't distract to much from the discussion but though I throw in some data from an actual application. I'm sure there are much better OSS projects out there which could be used for such analysis. thanks, - Markus

« previous php.internals (#101447) next »