Skip to content

delay extension reduces type detection (for Observable.create series) #249

Description

@samuelchou

It seems that kotlin type recognition for Observable.create extension series will fail, after adding delay functions.

For example, consider code below:

data class User(val id: String, val name: String, val age: Int)

val exampleUser("test0001", "Samuel", 27)

fun getUserById(id: String): Single<User> {
    return Single.create { single ->
         exampleUser.copy(id = id).let { single.onSuccess(it) }
    }
}

Everything works fine.

Now, if I add .delay behind:

fun getUserById(id: String): Single<User> {
    return Single.create { single ->
         exampleUser.copy(id = id).let { single.onSuccess(it) }
    }.delay(500, TimeUnit.MILLISECDONDS)
}

And then build it. The editor would throw build errors at Single.create line:

at end of `Single.create`:
Not enough information to infer type variable T

at start of `single`:
Cannot infer a type for this parameter. Please specify it explicitly.

If I specify the type, the build errors will disappear:

fun getUserById(id: String): Single<User> {
    return Single.create<User> { single ->
         exampleUser.copy(id = id).let { single.onSuccess(it) }
    }.delay(500, TimeUnit.MILLISECDONDS)
}

However, some editor/kotlin plugin might suggest to "Remove explicit type arguments" then.

These errors seems not only happen at Single.create, but also at Observable.create. I guess the errors happen in all .create series?

Versions

I'm using 3.0.1 in Android Studio.

All Rx related version:

implementation 'io.reactivex.rxjava3:rxkotlin:3.0.1'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'com.uber.rxdogtag2:rxdogtag:2.0.1'
implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
implementation 'androidx.room:room-rxjava3:2.4.1'

The editor and plugin version:

Android Studio Chipmunk | 2021.2.1 Patch 1
Build #AI-212.5712.43.2112.8609683, built on May 19, 2022

Kotlin Plugin:
212-1.7.10-release-333-AS5457.46

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions