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
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
  1. DZone
  2. Data Engineering
  3. Data
  4. Learning Kotlin: The Lazy Delegate

Learning Kotlin: The Lazy Delegate

Want to learn more about using the Lazy delegate in Kotlin? Check out this tutorial to learn more about using the by operator and delegates in Kotlin.

Robert Maclean user avatar by
Robert Maclean
·
Sep. 03, 18 · Tutorial
Like (1)
Save
Tweet
Share
8.96K Views

Join the DZone community and get the full member experience.

Join For Free

Following from our introduction to the by operator and delegates, this post looks at the first of five built-in delegates, lazy. Lazy property evaluation allows you to set the initial value of a property the first time you try to get it. This is really useful for scenarios where there is a high cost of getting the data. For example, if you want to set the value of a username, which requires a call to a microservice, but since you don't always need the username, you can use this to initial it when you try and retrieve it the first time.

Setting up the context for the example, let us look at two ways you can do this now. The first way is you call the service in the constructor, so you take the hit immediately — regardless if you ever need it. It is nice and clean though.

class User {
val name: String

constructor(id:Int){
// load service ... super expensive
Thread.sleep(1000)
name = "Robert"
}
}

fun main(args:Array<String>) {
val user = User(1)
println(user.name)
}


The second solution is to add a load function, so we need to call that to load the data. This gets rid of the performance hit, but it is less clean. If you forget to call load, your code is broken. You may think that will never happen to you, but I just did it while writing the sample code. It took me less than two minutes to forget I needed it. 

Another pain is I need to make my name property variable since it will be assigned at a later point.

class User(val id: Int) {
var name: String = ""

fun load() {
// load service ... super expensive
Thread.sleep(1000)
name = "Robert"
}
}

fun main(args:Array<String>) {
val user = User(1)
user.load()
println(user.name)
}


The solution to this is obviously lazy — this gives us the best of all of the above. It provides clean code and no performance hits unless I need it, in which case, I can use val  with none of the downsides.

class User(val id: Int) {
val name: String by lazy {
// load service ... super expensive
Thread.sleep(1000)
"Robert"
}
}

fun main(args:Array<String>) {
val user = User(1)
println(user.name)
}
Kotlin (programming language) Property (programming) microservice Data (computing) Evaluation POST (HTTP) Operator (extension)

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

  • What Is Testing as a Service?
  • The Real Democratization of AI, and Why It Has to Be Closely Monitored
  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • Silver Bullet or False Panacea? 3 Questions for Data Contracts

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: