mauke / Function-Parameters

Function::Parameters - define functions and methods with parameter lists ("subroutine signatures")
https://metacpan.org/pod/Function::Parameters
18 stars 19 forks source link

Make `fun foo` equivalent to `func foo()` under strict. #9

Closed schwern closed 1 year ago

schwern commented 9 years ago

Strict function parameters are about explicitly expressing intent and preventing mistakes, by both the caller and by the person writing the function. fun foo should be equivalent to fun foo() (same for methods) when strict parameters are on.

Intent: Does fun foo mean the author forgot the parameter list? Or does it mean it takes no parameters? Now I'm back to examining the body of the routine to figure it out, something parameters are there to prevent.

Mistakes: It's really easy to forget the parameter list in a function that takes no parameters, Perl programmers are trained that way. The caller will get no warnings nor errors that their arguments are being thrown away.

Compatibility with Perl 5: Since Function::Parameters is not using sub, it has no need to be compatible like Perl 5 does. Not using sub signals to the user that things are different now.

Backward Compatibility This will make F::P backwards incompatible for users of strict mode. On the up side, their tests will catch it real fast and there's a cross-compatible solution, always use empty parameters.

Expectations: The user asked for strict parameters. Give them strict parameters! Users may not realize the behavior exists, but that will only last until they run their code and get a good error message.

Alternative: Disallow no parameter list entirely under strict, then you must be explicit.

mauke commented 1 year ago

Alternative: Disallow no parameter list entirely under strict, then you must be explicit.

This has effectively been implemented in F:P version 2+. A function definition without () is now a syntax error, regardless of strict/lax mode.