Skip to Content
Java to Kotlin
book

Java to Kotlin

by Duncan McGregor, Nat Pryce
August 2021
Intermediate to advanced
422 pages
10h 18m
English
O'Reilly Media, Inc.
Content preview from Java to Kotlin

Chapter 16. Interfaces to Functions

In Java, we use interfaces to specify a contract between code that defines some functionality and code that needs it. Those interfaces couple the two parties together, which can make our software harder to maintain. How do function types help solve this problem?

Imagine, if you can, that you need to send email from some code that you are writing. Just that for now—not receive mail, or list sent messages—just fire and forget.

The code that describes the email is simple enough:

data class Email(
    val to: EmailAddress,
    val from: EmailAddress,
    val subject: String,
    val body: String
)

Given an Email, client code would like to call the simplest possible function to send it, which is:

fun send(email: Email) {
    ...
}

Of course when we come to implement this function, we discover that to actually send email, we require all sorts of other information. Not information about the email itself but, rather, configuration about how to send it. Things like the sending server’s hostname and security credentials—all the things that your nontechnical relative doesn’t know, but you need to set up their new computer. We’ll add three extra parameters to sendEmail to stand in for all this configuration:

fun sendEmail(
    email: Email,
    serverAddress: InetAddress,
    username: String,
    password: String
) {
    ...
}

As a client, things have just become a lot less convenient. Everywhere we want to send email has to know this configuration; we’ll be passing it around from the top ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java to Kotlin

Java to Kotlin

Duncan McGregor, Nat Pryce
Head First Kotlin

Head First Kotlin

Dawn Griffiths, David Griffiths

Publisher Resources

ISBN: 9781492082262Errata Page