> With that I was able to debug the problem in a snap. It turns out that one 
> of my functions had "readlines(open(...))" instead of 
> "open(readlines,...)". The critical difference is that the former leaves a 
> file pointer dangling.
>

Ah, ok, that's exactly the difference: using open(myfunc, filename) is the 
same as 

open(filename) do io 
 myfunc(io)
end

Like this you never have to worry about closing files, even on exceptions.

When using open without a function it looks like this:

 io = open(filename)
 myfunc(io)
 # this is crucial:
 close(io)

In this version, when an exception occurs in myfunc the file will never be 
closed.

Last note: the "do" syntax is not special for open but can be used with 
your own functions as well:

function functhatneedsfunc(f, a)
 println("let's just apply f")
 f(a)
end

# lets call it:
functhatneedsfunc(a) do x
 show(x)
 println("printed x!")
end


 

Reply via email to