[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: (not) handling new programming idioms with grace
- From: Wim Couwenberg <wim.couwenberg@...>
- Date: Sun, 29 Jul 2018 13:54:49 +0200
> As hard as I try, I don't see why 'with foo() as x' is clearer than
> 'local scoped x = foo()'. Both have a very clear and unique mark
> ('with' vs 'scoped'). Moreover, 'with' has different meanings in
> some other languages, so it can be even more confusing.
(Sorry if this breaks the mail thread, I only subscribe to digest mails.)
If I understand the “local scoped” proposal correctly then at least “local scoped” and Python’s “with” have different semantics. Python does *not* bind context management to a local variable name. For example you can omit the variable name or assign a different value to it in its scope, it will not affect the behavior. Instead the context manager is linked to the scope itself (behind the scenes as it were).
In terms of Lua, this would more closely resemble a “scoped” expression like this:
local x = scoped <expression>
Here “scoped <expression>” would bind the expression to the current scope. The local name x in the second example is completely unrelated to the context management of the expression.
Another difference then could be that the order of __enter__/__exit__ calls is quite clear in Python while it could be not so clear for such a “scoped expression” in constructs like:
local x = foo(scoped bar(), scoped baz())