The decision
A third-party contact directory supplies a mixed list. A partner badge must include the partner code even when the variable’s declared type is Contact. Other contacts display only their name. Keep formatting outside the directory classes.
Read the Kotlin
Use the declared receiver type to select an extension
The type before the dot in an extension declaration is its receiver. Think of an extension call as a regular function call whose first argument is written on the left. The compiler selects the overload using the type known at the call site. Giving the same object a wider declared type can change which extension gets called.
fun CharSequence.kind() = "characters"
fun String.kind() = "string"
val broad: CharSequence = "abc"
broad.kind() // "characters"Check the member’s signature before the extension
An applicable member with the same call shape wins over an extension when the caller recompiles. Check the parameters as well as the name. If a new member takes no arguments, an existing one-argument call can still resolve to its matching extension. A library upgrade can therefore change one call while leaving its neighbor alone.
Choose where runtime variation belongs
With one extension on Contact, a type check inside the function can choose formatting for a Partner at runtime. That works for variants you know today. If outside teams should supply their own policies, declare the operation as an interface member and let implementations override it. Calls on Contact-typed elements then use normal virtual dispatch.
The full example
class Lesson {
interface Contact { String name(); }
record Guest(String name) implements Contact {}
record Partner(String name, String code) implements Contact {}
static String badge(Contact contact) {
if (contact instanceof Partner p) {
return p.name() + " [" + p.code() + "]";
}
return contact.name();
}
}interface Contact { val name: String }
data class Guest(override val name: String) : Contact
data class Partner(override val name: String, val code: String) : Contact
fun Contact.badge(): String = when (this) {
is Partner -> "$name [$code]"
else -> name
}Inputs and outcomes
| Input | Outcome | Why it matters |
|---|---|---|
Guest("Jo") | Jo | An ordinary contact has no code. |
Contact variable holding Partner("Lin", "EU") | Lin [EU] | The partner’s runtime type matters to the label. |
Walk through the code
- Contact.badge declares a receiver, so a caller writes contact.badge(). Inside the function, this is that Contact; name is shorthand for its name property.
- Overloaded extensions are selected using the receiver’s declared type. With a Contact-typed variable the compiler selects Contact.badge even when the actual object is a Partner.
- The single extension uses a runtime is check and smart cast. Inside the Partner branch the extra code property is available. Java’s instanceof pattern provides the same runtime check and narrowing.
The Java habit
Adding a second fun Partner.badge() does not override fun Contact.badge(). This can make a mixed directory print a different label for the same partner depending on the variable type.
When to choose differently
If you own a hierarchy and the operation belongs to every subtype, a virtual member lets implementations choose their own behavior. An external type check must be revisited as new special cases appear.