i'd imagine it'd be better if `fizz` returned null...
# announcements
h
i'd imagine it'd be better if
fizz
returned nullable, then
Copy code
foo.fizz()?.log()
w
i don't have control over this api but maybe i can use an extension method to help me achieve that
h
yeah, that's what I often do
t
What if it's not an error? Does it just have a side effect or does it return a value?
w
there are side effects, only the result is returned (error type)
h
I assume it's a java library or something, and it's an error getter or something
w
no its not an error getter these are methods called for their side effects but an error type is returned
h
so i'd personally write an extension property:
Copy code
val Foo.error get() = foo.fizz().let { if (it.isNotNone()) it else null }
or something oh, alright. well, idk. examples.
t
You could do something like a
handle
function that puts all the error handling in one place. Then it's just
Copy code
handle(foo.fizz())
handle(foo.buzz())
...
Or an extension on the error types and
Copy code
foo.fizz().handle()
foo.buzz().handle()
w
thats what i've done ^
thinking about something like this too but not practical unless i can make them each lazy
Copy code
handle(
    foo.fizz(),
    foo.buzz()
)
...

    private fun handle(vararg operations: Error) {
        operations.forEach { op ->
            if (op != Error.NONE ) {
                // ...
            }
        }
    }
t
Yeah. You could do a
block
there to solve the lazy issue, but you'd still have to wrap each call in some kind of function to check the result.
w
@Toddobryan how would a block help me here? i'm not seeing how i could use that to wrap each call individually
t
No, you can't. That's what I meant.
w
ah ok. well i definately don't need to do anything fancy like that, just thought it would be cool if i could
t
Copy code
someFun {
   foo.fizz()
   foo.buzz()
   ...
}
means the functions don't need to be lazy and lets you exit early on a severe-enough error, but doesn't help at all with the fact that you'd still have to handle each error individually somehow. 😞
w
yeah exactly thats what i couldnt figure
oh well 🤷