Just a thought that crossed my mind which might satisfy both worlds. Has
anyone every considered unions as a type declaration?
namespace Vector/TypeDefs
union Stringable
{
as string;
as int;
as float;
private $value;
public function __make($type)
{
switch (type) {
case 'string': return (string) $this->value;
case 'int': return (int) $this->value;
case 'float': return (float) $this->value;
}
}
}
my_echo_func("123"); // << A scalar variable on the outside.
function my_echo_func(Stringable $stringable) // << a union on the inside
{
var_dump($stringable as string); // string(3) "123"
var_dump($stringable as int); // int(123)
var_dump($stringable as float); // float(123.0)
}
Perhaps not exactly like this, but adding unions as type of class
declaration should save a hell of a lot of keywords, and may save a number
of "instanceof" type checks as well.
On 20 April 2016 at 20:55, Fleshgrinder <[email protected]> wrote:
> I am not quoting anything because the formatting of your emails is
> completely off in Thunderbird. However, I want to add one to the list:
>
> declare(strict_types=1);
>
> interface Stringable {
> function __toString(): string;
> }
>
> function fn(string|Stringable $arg) {
> $param = (string) $arg;
> }
>
> We could add a new *stringable* primitive to PHP though, but do we guys
> really want to start doing so? There is pretty much an endless amount of
> combinations and finding names alone is endlessly hard.
>
> declare(strict_types=1);
>
> function fn(int|string $maybe_bigint) {
> // mysqlnd automatically creates the proper type based on the value.
> // Expressing this in userland is impossible ... :(
> }
>
> --
> Richard "Fleshgrinder" Fussenegger
>
>