Skip to main content

Function type aliases in public API

DiagnosticFUNCTION_TYPE_ALIAS_PUBLIC_API
Default severityError
Gradle propertyfunctionTypeAliasPublicApi
Exemption@IntentionallyFunctionTypeAlias

What it reports​

A type alias in the public API that expands to a function type.

public typealias ProgressHandler = (Int) -> Unit

Rationale​

Compiled callers use the expanded function type, so a signature change breaks compatibility. A fun interface gives the callback a named type that can have additional members or hierarchy.

Don't​

public typealias Callback = (Int) -> Unit

Do​

public fun interface Callback {
public fun onCall(value: Int)
}

Notes​

  • Function types nested in another type, such as List<(Int) -> Unit>, aren't reported.
  • Reflection function types, such as KFunction1 and KSuspendFunction2, aren't reported.
  • Aliases inside a @PublishedApi internal class aren't reported.

Exemption​

If this is intended, apply @IntentionallyFunctionTypeAlias to the type alias.

Use the exemption when the declaration is part of the API design or must stay for backwards compatibility.

@IntentionallyFunctionTypeAlias(reason = ExemptionReason.API_DESIGN)
public typealias Callback = (Int) -> Unit

Configuration​

build.gradle.kts
apiWatchdog {
functionTypeAliasPublicApi = WatchdogSeverity.ERROR
}

For a direct compiler call:

-P plugin:org.jetbrains.kotlinx.library.api.watchdog:diagnosticSeverity=FUNCTION_TYPE_ALIAS_PUBLIC_API:error

See also​