``` type T = bool|int;
type Result = string;
function greeting(T $input): Result { return match() {
true, 1 => 'hello user :) ',
false, 2 => 'good-bye user'
}
}```
What's your opinion on this?
1–5 of 5 posts
``` type T = bool|int;
type Result = string;
function greeting(T $input): Result { return match() {
true, 1 => 'hello user :) ',
false, 2 => 'good-bye user'
}
}```
What's your opinion on this?
I had in mind the modern C++ `using` mechanism for creating my own types, but didn't know how to write such logic.
I guess a union would be handy dealing with different alias types, depending on the use case, if you understand what I mean.
On the other hand, `match` is far better to be used if we really get type aliases as you suggest.
Something like `alias Numeric as int|float;`
In contrast to TypeScript or Flow, which can only check the program flow at compile time, PHP will crash and burn if you pass an int into a string function parameter at runtime.
Would you expect the presumed type aliases to be checked at runtime too?
There are already union types in PHP 8, but no aliases.
I think in PHP it is always the best approach to work with classes, whose type you can check at runtime (in contrast to duck-typed languages such as JS, where there is instanceof but that is generally a last resort).
Structural types checked at compile time are a different approach to the one PHP chose I think. Alhough the lines between compilation and execution are blurry in a JIT language of course.
How about overloading `as` operator instead and use `alias` in place of `type`? Something like `alias Numeric as int|float;`