<sarna>
how do you implement functions that can return a success or a failure? throwing an exception wouldn't be appropriate as failures are expected
<sarna>
I've been thinking about just returning an exception instead of throwing it, but it feels iffy
jvalleroy has joined #ruby
smp has quit [Remote host closed the connection]
smp has joined #ruby
<Momentum>
throwing an exception is the right way to handle exceptions AFAIK
<sarna>
yeah, in normal circumstances where errors are rare. I have failures that are likely to happen
<leftylink>
the most natural fit is Either or Option, but since Ruby doesn't have either of those this isn't an option. there doesn't seem to be one obvious natural fit
<leftylink>
I haven't seen returning an exception being used before but I don't hate the idea any more than other alternatives
<ox1eef_>
Maybe true or false. Or a Result struct (class Result < Struct.new(:success)).
<ox1eef_>
You could implement success? and failure? on top of the struct.
<sarna>
yeah I'm trying not to overdo it with the wrapping, at some point it's more trouble than it's worth..
<ox1eef_>
If I had to avoid exceptions, I would use Result, with: #success?, #failure?, and #failure_reasons or something like that. I would probably want to communicate the why rather than just a generic "it failed".
<ox1eef_>
Keep in mind it would be a custom pattern that might surprise other Ruby programmers.
<sarna>
maybe let me explain my use-case a bit better: I run an action over bundles of data, and then I report (to the user) for which bundles it ran okay, and for which bundles it failed and why
<sarna>
how would you normally do it in ruby?
<sarna>
I can't imagine using exceptions for that, and then rescuing them to push some stuff in the array with run results
<ox1eef_>
Ah I see. I would probably have an array of Result objects then, one for each bundle.
<ox1eef_>
Easy to filter that way as well: results.select(&:success?), results.select(&:failure?).
<sarna>
maybe it could even be more specific, right? not a generic `Result` but something like `RunResult`
<sarna>
not to scare people away with monads
<ox1eef_>
Sure.
<sarna>
I've also been thinking about doing something like `[:ok, 123]` & [:error "it exploded!"]` or `{ok: 123}` & `{error: "it exploded!"}`
<sarna>
but I'm not sure about the ergonomics/rubyness here.. probably too "primitive obsessed"
<ox1eef_>
That works, but probably easier to work with an object.
lena64t has quit [Remote host closed the connection]
lena64t has joined #ruby
cahoots has joined #ruby
<dorian>
z
lena64t1 has joined #ruby
cahoots has quit [Client Quit]
lena64t has quit [Ping timeout: 240 seconds]
joshcom has joined #ruby
infinityfye has joined #ruby
donofrio has quit [Read error: Connection reset by peer]
user23 has quit [Remote host closed the connection]
lena64t1 has quit [Quit: WeeChat 4.1.0-dev]
joshcom has quit [Quit: Leaving]
<johnjaye>
ox1eef_: i just realized I know nothing about how exception handling works in ruby
<johnjaye>
is that what you were talking about
MarvelousWololo has joined #ruby
passbe has quit [Quit: bye...]
passbe has joined #ruby
xuochi has joined #ruby
cata has quit [Quit: WeeChat 3.8]
_ht has joined #ruby
<leftylink>
no, exceptions are for failures are exceptional and not expected. the discussion was about when failures are expected and a regular occurrence
<leftylink>
in such a case exceptions are inappropriate
nkm has joined #ruby
reset has joined #ruby
gh0st6 has joined #ruby
teclator has quit [Ping timeout: 250 seconds]
TomyWork has quit [Remote host closed the connection]
keypresser86 has quit [Remote host closed the connection]
keypresser86 has joined #ruby
polychromata has quit [Ping timeout: 250 seconds]
polychromata has joined #ruby
A_Dragon has quit [Killed (Stx (Happy birthday! (I know its a bit late) She: Yes it does.))]
A_Dragon has joined #ruby
caedmon has joined #ruby
nkm has quit [Quit: Client closed]
nkm has joined #ruby
xuochi has quit [Quit: leaving]
nkm has quit [Quit: Client closed]
gh0st6 has quit [Quit: Connection closed for inactivity]
Laplace has quit [Quit: Connection closed for inactivity]
Linux_Kerio has quit [Ping timeout: 250 seconds]
_ht has quit [Remote host closed the connection]
svdasein has joined #ruby
svdasein_ has joined #ruby
dnadev2 has quit [Ping timeout: 245 seconds]
caedmon has quit [Ping timeout: 245 seconds]
shokohsc5 has quit [Ping timeout: 244 seconds]
shokohsc51 has joined #ruby
infinityfye has quit [Quit: Leaving]
John_Ivan_ has quit [Quit: Disrupting the dragon's slumber one time too often shall eventually bestow upon all an empirical and indiscriminate conflagration that will last for all goddamn eternity.]
John_Ivan has joined #ruby
teclator has joined #ruby
ruby[bot] has quit [Remote host closed the connection]
ruby[bot] has joined #ruby
helge has quit [Ping timeout: 252 seconds]
wnd has quit [Quit: Caught sigterm, terminating...]
wnd has joined #ruby
helge has joined #ruby
desnudopenguino has quit [Remote host closed the connection]
desnudopenguino has joined #ruby
teclator has quit [Ping timeout: 264 seconds]
keypresser86 has quit [Read error: Connection reset by peer]