Re:Declined Proposal: A built-in Go error check function, “try”
by gigatexal on Wednesday July 17, 2019 @01:57AM
(#20456879)
I thought it could lead to doing method chaining for a fluent like API which I find cleaner than how things work now.
[ Parent
| on HN ]
Re:Declined Proposal: A built-in Go error check function, “try”
by natefinch on Wednesday July 17, 2019 @08:00AM
(#20458470)
I really dislike method chaining. I'd much rather have 5 lines than 5 chained methods. If that's too much to read, you can always encapsulate it in a well-named function.
Re:Declined Proposal: A built-in Go error check function, “try”
by gigatexal on Wednesday July 17, 2019 @11:06AM
(#20460225)
But five functions that return a value and an error would each have to run the if err != nil dance whereas with method chaining it's cleaner
Re:Declined Proposal: A built-in Go error check function, “try”
by NateDad on Wednesday July 17, 2019 @04:06PM
(#20463423)
not if each one can fail. What if call #1 and call #3 can return the same error.. how does the caller know which one failed? This is the same as wrapping a bunch of calls with a catch (Exception) ... you lose context of what failed and can't behave differently for different failures. All you can do is perform a generic "something went wrong" behavior.
Re:Declined Proposal: A built-in Go error check function, “try”
by mikeschinkel on Thursday July 18, 2019 @05:59PM
(#20474315)
Interestingly you _can_ implement method chaining to require a terminal method call i.e. err := GetFoo().SetBar(1).SetBaz(2).Run() and then each chained method would set an error property in the object and if err!=nil then do nothing but return the object, and then the last method could return the error value.
That said, I am not a huge fan of fluent interfaces. I much prefer passing in a struct as an "args" parameter, in most case (but not all.)