Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace param "t" to "className"?

Android Studio 3.2.1
Kotlin plugin version 1.3.11-release-Studio3.2-1
ext.kotlin_version = '1.3.11' 
RxJava 1.3.0 

For example when I type in *.kt file: Observable.unsafeCreate{} and then press Ctrl+Space between {} , then IntellijIdea suggest me to type symbol t ->, but when I do the same in .java file result is subscriber -> :

*.kt file:

fun anyFunc(): Observable<Boolean>{
    return Observable.unsafeCreate { t -> t }
}

enter image description here

*.java file:

public void anyFunc() {
    Observable.unsafeCreate(subscriber -> {});
}

How to make in Kotlin file the same behavior of suggestion typing like in Java file?


[UPDATE] Seems it is because of call() method in rx.functions.Func1, it has param t:

public interface Func1<T, R> extends Function { 
    R call(T t); 
}
like image 852
NickUnuchek Avatar asked Jan 18 '19 08:01

NickUnuchek


1 Answers

I'm using

Android Studio 3.2.1,

rxjava:2.2.0,

Kotlin plugin version 1.3.11-release-Studio3.2-1

After typing in *.kt file: Observable.unsafeCreate{} and pressing Ctrl+Space between {} Android Studio shows me next suggestion:

enter image description here

The same suggestion Android Studio shows when I'm typing in *.java file.

I didn't make any configuration changes in Android Studio Preferences.

I guess you are importing Observable from rx - the first item in the completion list on the image below. Try to import Observable from io.reactivex - highlighted item in the completion list below, it may help: import io.reactivex.Observable.

enter image description here

To use it you need to import rxjava2:

implementation 'io.reactivex.rxjava2:rxjava:2.2.0'

EDIT:

As was figured out code completion dialog showed t -> because parameter in Action1.call(T t) called t. We can see it if we look at the signature of rx.Observable.unsafeCreate method in RxJava 1.3:

public static <T> Observable<T> unsafeCreate(OnSubscribe<T> f) {
    return new Observable<T>(RxJavaHooks.onCreate(f));
}

OnSubscribe interface extends Action1<Subscriber<? super T>, and Action1 has next signature:

public interface Action1<T> extends Action {
    void call(T t);
}

So parameter is called t and Android Studio suggests it as t ->.

In RxJava2 we have different signature:

public static <T> Observable<T> unsafeCreate(ObservableSource<T> onSubscribe) {...}

public interface ObservableSource<T> {
    void subscribe(@NonNull Observer<? super T> observer);
}

We see that in ObservableSource.subscribe() method parameter is called observer, so we see it as observer->.

Conclusion: IDEA's suggestion is based on the parameter's name of the method of Functional interface you are implementing as lambda.

like image 69
Sergey Avatar answered Sep 22 '22 07:09

Sergey