On Mon, Apr 28, 2014 at 10:58 AM, Jason Grout
<[email protected]>wrote:
> On 4/28/14, 12:53, Simon Byrne wrote:
>
>>
>>
>> On Monday, 28 April 2014 17:06:37 UTC+1, Simon Byrne wrote:
>>
>> My own perspective is that this is due to two reasons
>>
>> 1) `do` is the only (non-macro) construction that rewrites
>> expressions i.e.
>>
>> open("outfile", "w") do f
>> write(f, data)
>> end
>>
>> does not (directly at least) call the method `open("outfile", "w")`
>>
>>
>> Also, the fact that rewriting occurs due to a term that appears *after*
>> a completed expression I think adds to the confusion. Perhaps this would
>> be less confusing if some indicator had to appear before the `open`
>> statement. For example, we could have a macro:
>>
>> @do open("outfile", "w") f begin
>> write(f, data)
>> end
>>
>
> Then we're almost to the python syntax:
>
> with open("outfile", "w") as f:
> f.write(data)
Since the actually functionality of `do` is passing the block as an
anonymous function to `open` et al., this doesn't always work. Consider
get!(mydict, "key") do
f(key)
end
This is equivalent to
get!(()->f(key), mydict, "key")
It's unclear to me how this would work using Python's "with ... as" syntax.
I was also confused by the do syntax at first. In particular, it's unclear
to me that the block is an anonymous function, and I'd prefer that be made
explicit. Maybe something like
open("outfile", "w") do (f) -> write(f, data)
or
open("outfile", "w") do function(f)
write(f, data)
end
It's more verbose, but I'd argue it's also clearer. The `get!` example
would then be
get!(mydict, "key") do () -> f(key)
or
get!(mydict, "key) do function()
f(key)
end
Cheers,
Kevin