@alexsderkach

Kotlin Decorators

November 24, 2017 · 2 mins read · 0 Comments

2 years ago, I was working for a small company as Java dev. My place was situated very closely to a different team, who were doing a Python project. Every week someone from their team was screening a candidate. A question, which I would hear everytime was: “What is a decorator in Python?”

For Python, it is not exactly the same thing as Decorator pattern, but a syntactic sugar, which allows to alter functions and methods.

1
2
3
4
5
6
7
8
9
10
11
12
def p_decorate(func):
   def func_wrapper(text):
       return "A+{0}".format(func(text))
   return func_wrapper

@p_decorate
def get_text(text):
   return "B+{0}".format(text)

print get_text("C")

# Outputs A+B+C

In Java, the same is usually achieved with AOP.

For Kotlin you could expect the same answer, but Kotlin is more expressive than Java.

Lets say, we have the same function which returns text, as in Python example.

1
2
3
4
5
fun getText(name: String) = "B+$name"

fun main(args: Array<String>) = println(getText("C"))

// Outputs B+C

What we need next, is a high-order function. It is a function that takes functions as parameters, or returns a function.

1
inline fun prefix(f: () -> String) = "A+${f()}"

Additionally, in Kotlin, if a function takes another function as the last parameter, the lambda expression argument can be passed outside the parenthesized argument list.

This means, that we could just wrap a body of a function in prefix { … }, in order to add some behavior to it.

1
2
3
4
fun main(args: Array<String>) = println(getText("C"))

inline fun prefix(f: () -> String) = "A+${f()}"
fun getText(name: String) = prefix { "B+$name" }

Additional reading

https://kotlinlang.org/docs/reference/lambdas.html

http://kotlinlang.org/docs/reference/grammar.html#callSuffix