How to type-infer PHP-functions with optional arguments

The thing with PHP is that it doesn’t have a specific syntax when using a function with optional arguments. This:

foo(10);

can either be this

function foo($a) {}

or this

function foo($a = 0) {}

or this

function foo($a, $b = 0, ...) {}

(or even function foo() {}, but disregard that for now).

So how do we type-infer it when a function is used before it’s defined?

One solution is to record all usages until the definition comes, and then see if they all can unify with it. The example above would then store

int -> unit

and later check if it’s compatible with

int -> (typ list) -> unit

or whatever (where typ list is the list of optional arguments).

You think this could work? Are there other conventional ways to deal with this?


Source: php

Leave a Reply