Re: Better types without runtime checking
| From: | Theodore Brown | Date: | Tue, 05 Jun 2018 15:04:35 +0000 |
| Subject: | Re: Better types without runtime checking | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102169@lists.php.net to get a copy of this message | ||
One Tue, Jun 5, 2018 at 2:28 AM Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
> On Tue, Jun 5, 2018 at 4:23 PM Rudolph Gottesheim <r.gottesheim@loot.at>
> wrote:
>
>> There's always a lot of talk about types in the PHP community.
>> Specifically, many developers want property types, generics, function
>> signature types, union and intersection types, and more. Those talks
>> (and RFCs) always end with the same result: "We can't do it because
>> performance issues."
>>
>> Has there ever been a discussion about adding some of those features
>> syntactically, but ignoring them during runtime? At least until someone
>> finds a performant way to check them at runtime. That way we could have
>> advanced type checking in our editors at least.
>>
>> The idea will sound familiar to TypeScript users. It works great for
>> that language.
>>
>> I'm excited to hear your opinions.
>
> DbC style has no performance issue at all. In addition, you can perform
> much better validation than simple type checks.
>
> function f($username) {
> assert(is_string($username) && preg_match('\\A[a-z]\\z', $username);
> ....
> }
Assertions are not a replacement for syntactical type declarations, since
they cannot be checked until runtime. They are also more verbose, and
don't help editors provide autocompletion when writing code.
+1 to the possibility of a PHP static type checker which can be run
independently from the runtime to support more advanced types without
degrading performance.