Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

For what it's worth, this is also a very common pattern in ruby, using blocks:

  def with_error_handler
    if error = yield
      puts "error: #{error}"
    end
  end

  def do_things
    error = do_this
    return "error doing this: #{error}" if error
    error = do_that
    return "error doing that: #{error}" if error
    nil
  end

  with_error_handler do
    do_things
  end
Having said that, I did need to read the Go version more than once to grok it. I theoretically like Go's syntax for defining functions that take functions, but in practice I find it quite hard to scan, especially if there are more parameters on top of the function, or the passed function has multiple returns, or (god forbid!) it takes a function itself - it can all become quite a lot of bookkeeping.


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

Search: