This works:

open("foo.txt","w") do f
    print(f,"Hello World")
end

Other use cases could make use of the same idiom.


On Mon, Jan 13, 2014 at 11:29 AM, andrew cooke <[email protected]> wrote:

> PS I should add that it's not clear to me how you get from opening/closing
> files to providing default arguments - perhaps there's a thread local
> global?
>
>
> On Monday, 13 January 2014 13:28:20 UTC-3, andrew cooke wrote:
>>
>> Hi,
>>
>> Is there anything in Julia similar to Python's "with context" (or using
>> with-open-file in lisp, or similar)?  To automate closing files after
>> opening them, for example.
>>
>> The reason I ask is that I'm sitting here listening to Miles give a talk
>> on JuMP and looking at code like:
>>
>> ```
>> m = Model()
>> @addconstraint(m, ...)
>> @something(m, ...)
>> ```
>>
>> and it seems like it would be nicer to have a protocol of some kind that
>> enabled:
>>
>> ```
>> with Model()
>>    @addconstraint(...)
>>    @something(...)
>> ```
>>
>> (where the first argument is implicit)
>>
>> Perhaps that's a bit too much syntactic sugar? Or perhaps too much like
>> single dispatch OO?
>>
>> I have a feeling there is something vaguely similar, but I can't remember
>> what (sorry).
>>
>> Thanks, Andrew
>>
>>

Reply via email to