On Sun, Apr 24, 2016 at 2:58 AM, Levi Morrison <le...@php.net> wrote:

> On Sat, Apr 23, 2016 at 10:40 AM, Quim Calpe <q...@kalpe.com> wrote:
>
> Option is no better than a union type with null[1]. If a language
> requires an option to be unwrapped then it can do the same with some
> type or null. This is what Swift does. These things are exactly
> equivalent.
>

I don't think that's strictly true. Option<T> (or Maybe<T>) types that must
be unwrapped can usually be safely nested as Option<Option<...>>. This is
important if you have a generic class Foo<T> that uses Option<T> internally
and Foo is instantiated as Foo<Option<string>> (using string as an
example), causing an Option<Option<string>> to occur inside Foo. If
Option<string> meant string|null, the Option<Option<string>> would become
string|null|null and the real "null" at runtime would become ambiguous.

Nonetheless, nesting is very rarely needed, but it is something to keep in
mind when using generics in combination with union with null for
nullability.



> However in PHP we do not have generics, which means a nullable type is
> actually better because we can express the type that participates with
> null. With an Option type we cannot.
>
>   [1] At least with the behaviors here. If Option is a Traverable that
> returns 0 or 1 items then we can use that behavior to our advantage.
> Of course, we lose the ability to express the underlying option type.
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply via email to