Delegates in Kotlin

Hi Everyone,

Today we gonna discuss delegates in kotlin.

The Delegation pattern has proven to be a good alternative to implementation inheritance, and kotlin supports it natively requiring zero boilerplate code.

Property delegates don’t have to implement any interface, but they have to provide a getValue() and setValue() function.

let’s see an example, that mention in Kotlin ref docs. going to define Delegate class with getValue and setValue function.

class Delegate {

    operator fun getValue(thisRef : Any?, property : KProperty<*>) : String
    {
        return "${thisRef?.javaClass?.name}, thank you for delegating '${property.name}' to me!"
    }

    operator fun setValue(thisRef : Any?, property : KProperty<*>,value : String)
    {
        println("$value has been assigned to '${property.name}' in ${thisRef?.javaClass?.name}.")
    }
}

now creating another class.

class Example {
     var p : String by Delegate()
}

call Example class

fun main(args : Array)
{
    var example = Example()
    println(example.p)
    example.p = "delegate world"
}
//output:

com.delegated.Example, thank you for delegating 'p' to me!
delegate world has been assigned to 'p' in com.delegated.Example.

kotlin provides few delegated properties build into the language – Lazy properties, Observable properties, and properties storing in the map.

Lazy Properties: lazy is a function that takes a lambda and returns and returns an instance of Lazy which can serve as a delegate.

let’s create an example class.

class Example {
     val lazyValue: String by lazy {
          println("computed!") 
          "Hello"
     }
}
fun main(args : Array)
{
    println(example.lazyValue)
}
//output:
computed!
Hello

Observable properties: Observable takes two arguments: the initial value and a handle for modifications. the handler gets called every time we assign the property. it has three parameters: a property being assigned to, the old value and the new one.

let’s create an example class

class Example {
     
     var name: String by Delegates.observable("") {
          prop, old, new ->
          println("$old -> $new")
     }
}
fun main(args : Array)
{
    var example = Example()

    example.name = "first"
    example.name = "second"
}
//output:
 -> first
first -> second

Properties storing in the map: One common use case is storing the values of properties in a map. This comes up often in applications like parsing JSON or doing other “dynamic” things. In this case, you can use the map instance itself as the delegate for a delegated property.

let’s create an example class.

class User(val map: Map<String, Any?>) {
    val name:String by map
    val age: Int by map
}
fun main(args : Array)
{
    var user = User(mapOf("name" to "Ritesh pathak", "age" to 23))
    println(user.name)
    println(user.age)
}
//output:
Ritesh pathak
27

Ref – Kotlin docs.

Hope you like this. Thanks for reading 🙂

Advertisements

Sealed class in kotlin

Hi Everyone,

Today we gonna discuss Sealed class in kotlin, Sealed is a new class type that introduced by kotlin.

Sealed class –  Sealed classes are used for representing restricted class hierarchies when a value can have one of the types from a limited set.

whereas a subclass of a sealed class can have multiple instances which can contain state.

there are few rules that make a class as a sealed

  1. To declare a sealed class you have to put ‘sealed’ modifier before the name of the class.
  2. A sealed class is abstract by itself, it cannot be instantiated directly and can have abstract members.
  3. Sealed classes are not allowed to have non-private constructors
  4. A sealed class can have subclasses, but all of them must be declared in the same file as the sealed class itself. but that classes which extend subclasses of a sealed class can be placed anywhere, not necessarily in the same file.

Let’s see the example as per kotlin reference doc

sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
class Multi(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()

above we define sealed class Expr and there other classes that extend Expr

The key benefit of using sealed classes comes into play when you use them with WHEN expression. If its possible to verify that the statement covers all cases, you don’t need to add an else clause to the statement.if you remove any of the cases it will throw compile time error.

fun eval(expr: Expr): Double = when(expr) {
    is Const -> expr.number
    is Sum -> eval(expr.e1) + eval(expr.e2)
    is Multi -> eval(expr.e1) * eval(expr.e2)
    NotANumber -> Double.NaN
// the `else` clause is not required because we've covered all the cases
}

 

let’s create other class that extends Multi class, here we can place Multi class child into other files.

class ArrayMulti(val x: Expr, val y: Expr) : Multi(x,y)

Now you can also call ArrayMulti class in WHEN expression, but Expr immediate child(e.g Multi class) must be there.

let’s see an example.

fun eval(expr: Expr): Double = when(expr) {
    is Const -> expr.number
    is Sum -> eval(expr.e1) + eval(expr.e2)
    is ArrayMulti -> eval(expr.e1) * eval(expr.e2)
    is Multi -> eval(expr.e1) * eval(expr.e2)
    NotANumber -> Double.NaN
// the `else` clause is not required because we've covered all the cases
}

Hope you like this. Thanks for reading 🙂

Data class in Kotlin

Hi Everyone,

Hope you like my earlier post on Kotlin (Java to kotlin part-1, java to kotlin part-2 and spring 5 reactive application using kotlin).

Kotlin has introduced many features,  in this post we will focus on data class.

 data –  A type of class that mainly use for hold a data. in such class, some standard functionality and utility functions are often mechanically derivable from the data.  we called data class in kotlin.

In case of data class compiler automatically derives the following members from all properties declared in the primary constructor:

#equals()/hashCode() pair.
# toString()
# componentN() functions corresponding to the properties in their order of declaration.
# copy() function

data classes have to fulfill the requirements:

# The primary constructor needs to have at least one parameters.
# All primary constructor parameters need to be marked as val or var.
# Data classes cannot be abstract, open, sealed or inner.
# (before 1.1) Data classes may only implement interface

let see the example of data class.

Now we are going to create a class with data prefix and define a primary parameterized constructor.

data class Customer(var id: Int,var name: String,var email : String)
//or you can define with default value
data class Customer(var id: Int,var name: String="",var email : String="test@gmail.com")

In the next step, we will be going to instantiate our customer class and print the customer object by using toString method.

var customer = Customer(1,"Nikesh","test@nikeshpathak.com")
println(customer.id)
println(customer.name)
println(customer.email)
println(customer.toString())

output:
1
Nikesh
test@nikeshpathak.com
Customer(id=1, name=Nikesh, email=test@nikeshpathak.com)

Now we will compare customer object using the equals method.

var customer = Customer(1,"Nikesh","test@nikeshpathak.com")
var customer1 = Customer(1,"Nikesh","test@nikeshpathak.com")
println(customer.equals(customer1))
customer1 = Customer(1,"Ritesh","test@nikeshpathak.com")
println(customer.equals(cus=tomer1))

output:
true
false

Note – Structural equality is checked by the equal/==  operation and referential equality is checked by the === operation.

We can Copy one object in another by using copy method.

var customer = Customer(1,"Nikesh","test@nikeshpathak.com")
var customer1 = customer.copy(2,"Ritesh")
println(customer1.id)
println(customer1.name)
println(customer1.email)

output:
2
test@nikeshpathak.com
Ritesh

using componentN (Destructuring declarations)

val (_,name,email) = customer

above syntax is called a destructuring declaration. A destructuring declaration creates multiple variables at once. we have declared two new variables name and email and can use them independently.

println(name)
println(email)

let’s take look example.

var customer = Customer(1,"Nikesh","test@nikeshpathak.com")
val (_,name,email) = customer
// println(id)
println(name)
println(email)

output:
Nikesh
test@nikeshpathak.com

Ref – Kotlin docs

Thanks for reading 🙂

Java to Kotlin part -2

Hello Everyone,

In this post, we will see object-oriented syntex and features in Kotlin. for other basic detail please visit the part-1 tutorial to make yourself comfortable.

let’s jump into code!

#using class

Java

class Customer {
}

Kotlin

class Customer {
}
//or if the class has no body then curley braces can be omitted
class Customer

 

#using constructor including primary and secondary 

Java

class Customer {

    String name;

    Customer() {}

    Customer(String name)
    {
        //TODO
    }
}

Kotlin

class Customer() {

    lateinit var name: String

    constructor(name: String) : this()
    {
        //TODO
    }
}

#using instance of class

Java

Customer customer = new Customer();
//or
Customer customer1 = new Customer("Test");

Kotlin

val customer  = Customer()
//or
val customer1 = Customer("Test")

#using inheritance 

Java

public class Base {

    Base(int p)
    {
        //TODO
    }
}
public class Derived extends Base{

    Derived(int p) {
        super(p);
    }
}

Kotlin

open class Base(p: Int)
class Derived(p: Int) : Base(p)

#using method overriding

Java

public class Base {

   final void v() {}
   void nv() {}
}
public class Derived extends Base{

    @Override
    void nv() {
        super.nv();
    }
}

Kotlin

open class Base {
    open fun v() {}
    fun nv() {}
}
class Derived() : Base() {
    override fun v() {}
}

#using static methods

Java

class Customer {

   static String name = "Test";
}

Kotlin

class Customer {

    companion object {
         var name : String = "Test"
    }
}

 

Thanks for reading 🙂