Repository navigation
Description
As of 0.14.1, in many places involving function composition, unpythonic uses the convention that returning a tuple means returning multiple values positionally. These are then unpacked, positionally, as the arguments of the next function in the chain.
From a composition viewpoint, what is currently lacking are named return values, in symmetry with named arguments.
The syntax could be something like this:
def f(...):
...
return values(my, positional, values), named("my": ..., "named": ..., "values": ...)So instead of just a tuple, accept any of:
values(semantic wrapper fortuple) ⇒ positional valuesnamed(semantic wrapper fordict) ⇒ named values- length-2
tupleof (valuesor any type,named) ⇒ both positional and named values - else ⇒ one positional return value, the usual case
The semantic wrappers could be:
class values(tuple):
"""Multiple return values. Wrapper for tuple."""
class named(dict):
"""Named return values. Wrapper for dict."""The important points are to make it possible to tell these apart from the respective base types using isinstance, yet let them act just like the corresponding base type (including passing any isinstance checks) in code that doesn't need this distinction.
This would be a breaking change, so 0.15.0 is the earliest where we could do this. This would improve the semantics, but needs updates to everything across the codebase where multiple return values are handled (grep the codebase for multiple, including any comments). The most fearsome part requiring changes is the continuations macro.
The lazify macro also needs some changes; lazyrec needs to recognize the values and named constructors. Adding them to the list of recognized constructors should be enough; with exactly the same settings as the builtin tuple and dict.
Probably best to start by changing the convention of a tuple return value to use an explicit values instead. This would already improve the semantics, allowing us to get rid of many 1 variants of functions (composer1, iterate1, etc.). If the old semantics are needed for compatibility reasons, they can be restored with a decorator:
def valuify(f):
"""Decorator. If `f` returns `tuple`, cast into `values`, else pass through."""
@wraps(f)
def valuified(*args, **kwargs):
result = f(*args, **kwargs)
if type(result) is tuple: # yes, exactly tuple
result = values(result)
return result
return valuified
@valuify
def f():
return (1, 2, 3)
assert isinstance(f(1, 2, 3), values)
Activity
Technologicat commentedon Apr 12, 2021
Would be really nice, but not critical for now. Migrating to
mcpyrateand adding Python 3.8/3.9 support is already enough for 15.0.Technologicat commentedon May 22, 2021
Named return values now available in the
returnvaluesbranch. The branch will be merged into master before 0.15 hits.The solution is a bit different and more uniform than initially envisioned; see the new
Valuestype, which handles both positional and named return values.All function composition utilities (
curry, thecomposefamily, thepipefamily, and thewith continuationsmacro) have been changed to useValuesto denote multiple-return-values. Nowtuplemeans a single return value that is a tuple.valuifywas added as originally envisioned.One particular advantage of this new solution is that
currycan now pass named return values to an outer curry context.Technologicat commentedon May 22, 2021
Also, I figured that since, with the arrival of 0.15.0, the API will break anyway, it makes the most sense to do this now - because the next chance for breaking changes (0.16.0) may be a long way off.
Technologicat commentedon May 22, 2021
And there's a living TODO list in
unpythonic/seq.py, around line 100. The branch will be merged once these TODOs are resolved.EDIT: As of d45e49b, that misplaced TODO list is gone (good riddance). Tracking the remaining related doc update in this issue instead.
remove last TODO marker, now tracked in issue #32
Technologicat commentedon May 28, 2021
As of d45e49b, the code is done, so it's now in
master.But before this can be considered fully done, the docs need an update. Status and TODOs as of 37ef52a:
DocumentValuesandvaluify.Move the docs ofcall,callwith; these now live inunpythonic.funutil, the same module asValuesandvaluify. This new module should get its own documentation section, as it's a somewhat coherent collection of function call and return value related low-level utilities.Document how to useValuesin thepipefamily.Document how to useValuesin thecomposefamily.compose, except in the unit tests. Maybe needs some expansion?Document how to useValuesincurry.Document how to useValuesinwith continuations.Check if code examples in docs and docstrings need updating.Technologicat commentedon May 29, 2021
As of e848f95, resolved.