DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Learning Kotlin: Invoke

Learning Kotlin: Invoke

Want to learn more about type of operators in Kotlin? Check out this tutorial to learn more about using the Invoke method in Kotlin.

Robert Maclean user avatar by
Robert Maclean
·
Sep. 25, 18 · Tutorial
Like (3)
Save
Tweet
Share
19.19K Views

Join the DZone community and get the full member experience.

Join For Free

Today, we tackle a weird operator, invoke, which lets an instance of a class have a default function that I am not sure I've ever seen any language do. So, let us explore this with a simple example. We have a config class that returns the configuration for something, like this:

class Config {
fun get():String {
// do stuff
return "stuff"
}
}

fun main(args: Array<String>) {
val config = Config()
println(config.get())
}


Now, in our world, maybe get is the primary use, so we can actually make it that the instance config (line 9) can be called to get it:

class Config {
operator fun invoke(): String {
return this.get();
}

private fun get():String {
// do stuff
return "stuff"
}
}

fun main(args: Array<String>) {
val config = Config()
println(config())
}


Note that we add a new operator (line 2), and that calls the private get. It didn't need to be private, but I thought this would be cleaner, and now on line 14, we can just call the instance itself.

Now, you may be thinking, "nice, but so what — saving a few keystrokes isn't too awesome." Well, invoke can return anything, including itself, which opens up something crazy!

class Config {
var count = 0;
operator fun invoke(): Config {
count++
return this
}
}

fun main(args: Array<String>) {
val config = Config()
config()()()()()()()()()()
println("config was called ${config.count} times")
}


This will print out config was called 10 times. That is a lot more interesting, so let us ramp up another level and pass parameters to invoke:

class Config {
var word = ""
operator fun invoke(s: String): Config {
word += s
return this
}
}

fun main(args: Array<String>) {
val config = Config()
config("R")("o")("b")("e")("r")("t")
println(config.word)
}


While I do not know yet where I would use this myself, I do use invoke all the time. Since it is what makes lambdas possible in Kotlin because when we create a lambda, we get an object that is invoked with, well, invoke.

Kotlin (programming language)

Published at DZone with permission of Robert Maclean, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Recent Amazing AI Advancements
  • Introduction to Cloud Native
  • MongoDB Time Series Benchmark and Review
  • Unlocking the Power of ChatGPT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: