Kotlin Functions

This post is part of a series of posts about why I’m considering Kotlin for my new projects where I would normally choose Java. If you haven’t read the other posts, check them here: Let’s talk about Kotlin?

Igor Leal
3 min readFeb 9, 2018

If you come from Java you know that methods (let's just call if functions from now one due to how Kotlin names it) are pretty basics. Maybe the big feature it has is varargs. Yes, varargs is great! But many other languages are moving forward and helping developers with a lot of features and Kotlin brought some of them for us.

Expression Body

Let's start with something simple when you look at first, but after using it you will realize that it gives you a good velocity. You know when you need to create a method on your controller that will just call a service method? Or a method on your service that just check some info and returns a boolean? You would usually code something like this, right?

fun isUserValid(user: User): Boolean {
return user.document.matches(Regex.fromLiteral("/^(\\d{2}\\.?\\d{3}\\.?\\d{3}\\/?\\d{4}-?\\d{2})\$/"))
}

But Kotlin gives you the possibility of just saying that the body of isUserValid is an expression. The code would look like this:

fun isUserValid(user: User): Boolean =
user.document.matches(Regex.fromLiteral("/^(\\d{2}\\.?\\d{3}\\.?\\d{3}\\/?\\d{4}-?\\d{2})\$/"))

This is simple but is really helpful when you need velocity and even makes the code cleaner when you have a few functions on your file that have just one line. It looks like you are just writing expressions, not functions.

Default Arguments

If you come from a Python background you might think that this is child's play but if you come from a Java background you will be surprised. Sometimes you have have a function to do something (let's say it's math) and your function receives two parameters. But, one of these parameters is not really required, because sometimes you won't pass it. What would you do in Java? Probably something like this:

fun pow(x: Double, exponent: Double) : Double = Math.pow(x, exponent)fun pow(x: Double) : Double = pow(x, 2.0)

By doing this you can call pow function with x and exponent or just passing x assuming that the exponent will be 2. Kotlin's feature of default arguments allows you to do the same thing without the need of writing a new function. It looks like this:

fun pow(x: Double, exponent: Double = 2.0) : Double = Math.pow(x, exponent)

This code creates only one function but you can call it both with one or two parameters, having the same output of the code above.

Operator Overloading

Last but not least the operator overloading. This is a powerful feature. When you have two values, one Long and one BigDecimal, you can't add them. You can't do the following code:

val val1 = BigDecimal.TEN
val val2 = 1L
// BigDecimal.plus doesn't have a definition that accepts a Long parameter
val val3 = val1 + val2
// Long.plus doesn't exist
val val4 = val2 + val1

Operator overloading gives you the power to implement a method and add it (at your project's scope) to a class that is not yours. For example, you can implement BigDecimal.add(Long) and Long.add(BigDecimal). This is how you do it:

operator fun BigDecimal.plus(val2: Long) = BigDecimal.valueOf(this.toDouble() + val2)operator fun Long.plus(val2: BigDecimal) = this + val2.toDouble()

val val1 = BigDecimal.TEN
val val2 = 1L
// Now it will work
val val3 = val1 + val2
// This will also work
val val4 = val2 + val1

This is just a simple example. Operator overloading can be applied to the following operators:

+ (plus), - (minus), * (times), / (div), % (mod), .. (range to), ! (not), ++ (increment) and -- (decrement).

Now think about all the possibilities. In instead of creating specific methods and having to call them, you can, for example, modify the way to increment your object and just call ++. Your code will be a lot cleaner.

That was it about Kotlin features related to functions. I hope you enjoyed this basic explanation and that you are now encouraged to study deep this great language. In the next post I'll talk about another feature that will amaze you.

--

--