How to pass generic interface to Kotlin function?

Hello!

I have an interface within an SDK I can’t change:

package com.abc.whatever;

public interface Function<T1, R> {
  R call(T1 var1);
}

I have a working JAVA class, here is the excerpt:

package com.mycompany.stuff;

import com.abc.whatever.Function1;

public class AuthManager {

  [...]

  public boolean isAuthed() {
    Boolean isAuthed =
      getAuthPolicyStore(authPolicyStore -> authPolicyStore.getBoolean(IS_AUTHED));
    if (isAuthed != null) {
      return isAuthed;
    } else {
      return false;
    }
  }

  public <T> T getAuthPolicyStore(@NonNull Function1<SecureKeyValueStore, T> function) {
    Objects.requireNonNull(function);
    synchronized (AUTH_STORE_LOCK) {
      try {
        openAuthStore();
        return function.call(authPolicyStore);
      } catch (OpenFailureException | EncryptionError e) {
        LOGGER.error("Error: ", e);
        return null;
      } finally {
        authPolicyStore.close();
      }
    }
  }
}

And here is the Kotlin version of my java class:

var isAuthed: Boolean = false
  private set
  get() {
    return getAuthPolicyStore{ AuthStore: SecureKeyValueStore ->
        AuthStore.getBoolean(_isAuthed)
    } ?: false
  }

fun <T> getAuthPolicyStore(function: (SecureKeyValueStore) -> T?) : T? {
  synchronized(AUTH_STORE_LOCK) {
    return try {
      openAuthStore()
      function.call(AuthStore)
    } catch (e: OpenFailureException) {
      LOGGER.error("Error: ", e)
      null
    } catch (e: EncryptionError) {
      LOGGER.error("Error: ", e)
      null
    } finally {
      AuthStore.close()
      null
    }
  }
}

Now, in the Kotlin version of getAuthPolicyStore() I got a linter error which says:
Unresolved reference: call to line function.call(AuthStore).

What could be wrong?

Regards,
roncsak

It looks like there’s really no need for a function interface here. You can just write function(AuthStore)

1 Like

This helped fixing the syntax! Thank you!