https://kotlinlang.org logo
#announcements
Title
# announcements
h

Hullaballoonatic

11/02/2019, 8:34 PM
i'd imagine it'd be better if
fizz
returned nullable, then
Copy code
foo.fizz()?.log()
w

william

11/02/2019, 8:35 PM
i don't have control over this api but maybe i can use an extension method to help me achieve that
h

Hullaballoonatic

11/02/2019, 8:36 PM
yeah, that's what I often do
t

Toddobryan

11/02/2019, 8:36 PM
What if it's not an error? Does it just have a side effect or does it return a value?
w

william

11/02/2019, 8:38 PM
there are side effects, only the result is returned (error type)
h

Hullaballoonatic

11/02/2019, 8:38 PM
I assume it's a java library or something, and it's an error getter or something
w

william

11/02/2019, 8:39 PM
no its not an error getter these are methods called for their side effects but an error type is returned
h

Hullaballoonatic

11/02/2019, 8:39 PM
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

Toddobryan

11/02/2019, 8:40 PM
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

william

11/02/2019, 8:41 PM
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

Toddobryan

11/02/2019, 8:53 PM
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

william

11/02/2019, 8:57 PM
@Toddobryan how would a block help me here? i'm not seeing how i could use that to wrap each call individually
t

Toddobryan

11/02/2019, 8:57 PM
No, you can't. That's what I meant.
w

william

11/02/2019, 8:59 PM
ah ok. well i definately don't need to do anything fancy like that, just thought it would be cool if i could
t

Toddobryan

11/02/2019, 8:59 PM
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

william

11/02/2019, 9:05 PM
yeah exactly thats what i couldnt figure
oh well 🤷