Nullable types before union types

php.internals

Igor Inas

10 years ago
Hello, same as last post in the mailing list - this may have been suggested before - but I had hard time finding it in the archives. Coming from static typed languages, I love the addition of return type annotations and extension of parameter type annotations. I also love the fact, that types are not nullable by default. I am reading about the proposal to add nullable types before union types are fully implemented. This would be done by utilizing question mark (int? / ?int). My question is: would it be too difficult to implement nullable types using syntax of union types, but semantically, only max two types would be allowed in the union and one of them would have to be null. So only allowed forms would be: - int | null - null | int I am thinking about the future, when union types become implemented and suddenly there would be two ways how to annotate the same nullable type: - int | null - int? Thanks for more insights, Regards, Igor

Levi Morrison

10 years ago
On Mon, Aug 29, 2016 at 7:36 AM, Igor Inas <igor.inas@gmail.com> wrote:
> Hello, > > same as last post in the mailing list - this may have been suggested before > - but I had hard time finding it in the archives. > > Coming from static typed languages, I love the addition of return type > annotations and extension of parameter type annotations. I also love the > fact, that types are not nullable by default. > > I am reading about the proposal to add nullable types before union types > are fully implemented. This would be done by utilizing question mark (int? > / ?int). > > My question is: would it be too difficult to implement nullable types using > syntax of union types, but semantically, only max two types would be > allowed in the union and one of them would have to be null. So only allowed > forms would be: > - int | null > - null | int > > I am thinking about the future, when union types become implemented and > suddenly there would be two ways how to annotate the same nullable type: > - int | null > - int? > > Thanks for more insights, > Regards, > Igor
The union types RFC did not get enough support to pass. Additionally having two forms of syntax to do the same thing is not the worst thing that can happen in language design. They would at least be semantically equivalent which is the important thing.

Björn Larsson

10 years ago
I think you will find information about this in the threads for discussion and voting regarding Nullable types RFC: - https://marc.info/?l=php-internals&m=146060540111128 - https://marc.info/?l=php-internals&m=146289381815830 - https://wiki.php.net/rfc/nullable_types Regards //Björn Larsson Den 2016-08-29 kl. 15:36, skrev Igor Inas: