logoalt Hacker News

p4bl0 • today at 2:03 PM • 0 replies • view on HN

I'm a bit sad that there's still no way to limit the number of arguments of a user-defined function in the type system. The internal functions have that.

I know that introducing such a behavior by default could break existing code, but it seems relatively easy to introduce this feature in a backward compatible way by using the void type. For example :

    function foo (void) {}
    foo(42); // fatal error: foo() expects exactly 0 arguments, 1 given

    function bar ($a, $b, void) {}
    bar(1, 2, 3); // fatal error: bar() expects exactly 2 arguments, 3 given

    function baz ($a, $b=null, void) {}
    baz(1, 2, 3); // fatal error: baz() expects at most 2 arguments, 3 given