Danack / FunctionTypes

php needs function types
MIT License
3 stars 2 forks source link

Return type guarantees + closures #5

Closed Danack closed 4 years ago

Danack commented 4 years ago

If the signature says the callable needs to return a type, an invocation of said callable must always return an instance of that type.

typedef returns_int = callable(string $in): int ;

function foo(returns_int $cb) {
    return $cb("123"); 
} 

// This is a closure that returns int
$fn = fn($a) => "4$a";

// This is using the closure.
$result = foo($fn);

In strict mode, this should be an error. In weak mode type juggling could occur.

But adding return type info to closures seems unlikely to be possible.

Danack commented 4 years ago

I think this is done. Other people might disagree. Need someone to proofread a7a52e21095b04a87e9b584567ff97cb52df92cc

Danack commented 4 years ago

Probably need to add void return type examples.