On Thu, 20 Jun 2024, at 18:38, Larry Garfield wrote: > Hello, peoples. > > Ilija and I have been working on and off on an RFC for pattern matching > since the early work on Enumerations. A number of people have noticed > and said they're looking forward to it.
Hi Larry, I haven't time to read through the full RFC at the moment, but a couple of thoughts: As Andreas says, we should be careful not to pre-empt things that might be added to the type system in general, and end up with incompatible syntax or semantics. That particularly applies to the generic-like array<int> syntax, which is quite likely to end up in the language in some form. The "weak-mode flag" seems useful at first glance, but unfortunately PHP has multiple sets of coercion rules, and some are ... not great. It's also not immediately obvious which contexts should actually perform coercion, and which should just assert that it's *possible* (e.g. match($foo) is { ~int => (int)$foo } feels redundant). So I think that would need its own RFC to avoid being stuck with something sub-optimal. Similarly, the "as" keyword has potential, but I'm not sure about the naming, and whether it should be more than one feature. Asserting a type, casting between types, and de-structuring a type are all different use cases: $input = '123'; $id = $input as int; // looks like a cast, but actually an assertion which will fail? $handler as SpecialHandler; // looks like an unused expression, but actually an assertion? $position as [$x, $y]; // looks like its writing to $position, but actually the same as [$x, $y] = $position? It's worth noting that in languages which statically track the type of a variable, "$foo = $bar as SomeInterface" is actually a type of object cast; but in PHP, it's the value that tracks the type, and interfaces are "duck-typed", so it would be equivalent to "assert($bar is SomeInterface); $foo = $bar;" which isn't quite the same thing. Regards, -- Rowan Tommins [IMSoP]