Replies: 4 comments 18 replies
-
If this is missing, could we have labels for missing (and to be implemented) Python features so it's easy to find them in the GitHub issues database? |
Beta Was this translation helpful? Give feedback.
-
Mojo does have closures, just not the from Vector import DynamicVector
var lambdas = DynamicVector[fn(Int) capturing -> Int]()
for i in range(10):
fn f(x: Int) -> Int:
return x + i
lambdas.push_back(f)
print(lambdas[3](0)) But be careful about #217 |
Beta Was this translation helpful? Give feedback.
-
%%python
lambdas = []
for i in range(10):
lambdas.append(lambda x: x+i)
print(lambdas[3](0)) # Output: 9, due to the late binding closure issue %%python
lambdas = []
for i in range(10):
lambdas.append(lambda x, i=i: x+i)
print(lambdas[3](0)) # Output: 3 |
Beta Was this translation helpful? Give feedback.
-
Loosely held opinion, Mojo clearly needs to support:
Lower priority, but I think we're likely to explore:
User defined statements are a nice way to shift more language syntax into the library, but are just syntactic sugar and will require a little more infra to get wired up. For example, I would like "return" in that context to return from the enclosing function (not from the lambda), and things like break to work for loop-like constructs. This is quite possible to wire up, but will require a bit of design work. |
Beta Was this translation helpful? Give feedback.
-
I couldnt figure out how to make a closure in mojo. I wonder if it is supported, or whether there are plans to support it in the future.
i just hope this will result in something more sensible than pythons result
Beta Was this translation helpful? Give feedback.
All reactions