If a Java functional interface has its type parame...
# announcements
d
If a Java functional interface has its type parameter used invariantly, but it can't declare it as intravariant, how can I make a class that implements it with the type parameter in invariant position? Expanded in a thread with code.
given this JAVA functional interface, which cannot declare its type parameter T as invariant -
Copy code
public interface SingleResultCallback<T> {
    /**
     * Called when the operation completes.
     * @param result the result, which may be null.  Always null if e is not null.
     * @param t      the throwable, or null if the operation completed normally
     */
    void onResult(T result, Throwable t);
}
I want to implement this by wrapping a
Continuation<T>
- and I want the type parameter to be in invariant position.
Copy code
class CallbackImpl<in T>(val cont: Continuation<T>) : SingleResultCallback<T> {
    override fun onResult(result: T, error: Throwable?) {
        if (error != null) {
            cont.resumeWithException(error)
        } else {
            cont.resume(result)
        }
    }
}
However, the
in
produces an error when I try to implement SingleResultCallback
Can I make the compiler trust me?
😂
Btw, SingleResultCallback is from com.mongodb.async.client
Not sure this is the right approach..
p
typealias can be an option https://pl.kotl.in/H1zL1Ebt4
👌🏻 1