The God-level Kotlin Function

Infix, Extension, Operator, and Lambda function

Nishant Aanjaney Jalan
CodeX
3 min readJul 4, 2022

--

I was writing a Custom View for a company last year and I produced the God-level function, where you use every single type of function that Kotlin has given us. There is a minor prerequisite to understanding this article better — you should know how to animate values inside of a custom view. If you are not aware of this topic, you can have a brief skim over this article I published a few days back:

Photo by Adrien Converse on Unsplash

Kotlin introduces many different types of functions:

  1. Infix functions
  2. Extension functions
  3. Lambda functions
  4. Operator functions

and more…

I would like to tell to how I managed to use all of those 4 functions in one line.

Introduction

I needed to animate values in a Custom View, so I used the ValueAnimator class to do so.

This will animate the value of myFloat from 0 to 10 in 10 seconds using the Accelerate Deaccelerate Interpolator (I will have an interesting article on interpolators published soon).

As the complexity of this Custom view grew, I got tired of writing the same code again and again. I needed to extract the common code.

Development Process

There are two values here that I need: the starting value and the ending value. Since I have worked with Kotlin extensively, I knew that Pair<Float, Float> would be the perfect class to host these 2 values. As a bonus point, the extension infix function to makes the code readable.

Okay, so currently I have something that looks like 0f to 10f; but this does not do anything; it only created an object. I can use the invoke operator function to run some code that I want. This invoke operator function must exist on the Pair object for the animation to work. Hence, an extension function.

So now we have an extension function of the Pair object which is an operator function, invoke. So, if I run the code (0f to 10f)(), This function shall execute thereby starting the animation.

This was a Custom View that I was developing. I needed the instance of the view to invoke the invalidate function to redraw the Canvas. Also, we need the duration and the interpolator for this animation. I added these to my function parameters. Furthermore, I also need a callback function that would update the value of the variable I want to update; this calls for a Lambda function

Finally, I had everything I need. Working with this, I made the discovery that I used 4 different types of functions in just one single line!

My god-level function

Conclusion

The flexibility that Kotlin allows you is unmatched. It is outstanding how you can reduce a large boilerplate code into a single liner that isn’t different to understand as it reads like English.

I hope you enjoyed reading my article and learnt something. Thank you! ✌️

--

--

Nishant Aanjaney Jalan
CodeX

SWE Intern@JetBrains | UG Student | CS Tutor | Android & Web Developer | OCJP 8 | https://cybercoder-naj.github.io