DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Kotlin Generic Extension Functions

Kotlin Generic Extension Functions

This quick guide to writing generic extension functions in Kotlin is useful for calling functions based on your default value's types.

Ravi Hasija user avatar by
Ravi Hasija
·
Oct. 03, 17 · Java Zone · Tutorial
Like (5)
Save
Tweet
8.17K Views

Join the DZone community and get the full member experience.

Join For Free

I have been educating myself in Kotlin recently, and one of the most powerful features that Kotlin provides is extension functions. The simplest example of an extension function that I can come up with is: fun String.addOne() = this + "1" 

And then, println("Boba".addOne()) leads to an output of: Boba1

So above we have added a new function to the String class.

Generic Extension Functions

With the basic introduction out of the way, I wanted to write a generic extension function. We use Netflix OSS at work and are constantly getting property values via code that looks like this:

DynamicPropertyFactory.getInstance().getStringProperty("SOME_CONFIG_PROP_NAME", "I am a good default value").get()


Basically, the above code allows us to fetch (dynamically) values of a property, SOME_CONFIG_PROP_NAME in this example, and provide a default value, I am a good default value in this case.

We have several variations of this, for example:

DynamicPropertyFactory.getInstance().getIntProperty 
DynamicPropertyFactory.getInstance().getFloatProperty 
DynamicPropertyFactory.getInstance().getDoubleProperty 
DynamicPropertyFactory.getInstance().getLongProperty 
DynamicPropertyFactory.getInstance().getBooleanProperty


So on and so forth. I wanted to have a generic extension function available on the String class because property names are always of the String type. Here's the generic function:

fun  String.getConfig(default: T): T {  
    val dynamicPropertyFactory = DynamicPropertyFactory.getInstance()
    return 
      when (default) {
         is String  -> dynamicPropertyFactory.getStringProperty(this, default).get()
         is Int     -> dynamicPropertyFactory.getIntProperty(this, default).get()
         is Float   -> dynamicPropertyFactory.getFloatProperty(this, default).get()
         is Double  -> dynamicPropertyFactory.getDoubleProperty(this, default).get()
         is Boolean -> dynamicPropertyFactory.getBooleanProperty(this, default).get()
         is Long    -> dynamicPropertyFactory.getLongProperty(this, default).get()
         else       -> default
      }
}


Kotlin would know based on the input param what "type" it is. And we can use that power to use the when expression, along with is, to check what type it is and call the appropriate function based on the type of the default value.

So now our code can become as simple as:

"some.config.name".getConfig("I am a good default value")

"call.external.service".getConfig(false)

Boom. Done.

Kotlin (programming language)

Published at DZone with permission of Ravi Hasija, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Concise Guide to DevSecOps and Their Importance in CI/CD Pipeline
  • APIs Are Now at the Center of Digital Transformation
  • CockroachDB TIL: Volume 8
  • Clojure: Destructuring

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo