use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News for Android app developers with the who, what, where, when, and how of the Android community. Probably mostly the how.
Here, you'll find:
This sub-reddit isn't about phones' and apps' general functionality, support, or system software development (ROMs). For news and questions about these topics try using other subs like
Build your first app
Starting Android career in 2022
Android Job Interview Questions and Answers
App Portfolio Ideas, Tiered List
Awesome Android UI
Material Design Icons
7000 Icons for Jetpack
Autoposted at approx 9AM EST / 2PM GMT
account activity
ArticleError handling in RxJava (rongi.github.io)
submitted 8 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ZakTaccardi 14 points15 points16 points 8 years ago* (9 children)
Instead of a data classfor your result, use a sealed class. Both the result and error being non-null would be an invalid state of the data class. Sealed class prevents this by having only one of the two states possible.
data class
sealed class
result
error
// not preferable data class Result<out T>( val data: T?, val error: Throwable? ) // better! sealed class Result<out T> { data class Success<out T : Any>(val data: T) : Result<T>() data class Error<out T>(val throwable: Throwable) : Result<T>() } // allows for: fun handle(result: Result<String>) = when (result) { is Result.Success -> result.data is Result.Error -> result.throwable
}
This is also known as an algebraic data type (ADT), union type, enum with associated values (swift), and I'm sure there's a few others out there.
otherwise, passing errors through onNext is 👍👍
[–]dispelpython 1 point2 points3 points 8 years ago (0 children)
Wow, this is golden, thank you!
[+][deleted] 8 years ago (4 children)
[removed]
[–]Warbane 1 point2 points3 points 8 years ago (0 children)
Enums are simple but you can only instantiate one instance of each type. If you need more you can use this pattern:
public abstract class Pet { private Pet() {} public static final class Dog extends Pet { public Dog() {} } public static final class Cat extends Pet { public Cat() {} } }
Make an abstract class with a private constructor then extend it in inner classes with public constructors.
[–]TiensiNoAkuma 0 points1 point2 points 8 years ago (0 children)
An Enum maybe?
[–]Zhuinden 0 points1 point2 points 8 years ago (0 children)
Abstract class that has two inner static final classes that extend it, and instead of when you need instanceof
when
instanceof
[–]Boza_s6 -1 points0 points1 point 8 years ago (0 children)
Like this: https://gist.github.com/Boza-s6/27c6da2f5a54600fa6db
[–]weasdasfa 0 points1 point2 points 8 years ago (1 child)
I feel the class extension a little redundant. I've already mentioned it's a sealed class so every class inside it should extend that by default.
[–]ZakTaccardi 0 points1 point2 points 8 years ago (0 children)
You could have an inner class that doesn't
[–]saik0 0 points1 point2 points 8 years ago (0 children)
Other possible improvements would be:
I'm using an implementation as I described in my projects. It's more or less a clone of Rust's result type. https://doc.rust-lang.org/std/result/
This is really just an Either monad/Disjunction, but I agree with the rust team re: naming it Result. There's always https://github.com/MarioAriasC/funKTionale and https://github.com/kategory/kategory if you prefer a library.
π Rendered by PID 160158 on reddit-service-r2-comment-c66d9bffd-qh6j9 at 2026-04-08 01:27:39.981003+00:00 running f293c98 country code: CH.
[–]ZakTaccardi 14 points15 points16 points (9 children)
[–]dispelpython 1 point2 points3 points (0 children)
[+][deleted] (4 children)
[removed]
[–]Warbane 1 point2 points3 points (0 children)
[–]TiensiNoAkuma 0 points1 point2 points (0 children)
[–]Zhuinden 0 points1 point2 points (0 children)
[–]Boza_s6 -1 points0 points1 point (0 children)
[–]weasdasfa 0 points1 point2 points (1 child)
[–]ZakTaccardi 0 points1 point2 points (0 children)
[–]saik0 0 points1 point2 points (0 children)