Another one would be to call local t, data just before the try block, though I like Matt’s better.
On Thursday, September 3, 2015 at 5:21:31 PM UTC-4, Spencer Russell wrote: Ah yes. That works. Good idea, thanks for the tip. > > -s > > > On Thu, Sep 3, 2015, at 05:12 PM, Mike Innes wrote: > > Would `t, data = try take!(outbox) catch ...` work? > > On Thu, 3 Sep 2015 at 22:08 Spencer Russell <s...@media.mit.edu > <javascript:>> wrote: > > > I often find myself wanting to do an assignment inside a try...catch > block, as in the following real-world code I'm working on to read from a > Channel until it's closed > > global outbox = Channel() > @async while true > try > t, data = take!(outbox) > catch e > # InvalidStateException is thrown when the channel is closed > isa(e, InvalidStateException) || rethrow(e) > break > end > println(sock, json(c(t, data))) > end > > But the problem is that try...catch blocks introduce their own scope, so t > and data don't exist after the exception handling. One thing I've done > before is introducing the variables before the try block, (t = nothing; > data = nothing) block before the try...catch, but that kills the type > stability of the variables and doesn't feel right. > > Is there a more Julian idiom for this sort of thing? > > Thanks, > -s > > >