Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rmolinari opened this issue Apr 4, 2023 · 0 comments
Open

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

rmolinari opened this issue Apr 4, 2023 · 0 comments

Comments

@rmolinari
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant