I'd like to rewrite some of my code to Kotlin. I pasted the working code first to show the origin.
I tried
The following is a working excerpt written in Java:
Function1.class (part of a framework):
public interface Function1<T1, R> {
R call(T1 var1);
}
Authmanager.class:
public boolean isAuthed() {
Boolean isAuthed = getWithAuthPolicyManager(
authPolicyManager -> authPolicyManager.getBoolean(IS_AUTHED)
);
if (isAuthed != null) {
return isAuthed;
} else {
return false;
}
}
public <T> T getWithAuthPolicyManager(@NonNull Function1<AuthManager, T> function) {
Objects.requireNonNull(function);
synchronized (AUTH_POLICY_LOCK) {
try {
openAuthPolicyManager();
return function.call(authPolicyManager);
} catch (OpenFailureException | EncryptionError e) {
LOGGER.error("Error:", e);
return null;
} finally {
authPolicyManager.close();
}
}
}
I would like to rewrite it to Kotlin but I cannot:
Authmanager.kt
fun isAuthed(): Boolean {
val isAuthed =
getWithAuthPolicyManager<Boolean>({
authPolicyManager -> authPolicyManager.getBoolean(IS_AUTHED)
})
return isAuthed ?: false
}
fun <T> getWithAuthPolicyManager(function: Function1<AuthManager, T>) : T? {
synchronized (AUTH_POLICY_LOCK) {
return try {
openAuthPolicyManager();
return function.call(authPolicyManager);
} catch (OpenFailureException | EncryptionError e) {
LOGGER.error("Error", e);
null;
} finally {
authPolicyManager.close();
null
}
}
}
Actually, I just copy-pasted the Java code to the Kotlin class to automatically convert to Kotlin. However, the linter says:
Type mismatch.
Required: Function1<AuthManager, Boolean>
Found: (AuthManager) -> Boolean!
to this line getWithAuthPolicyManager<Boolean>({authPolicyManager -> authPolicyManager.getBoolean(IS_AUTHED)}) in fun isAuthed().
Do you have any idea how this could be fixed?