2

Since sealed is like enum object so i decided to use sealed class for network response, were it contains success or failure if it is success it contains data else error message
Example

  sealed class Result {
            sealed class Success : Result() {
                data class F1(val data: Any) : Success()
                data class F2(val data: Any) : Success()
            }

            sealed class Error : Result() {
                data class F1(val error: Any) : Error()
                data class F2(val error: Any) : Error()
            }
        }

the above Result class has either Success or Failure

 getSomeDataFromService()
                .filter {
                    it is Result.Success.F1
                }
                .map { it: Result
                    /*
                    i face problem here,my need is F1 data class but what i
                     got is Result ,i know that i can cast here, 
                    but i am eager to know for any other solution other than casting  
                     */


                }

}

is there is any other solution ?
Any help

Stack
  • 1,164
  • 1
  • 13
  • 26

1 Answers1

2

Assuming getSomeDataFromService() returns a Single, you should be able to utilize Maybe.ofType():

getSomeDataFromService()
    .toMaybe()
    .ofType(Result.Success.F1::class.java)
    .map {
        ...
    }

If getSomeDataFromService() returns an Observable, you can use Observable.ofType() directly.

tynn
  • 38,113
  • 8
  • 108
  • 143
  • yeah that's good, but what for the case if i want to check with multiple conditions ? – Stack May 09 '19 at 07:52
  • You could try to utilize some of the many `flatMap*()` operations. But you have to consider that the stream itself can only contain one type. If you're mapping to a single type only, it might be worth having the type-checks within the `map()` operation. – tynn May 09 '19 at 08:01