Re: intersection types and null for defaults, properties and return types

From: Date: Mon, 19 Jul 2021 14:28:17 +0000
Subject: Re: intersection types and null for defaults, properties and return types
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-115495@lists.php.net to get a copy of this message
On 19.07.21 11:21, azjezz wrote:
however, when T is an intersection between X and T ( X&Y ), X&Y $x = null becomes null|X&Y $x = null, which is a combination between union and intersection types, however, as the RFC stats, currently combination between union and intersection types is not support ( hence "pure" ). for this to be allowed, PHP would need to support combining union and intersection types, preferable using parenthesis ( X|(Y&Z) $x = null or (X&Y)|Z $x = null ). Supporting a syntax like ?A&B could just be a special case just like ?A
was in the past, before actual union types existed. I can see how full support for union types and intersection types can get complicated (and may never make it into PHP, or need quite some time until it does), but pure intersection types that can be nullable seems like a relatively simple special case which would be very helpful in code, because you could have optional intersection types in arguments/properties, which is otherwise not possible (and one would have to fall back once again to annotations and less type safety). Nullable intersection types also do not touch upon the main problems of mixing union types and intersection types, namely in reflection, where it would just lead to "ReflectionType::allowsNull" to be true instead of false, but not change any other logic as far as I can tell.

« previous php.internals (#115495) next »