Now we have decorators and functions-as-objects I think it would be interesting to solve defaults handling limitations, the changes I propose are as follows:1) Consolidate [Py|Lex]Func and [Py|Lex]FuncVarArg into [Py|Lex]Func which will have both [defaults : (listof [Py|Lex]Expr)] and [vararg : (optionof [Py|Lex]Expr)]2) Desugar LexFunc in the same way LexFuncVarArg is being desugared until now but, add two attributes:- __defaults__: a tuple built with the results of the evaluation of defaults expressions- __name__: a string with the function name (for completeness)3) Extend [Py|Lex]Lam to support both vararg and defaults in the same way, except __name__ = '(Lambda)'4) Change py-app to use __defaults__:- if there is no stararg and there are sufficient positional arguments, no changes WRT existent desugar (I think itconvenient to simplify the "normal" case for performance and bootstrapping reasons)- else, let starvals be the result of tuple(stararags) or tuple() if no starargs present, appended with elements fromthe tail of function.__defaults__ up to match the positional parameters of the function and desugar as usual.IMHO this is worth doing since it extends the supported functionality while it simplifies the current desugar and, in principle, it doesn't require modifications to the core/interpreter.
To add support for keyword arguments, **kwarg and **expr is another kind of beast, I'm afraid.
If we do run with it, we should see if we can simplify the Py/Lex data structures first in case Matthew is going to go off and re-write code that uses those structures.
To add support for keyword arguments, **kwarg and **expr is another kind of beast, I'm afraid.Agreed that this is trickier but also worthwhile. At a high level, do we think there's any chance of handling these purely in desugaring, or are they deeply embedded in how functions work? It might be worth spending a little time thinking about them if they'll significantly change the way application works, so we can settle on a model of functions once and for all. I haven't played with these as much as I'd like, does anyone have a good sense of what's involved here?I could see us, for example, desugaring all user-defined Python functions to have the first two arguments are always be the positional tuple and keyword dictionary, and desugaring to wrap the body in the appropriate let-bindings for any named or default arguments in the original function signature. That would be a big change to how we do desugaring, but (I think) not require changing the core.
I think it would be not too difficult to extend the proposed scheme to handle keyword arguments by desugaring, it would be an additional "massaging" of the starargs tuple, even *kwarg seems possible but, the major problem seems to be "mapping expansion" feature in calls (**expr), since it allows the dynamic building of keyword/value pairs and, it probably will require some kind of additional core/interpreter support.