Skip to content

Function captures: nil has been captured vs no captures #4

@rmolinari

Description

@rmolinari

Sometimes a function capture doesn't capture any values: currently this is represented by a return value of nil.

And we want to support the case of the value nil being captured. We represent this case with a return value of [nil].

We also want to distinguish between multiple captures and a single captures that is an array. This is currently the difference between return values of [1, 2, 3] and of [[1, 2, 3]].

What does [] mean: no values were captured, or an empty array was captured? If the latter, what does [[]] mean?

This is all rather awkward. At least part of the problem is that Ruby doesn't really have multiple return values. We return an array and deconstruct via assignment. Lua does this better: return 1, 2 vs return {1,2}.

Is there a better way?

Ideas:

  • use a Maybe monad of some sort.
    • None would be no capture, while Some(nil) would be a capture of nil
    • But how to represent multiple captures? The natural interpretation of Some([1,2,3]) would be be a single captured array.
    • Maybe [Some(1), Some(2), Some(3)]
  • Always return an array (or convert a non-Array x to [x])
    • Then [] is no captures, and [nil] is a capture of nil
    • What does it mean for a function to return nil?

What would such requirements mean for folding captures and other capture-side calculations? Would the use of some sort of Maybe monad require lifting and binding?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions