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

> In ML or Haskell when using sum types, that code path does not exist.

Of course it exists: don't unpack the result and ignore it or pass it along, or use it through its monadic or applicative interface (to proxy-with-transform to the caller).



I feel we may be arguing without me having properly defined what I meant, so I will try to remedy this. Consider the following code:

    a, _ := some_computation_that_may_fail()
    b := f(a)
In Go, if there was an error in `some_computation_that_may_fail`, the call to `f` will be made with a undetermined value for `a` and we end up with an undetermined value for `b`. This is why I say that there is a path where we can use `a` even when we shouldn't.

Compare with OCaml:

    let Some a = some_computation_that_may_fail () in
    let b = f a
First of all, the compiler will warn you that the pattern matching on line 1 is non-exhaustive and will give you an example of a failing case (here, it will simply be `None`). If the programmer ignores that warning and runs the program anyway and the computation executes with an error, the program will fail at the first line, and thus the second function `f` will never be called and `b` will not contain an undetermined value.

I hope this clears up what I meant, I apologize that I wasn't clearer in the first place. Cheers!


I don't know what you mean by your first example, how does that lead to using undefined data? And your second example is incorrect, if you use an Either in monadic style then each successive function call after the error occurs is doing nothing. It simply returns the existing error from the Either, it doesn't try to run a computation on the non-existent value.




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

Search: