Hey Larry, On Wed, Jul 15, 2020 at 5:32 PM Larry Garfield <la...@garfieldtech.com> wrote:
> 1) return null, which is a non-type, and thus you need to make the return > type ?User or User|null, which means the caller *must* always check it's > nullness. > > Allowing an object to falsify itself is a 4th, more type-safe option. It > lets you return an object that both fails a boolean check (like null) but > also has default values in it for the base case. A user object likely > wouldn't use that, but a value object like an Address very well could. > > Until we can support for-reals monads (which would require enums and > generics to do properly; the former is possible the latter is very hard), > it's the best option we have for more type-safe returns. > Adding a "falsey" state for code that expects `is_object($foo) === (bool) $foo` seems to be a massive BC break to me, and reduces type safety of boolean operators (and checks) by expanding the `object` type and all operators and expressions that can interact with it. In general, `?T` is very much equivalent to `Maybe T` Where `instance Monad Maybe` is implemented in type-safe langs as (quoting https://en.wikibooks.org/wiki/Haskell/Understanding_monads/Maybe - I omitted `return` because the lifting bit is not really used in PHP): ```hs (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b (>>=) m g = case m of Nothing -> Nothing Just x -> g x ``` `?T` is very much the same in PHP (pseudo-code, since nullability is not represented the same way in Haskell): ```hs (>>=) :: ?T -> (T -> Maybe T2) -> ?T2 (>>=) m g = case m of null -> null x -> g x ``` Let's not confuse Java nullable issues with PHP nullability, which is quite healthy :-) I see adding a "falsey" evaluation to something that has been assumed (for a looooooong time) as universally "truthy" is a very major issue. We need code examples where this is a clear advantage over previous logic: as it stands, I don't see why I would ever prefer `(bool) $object` over `$object !== null`, or `$object->valid()` (which I believe to be an anti-pattern) Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/