Hacker News new | past | comments | ask | show | jobs | submit login

The file handle is the PID of the process that opened the file, it monitors the process that asked for the file to be opened. and if that process goes down the file will be closed.

You can also add an else in the with so it looks like

    with {:ok, fh} <- File.open(filename),
         {:ok, contents} <- IO.read(fh)
    do
         contents
    else
         {:error, reason} -> File.close(fh)
                             {:error, reason} # this will return after the file has been closed
    end
or maybe you would prefer to open the file, and pass that into the with and either way close the file. I just used IO as an example because they return nice {:ok, x} or {:error, reason} tuples, but this works with any pattern.



> The benefits over an exception is as the caller i can pattern match on the result of this. so i could have this is in a function in a case

Your example looks just like exceptions to me, just with different keywords

    try:
        return with_example(fname)
    except RecoverableError:
        recover(fname)
    except Exception as e:
        raise e


In the examples yes, because it's just a simple binary ok or error case

but if you returned a list you can pattern match on an empty list, a single element list or a list that's longer. which you wouldn't do with exceptions in another language. that's the nice part about `with` that you can stop once you stop matching the pattern and return whatever you currently have, which in the list example may be a perfectly valid thing to return.

    with [value] <- Module.some_list_function(arg), # can return an empty list too
         [head | tail] = list <- Module.another_func(value), # can return a single element list
         longer_list <- Module.takes_multi_element_list(list)
    do
       longer_list
    end
There are loads of other examples for uses of this, or you can just write in an additional clause for your function that handles the other case and pipe it down the line.

It's about what makes sense for your domain.

I may not be explaining this well so apologies for any confusion




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: