7 ms·
do you know of any languages that have a sort of inverted switch statement to clean up a list of if statements like this? something like: invswitch err !=
by cinger 13y ago
do you know of any languages that have a sort of inverted switch statement to clean up a list of if statements like this?
something like:
invswitch err !=nil {
case 'err := binary.Write(w, binary.LittleEndian, int32(len(g.Name)))': return err
case _, err = w.Write([]byte(g.Name)): return err
case err = binary.Write(w, binary.LittleEndian, g.Age): return err
default: return binary.Write(w, binary.LittleEndian, g.FurColor)
}
- stass 13y agoYes, CPS is essentially that and available in any modern language, or can be emuated by setcontext(2)/getcontext(2). However, it can become a mess very quickly. Another option is monadic style, which will pass error checking along. I believe it will work well for this example, and can be implemented in Go, most likely (I don't know Go, bur it seems so). Of course, the real problem with this code is that it is not decomposed properly. Nested error checking is the first sign of it, as somebody else already rightfully noted in the thread.
- pcwalton 13y agoI think you'd probably need generics to implement monadic chaining in Go.
- pcwalton 13y agoThis is similar to what Haskell do-notation provides.