4 ms·
This isn't exactly a sum type. Sum type is basically a Union, a proper algebraic data type, and PHP doesn't have algebraic data types (although, even before typ
by krick 6y ago
This isn't exactly a sum type. Sum type is basically a Union, a proper algebraic data type, and PHP doesn't have algebraic data types (although, even before type hints it was a relatively common practice to "implement" them via PhpDoc... horrible, I know).
Enum is basically a bunch of constants, for all practical purposes. There were enums in Java for a long time, long before languages with more-or-less proper type systems became mainstream, and everybody was fine with a rigid mess of type system Java has.
So, to have what effectively is enum in PHP, I do just
class Status
{
const STATUS_A = 'STATUS_A';
const STATUS_B = 'STATUS_B';
// and so on
{
So it looks like a new enum would be basically just a syntactic sugar for that. (And finally a way to make a type hint for $a = Status::STATUS_A.)
- cannabis_sam 6y ago> everybody was fine with a rigid mess of type system Java has. Were we? Java is literally the language that drove me to hate types and prefer python for a few years, until I learned Haskell..