Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Learning Kotlin: Operators Don't Have to Mean One Thing

DZone 's Guide to

Learning Kotlin: Operators Don't Have to Mean One Thing

In Kotlin, operators don't necessarily have to mean one thing. Check out this tutorial to learn more about using operators in Kotlin with the unary operator.

· Java Zone ·
Free Resource

From the previous post, we looked at operators and being able to use them yourself by implementing the relevant operator methods. The first part I want to cover in this second post is the Unary operators +, -, and !.

When I was learning this, the term unary jumped out as one I did not immediately recognize. After a quick search with Google, it became clear: if you use a negative unary with a positive number, it becomes a negative number. Unary is a fancy way of referencing basic math.

One thing to remember about operators is that it is totally up to you what they mean. For example, let's start with a simple pet class to allow us to define what type of pet we have.

package blogcode

enum class animal {
    dog,
    cat
}

data class pet(val type: animal);

fun main(args: Array<String>) {
    val myPet = pet(animal.dog)
    println(myPet)
}


This produces pet(type=dog). Now, maybe in my domain, the reverse of a dog is a cat. Because of this, I can make this reflect my domain:

package blogcode

enum class animal {
    dog,
    cat
}

data class pet(val type: animal) {
    operator fun not() : pet = when(this.type) {
        animal.cat -> pet(animal.dog)
        animal.dog -> pet(animal.cat)
    }
}

fun main(args: Array<String>) {
    val myPet = pet(animal.dog)
    println(!myPet)
}


This produces pet(type=cat). This is the core principle: while a unary has a specific purpose, normally, you can use it the way that makes sense. But, it doesn't stop there.

Normally, when we think of something like the unary not with a boolean, it goes from true to false (or vice versa), but it remains a boolean. There is nothing stating it has to be that way:

package sadev

enum class animal {
    dog,
    cat
}

data class pet(val type: animal) {
    operator fun not() :String = "BEN"
}

fun main(args: Array<String>) {
    val myPet = pet(animal.dog)
    val notPet = !myPet
    println("myPet is ${myPet::class.simpleName} with a value of ${myPet}")
    println("notPet is ${notPet::class.simpleName} with a value of ${notPet}")
}


In the above, the output is:

myPet is pet with a value of pet(type=dog)
notPet is String with a value of BEN


In short, the not of a dog pet is actually a string with the value of ben. I have no idea how this is useful to real development, but it is amazing that Kotlin is flexible enough to empower it.

Topics:
java ,tutorial ,code ,unary ,operators

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}