Mastering Kotlin Conventions: Get | Set | In | RangeTo : Part II
Kotlin is famous for its features like high order functions or the power to use functions as first class citizens, extension functions and its conventions to make code easier to understand.
Along with other general programming language conventions like naming classes, functions, Kotlin has its different flavor called Kotlin Conventions.
If are you want to start from basic conventions, check out the PART: I of Kotlin convention blog.
What is Kotlin Conventions?
Kotlin allows us to provide implementations for a predefined set of operators on our types. These operators have fixed symbolic representation (like
+
or*
) and fixed precedence. To implement an operator, we provide a member function or an extension function with a fixed name, for the corresponding type, i.e. left-hand side type for binary operations and argument type for unary ones. Functions that overload operators need to be marked with theoperator
modifier.
We are going to map all below-mentioned conventions by an example of Date
Let’s start with the conventions:
Access An Element by get
convention
In Kotlin or some other programming languages like Java, C, C++ we can access an element of an array by array[6]
to get the 7th element from an array.
Similarly, to get month
from a date instance we can implement `getMonth()` method in date class and some more methods to get other fields.
Instead, if we can access the first element of date ( day
) by date.get(0)
or by justdate[0].
This is where we can use Kotlin get
convention. To access an element by index, we need to implement operator function get
on the Date
class.
Set An Element by set
convention
Similar to getting an element from an object we can also set an element by convention set.
Here, instead of setting element to some object by using setter method like date.setMonth(6)
we can alternatively use date[1]=6
with Kotlin set convention.
Create Range of Element by rangeTo convention
Lots of programming languages give the ability to create range or sequence of an element by using ..
between two values. Ex. 1..10
meaning the sequence of elements from 1 to 10. Kotlin also has this feature for language declared classed like Int, Double.
Along with this, we can also implement rangeTo
method on the custom class and construct range or sequence just by doing date(1,1,2018)..date(31,12,2018)
(dates within the year 2018).
Note: rangeTo
can be called on any class implementing Comparable
interface. Because Kotlin standard library implements extension function rangeTo
on Comparable
interface.
If you want to add some other behavior to rangeTo
function you can implement your own extensions function.
Check If Element Exists by in convention
Let’s say, we have a month like,
And we want to check if given date
falls in a month or not.
Like, date(13,3,2018) in month(3, 2018)
With kotlin contains
operator method on Month
class, we can make this possible.
Note: All above operator functions can be implemented as a member function of class instead of extensions function.
References:
Interested in joining one of our teams as we grow?
If you’d like to join us on the journey of building the mobile bank the world loves to use, have a look at some of the roles we’re looking for here.