Has there ever been discussion around a python-like "with" syntax?

Michael J. Ryan tracker1 at gmail.com
Wed Oct 17 20:19:17 UTC 2018


I was going to mention C#'s using statement as well... though I do like the
use of a symbol over Ron's proposal, I think using might be a better
approach.

-- 
Michael J. Ryan
480-270-4509
https://www.tracker1.info/
me at tracker1.info
tracker1 at gmail.com


On Mon, Oct 15, 2018 at 11:50 AM Till Schneidereit <
till at tillschneidereit.net> wrote:

> Ron Buckton has a proposal that's quite similar to what you're talking
> about: https://github.com/tc39/proposal-using-statement
>
> On Mon, Oct 15, 2018 at 11:40 AM Dan Aprahamian <dan.aprahamian at gmail.com>
> wrote:
>
>> Hello all! First time posting here. I was curious if there was ever talk
>> about adding something similar to python's with syntax to JS. Obviously we
>> already have a "with" keyword, but I figure we could probably use "use" as
>> the keyword instead. I was thinking something like
>>
>> // Some user defined resource that can be "entered" and "exited"
>> class MyResource {
>>   // Function called when entered
>>   async [Symbol.enter]() {
>>   }
>>
>>   // Function called when exited
>>   [Symbol.exit]() {
>>   }
>> }
>>
>> const resource = new MyResource();
>>
>> async function useResource() {
>>   use myResource {
>>     // Inside here, resource has had "acquire" called on it
>>
>>     await doSomethingAsync();
>>     // Once this block is finished executing (including async)
>>     // release is called
>>   }
>>   // Release has been called now
>> }
>>
>> Use would effectively be the equivalent of:
>>
>> async function use(resource, body) {
>>   await resource[Symbol.enter]();
>>   try {
>>     await body();
>>   } finally {
>>     await resource[Symbol.exit]();
>>   }
>> }
>>
>> Has something like this been considered before?
>>
>> Thanks,
>> Dan
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181017/831bb36f/attachment.html>


More information about the es-discuss mailing list