• On fun and val: • Generates a static method in the bytecode that delegates through to that function/property • On var: • Also generates a static setter that delegates through
save */ fun save(obj: T) }D class UserRepository implements Repository<User> {A @OverrideB public void save(User obj) {C throw new IOException("");D }E }F
save */ fun save(obj: T) }D class UserRepository implements Repository<User> {A @OverrideB public void save(User obj) throws IOException {C throw new IOException("");D }E }F
save */ fun save(obj: T) }D class UserRepository implements Repository<User> {A @OverrideB public void save(User obj) throws IOException {C throw new IOException("");D }E }F
save */ fun save(obj: T) }D class UserRepository implements Repository<User> {A @OverrideB public void save(User obj) throws IOException {C throw new IOException("");D }E }F
save */ @Throws(IOException::class) fun save(obj: T) }D class UserRepository implements Repository<User> {A @OverrideB public void save(User obj) throws IOException {C throw new IOException("");D }E }F
dereferenced • Most calls into Java will return a platform type • You should try to eliminate most/all of these in your own code • Solution: Nullability Annotations
interface in Java • () -> R becomes Function0<R> • (T) -> R becomes Function1<T, R> • Java SAMs compile to a special syntax in Kotlin • SAMName { ... }
cases in Java • It cannot, however, if Unit is the selected type for a generic • Ex: Lambdas. Java signature FunctionN<Args, Unit> • Java has to: return Unit.INSTANCE;
to say if they are: • Covariant: ? extends Foo • Contravariant: ? super Foo • In Kotlin, you just put that declaration on the type param itself: • Covariant: out T • Contravariant: in T
And in is roughly equivalent to super • Kotlin "fakes" declaration-site variance for Java by generating wildcards for all variant generics in parameters • Return types remain invariant • Final covariant types remain invariant
Auto-generation of hashCode(), equals(), toString() • These work perfectly in Java • Auto-generation of copy(...) • This works okay in Java • Lack of default + named params makes it clunky
String? = null, val username: String? = null, val gender: String? = null, val points: Int = 0 ) { fun toBuilder() = Builder(this) class Builder(private var user: User = User()) { fun id(id: String?) = apply { user = user.copy(id = id) } fun name(name: String?) = apply { user = user.copy(name = name) // ... fun build() = user } } Data classes
String? = null, val username: String? = null, val gender: String? = null, val points: Int = 0 ) { fun toBuilder() = Builder(this) class Builder(private var user: User = User()) { fun id(id: String?) = apply { user = user.copy(id = id) } fun name(name: String?) = apply { user = user.copy(name = name) // ... fun build() = user } } Data classes
String? = null, val username: String? = null, val gender: String? = null, val points: Int = 0 ) { fun toBuilder() = Builder(this) class Builder(private var user: User = User()) { fun id(id: String?) = apply { user = user.copy(id = id) } fun name(name: String?) = apply { user = user.copy(name = name) // ... fun build() = user } } Data classes
String? = null, val username: String? = null, val gender: String? = null, val points: Int = 0 ) { fun toBuilder() = Builder(this) class Builder(private var user: User = User()) { fun id(id: String?) = apply { user = user.copy(id = id) } fun name(name: String?) = apply { user = user.copy(name = name) // ... fun build() = user } } Data classes
String? = null, val username: String? = null, val gender: String? = null, val points: Int = 0 ) { fun toBuilder() = Builder(this) class Builder(private var user: User = User()) { fun id(id: String?) = apply { user = user.copy(id = id) } fun name(name: String?) = apply { user = user.copy(name = name) // ... fun build() = user } } Data classes
the time, interop Just Works™ • But when writing non-private members, say to yourself: • When writing Kotlin: "How will this look in Java?" • When writing Java: "How will this look in Kotlin?"
other language • If you use Java, write some Kotlin tests • If you write Kotlin, write some Java tests • Gives you insight into the ergonomics of your public API