Skip to content

Generalize return values #32

Closed
Closed

Description

@Technologicat

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 for tuple) ⇒ positional values
  • named (semantic wrapper for dict) ⇒ named values
  • length-2 tuple of (values or 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

  1. added
    enhancement
    discussion
    on Sep 9, 2019
  2. added this to the ∞ milestone on Sep 9, 2019
  3. modified the milestones: ∞, 0.15 on Mar 1, 2020
  4. Technologicat commented on Apr 12, 2021

    @Technologicat
    OwnerAuthor

    Would be really nice, but not critical for now. Migrating to mcpyrate and adding Python 3.8/3.9 support is already enough for 15.0.

  5. modified the milestones: 0.15, ∞ on Apr 12, 2021
  6. Technologicat commented on May 22, 2021

    @Technologicat
    OwnerAuthor

    Named return values now available in the returnvalues branch. 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 Values type, which handles both positional and named return values.

    All function composition utilities (curry, the compose family, the pipe family, and the with continuations macro) have been changed to use Values to denote multiple-return-values. Now tuple means a single return value that is a tuple.

    valuify was added as originally envisioned.

    One particular advantage of this new solution is that curry can now pass named return values to an outer curry context.

  7. modified the milestones: ∞, 0.15 on May 22, 2021
  8. Technologicat commented on May 22, 2021

    @Technologicat
    OwnerAuthor

    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.

  9. Technologicat commented on May 22, 2021

    @Technologicat
    OwnerAuthor

    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.

  10. Technologicat commented on May 28, 2021

    @Technologicat
    OwnerAuthor

    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:

    • Document Values and valuify.
    • Move the docs of call, callwith; these now live in unpythonic.funutil, the same module as Values and valuify. 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 use Values in the pipe family.
    • Document how to use Values in the compose family.
      • There's not much on multiple-return-values in compose, except in the unit tests. Maybe needs some expansion?
      • Nah, fine as-is for now.
    • Document how to use Values in curry.
      • The semantics are explained, but could use some examples.
      • Nah, fine as-is for now. Added a link to the unit tests, for advanced examples.
    • Document how to use Values in with continuations.
    • Check if code examples in docs and docstrings need updating.
  11. Technologicat commented on May 29, 2021

    @Technologicat
    OwnerAuthor

    As of e848f95, resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

discussionenhancement

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions