mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[Feature Request] Strict-mode multi-statement lambdas

Open elliotwaite opened this issue 1 year ago • 3 comments

Request

A strict-mode version of lambda that supports multiple statements.

Motivation and Description

Similar to how fn is like a strict-mode version of def, it seems like Mojo might benefit from having a strict-mode version of lambda. It would have the same distinctions as fn does from def, except unlike fn, it would support type inference for arguments and return types where possible.

Furthermore, it would be great if these strict-mode lambdas could support multiple statements. Although Python has been hesitant to add support for multi-statement lambdas in the past, other languages similar to Mojo, such as Swift, Rust, Julia, and Nim, have demonstrated the advantages of supporting them. And given the current prevalence of asynchronous and functional-style programming, this seems like an area where Mojo would benefit from taking inspiration from these other languages.

As for the syntax, maybe the fn keyword could be reused here, but in an anonymous way without specifying a function name. Or maybe the lambda keyword could still be used, but be made strict-mode by default and support multiple statements in a way that is compatible with its current syntax. Or maybe inspiration could be taken from the syntaxes used by Swift closures, Rust closures, or Nim anonymous procs.

There are probably nuances to this feature that I haven't considered yet, so I'd be interested to hear what others think.

elliotwaite avatar May 06 '23 04:05 elliotwaite

Agreed, we definitely need to look into this. I suspect that (over time) we will want to add something like the scala3 sugar for defining lambdas inline in a more ergonomic way, but I'd like to hold back on sugar for as long as possible. Thank you for filing this though, because we need to nail down the lambda syntax

lattner avatar May 07 '23 00:05 lattner

Sounds good. That scala3 sugar would be nice. But that makes sense to hold back on adding extra sugar for now.

What inspired me to post this feature request was Paul Graham's critique of Python in his Re: Revenge of the Nerds post, where he argues that it doesn't make much sense that lambdas are restricted in ways that named functions aren't:

The restriction on what you can say in an anonymous function seems particularly indefensible. In fact, the whole phrase "anonymous function" suggests limited thinking. Would you call a hash table that wasn't the value of a variable an "anonymous hash table?" If functions are a data type in your language, they should be treated just like other data types. A named function should just be a function that happens to be the value of a variable. Restricting what you can put in a function that isn't the value of a variable makes about as much sense as restricting what you can do with a hash table or string that isn't the value of a variable.

This made me think that maybe Mojo could finally address this issue by either supporting a version of lambda that is essentially an anonymous fn, or by upgrading lambda directly to support anything that can be done in a fn. However, if support for multi-statement lambdas isn't feasible in the near term, perhaps the initial syntax could just be designed in a way that allows for the possibility of adding support for them in the future.

elliotwaite avatar May 07 '23 16:05 elliotwaite

Cool makes sense, thanks for the link to the essay!

lattner avatar May 08 '23 08:05 lattner