5 ms·
> and false as standalone types That's just wrong. Boolean is the type. False is just one possibile value of that type.
by bufferoverflow 4y ago
> and false as standalone types
That's just wrong. Boolean is the type. False is just one possibile value of that type.
- blowski 4y agoIt's for backwards compatibility with functions that return either the expected value (maybe an array or a string) or false (never true). So you can use it in a union like `foo(): string|false {}`. In that sense, it's not a boolean because it will only ever be a string or false.
- tyingq 4y agoThat makes sense, though you would want a linter or something to check that callers are doing "=== false" and not "== false", since falsy (but not false) values would match the latter.
- Dragony 4y agoSadly there are a number of functions in the PHP standard library that literally never can return true. Often they return a resource or false, for example. In those cases the `false` type is more accurate than boolean. That's the reason for the distinction.
- cestith 4y agoI don't think the argument is against unit types. I think the argument is against calling one "false", and I agree. If you're going to have a reserved keyword "false", have "true" and "false" as Boolean. If you want a unit type to mean "failed", "failure", "incomplete", or something, use one of those words.
- djbusby 4y agoGood design choice for new lang. What about when there is 20+ years of history? Can't just move fast and break things.
- cestith 4y agoIsn't this an announcement about introducing this and other changes into the language now?
- masklinn 4y agoIt’s really an announcement about the formalisation of existing API patterns: returning FALSE on failure (regardless of the “success” type) is part of many old php apis, this change allows properly typing those.
- kijin 4y agoIt actually makes a bit of sense if you think of Bool as a class instead of a primitive type, and True and False as subclasses of Bool. So a function can either declare its return type as "bool", or as the more specific "false". True is simply not implemented. PHP isn't exacly an "everything is an object" language, but it's been slowly moving in that direction for years, replacing most callables, resources, etc. with corresponding objects. I wouldn't be surprised if the designers are approaching this issue with an object-oriented mindset, though it still feels wrong to make an exception for only one half of a boolean pair.
- steve_adams_86 4y ago
- mikojan 4y ago"false" is a literal type in TypeScript, Coq, Agda, Idris, Scala.
- zozbot234 4y agoFalse in Coq/Agda/Idris is a zero type, not a unit type. True is a unit type. Now PHP is making the opposite choice. Confused enough?
- masklinn 4y ago> That's just wrong. Boolean is the type. False is just one possibile value of that type. Having false and true be singleton types is perfectly cromulent. That's how Smalltalk has been doing it for 40 years, and why it can do without control structures: Boolean subclass: False [ ifTrue: trueBlock ifFalse: falseBlock [ ^falseBlock value ] "..." Boolean subclass: True [ ifTrue: trueBlock ifFalse: falseBlock [ ^trueBlock value ] "..."