Lynda - 4


*********************
   Operator Overloading
*********************

In Programming, Operator is function which have symbolic name

We can also create own function with symbol, which is called Operator overloading.

Operator consist between 2 operand.

Operator roughly divided in 3 group -
1. Binary Operator - PLUS, MINUS, MULTIPLICATION, DIVISION, MOD, RANGETO
2. Unery Operator - Increase(++A, A++), Decrease(--A, A--), Unery Plus (+A), Unery Minus(-A),
not(!A).
3. Rest are - Assign Operator ( x += 3)
4. Use operator overloading for extendd purpose, if u r using for multiplication purpose, may be confusing.

---------------------------------------------------------
***KotlinMain.kt
---------------------------------------------

fun useOverload(){

    val p1 = Position(10, 20)
    val p2 = Position(2, 1)
    val p3 = p1 + p2 // Here, + is overloaded operator
    println(p1)
    println(p2)
    println(p3)

}

fun main(args: Array<String>) {

    useOverload()

}
---------------------------------------------

***Position.kt
---------------------------------------------

// data before class
data class Position(var x : Int, var y : Int){

//    To overload operator and name which function want to overload like - plus
    operator fun plus(other: Position) : Position {
    return Position(x + other.x, y + other.y)

    }
}
---------------------------------------------
--------------------------------------------------------------

*********************
   Annotation
*********************

In Programming, Operator is function which have symbolic name

We can also create own function with symbol, which is called Operator overloading.

Operator consist between 2 operand.

Operator roughly divided in 3 group -
1. Binary Operator - PLUS, MINUS, MULTIPLICATION, DIVISION, MOD, RANGETO
2. Unery Operator - Increase(++A, A++), Decrease(--A, A--), Unery Plus (+A), Unery Minus(-A),
not(!A).
3. Rest are - Assign Operator ( x += 3)
4. Use operator overloading for extendd purpose, if u r using for multiplication purpose, may be confusing.

---------------------------------------------------------
***KotlinMain.kt
---------------------------------------------

fun useOverload(){

    val p1 = Position(10, 20)
    val p2 = Position(2, 1)
    val p3 = p1 + p2 // Here, + is overloaded operator
    println(p1)
    println(p2)
    println(p3)


}

fun main(args: Array<String>) {

    useOverload()

}
---------------------------------------------

***Position.kt
---------------------------------------------

@Deprecated("Second Class Depreacated", ReplaceWith("Use Class1 instead"), DeprecationLevel.WARNING)
data class Position(var x : Int, var y : Int){

    operator fun plus(other: Position) : Position {

    return Position(x + other.x, y + other.y)

    }
}

---------------------------------------------
--------------------------------------------------------------


***********************
REFLECTION - Reflection allow us to access programmatic information dynamically at run-time.
***********************

let's determine the name of the class at runtime.

MainClass
-----------------------

import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.functions

fun main(args: Array<String>) {

    val data = SecondClass("Bharat", "India")
    val kClass = data.javaClass.kotlin
    println("Simple Name - ${kClass.simpleName}")
    println("Qualified Name - ${kClass.qualifiedName}")
    println("Visibility - ${kClass.visibility}")
    println("Constructor - ${kClass.constructors}")
    println("declaredMemberProperties - ${kClass.declaredMemberProperties}")
    println("functions - ${kClass.functions}")

    println("All member of class - ${kClass.members}")
    println("All member size - ${kClass.members.size}")
    println("First member - ${kClass.members.first()}")
    println("Last member - ${kClass.members.last()}")
    println("isEmpty - ${kClass.members.isEmpty()}")
    println("isNotEmpty - ${kClass.members.isNotEmpty()}")
    println("elementAt - ${kClass.members.elementAt(2)}")
    println("elementAtOrNull - ${kClass.members.elementAtOrNull(15)}")
    println("withIndex - ${kClass.members.withIndex()}")
    println("indices - ${kClass.members.indices}")
    println("toList() - ${kClass.members.toList()}")
}

-----------------------------------------------------------------

Second Class

------------------------------------ ----------------------------
class SecondClass (var name : String, var lastName : String) {

    fun printName(){
        println(name)
    }

    fun printLastName(){
        println(lastName)
    }
}
------------------------------------- ----------------------


*********************** ***************
DSL ( Domain Specific Language ) - Use to solve very common probelem
*********************** ***************

----------------------------------------

import java.lang.RuntimeException

interface Matcher<T>{
    fun test(lhs : T): Unit

    infix fun  or(other: Matcher<T>): Matcher<T> = object : Matcher<T>{
        override fun test(lhs: T) {
            try {
                this@Matcher.test(lhs)
            }catch (e: RuntimeException){
                other.test( lhs)
            }
        }
    }

    infix fun <T>T.should(matcher: Matcher<T>){
        matcher.test(this)
    }

    infix fun <T> Collection<T>.should(fn: CollectionMatchers<T>.() -> Unit){
        val matchers =  CollectionMatchers(this)
        matchers.fn()
    }

    class CollectionMatchers<T>(val collection: Collection<T>){
        fun contains(rhs : T): Unit{
            if(!collection.contains(rhs))
                throw RuntimeException("Collection did not contain $rhs")
        }
        fun notContains(rhs : T): Unit{
            if(collection.contains(rhs))
                throw RuntimeException("Collection should not contain $rhs")
        }

        fun haveSizeLessThan(size : Int): Unit{
            if(collection.size >= size)
                throw RuntimeException("Collection should have size less than $size")
        }
    }

    fun unitTest(){
        val listOfNames = listOf<String>("April", "May", "June  ")
        listOfNames should { notContains("Portia")}
    }
}
---------------------------------------


****************************
DESTRUCTION - It is  a way to pull variable as we want from more complicated object.
****************************
KotlinMain.kt
--------------------------------------------------------------------

fun useOverload(){

    val p1 = Position(10, 20)
    val p2 = Position(2, 1)
    val p3 = p1 + p2 // Here, + is overloaded operator
    println(p1)
    println(p2)
    println(p3)

//kotlin gives the value for each variable based on the order
// that the property. we created in their constructor. so first
// property assign to first and second to socond and so on...

    val (xPosition, yPosition) = p1
    println("xPosition = $xPosition, yPosition = $yPosition")

//    Kotlin create some operator, the name of the first operator is component1 and
// second component2 and so far

    println("c1 = ${p1.component1()}")
}

class Point(val x : Int, val y : Int, val z : Int){
    operator fun component1() : Int = x
    operator fun component2() : Int = y
    operator fun component3() : Int = z
}

fun showComponents(){
    var myPoint = Point(5000, 500, 60000)
    val (myX, myY, myZ) = myPoint
    println("X = $myX, Y = $myY, Z = $myZ ")
}

fun main(args: Array<String>) {

    useOverload()
    showComponents()

}

--------------------------------------------------------------------

Position.kt
--------------------------------------------------------------------

data class Position(var x : Int, var y : Int){

    operator fun plus(other: Position) : Position {

    return Position(x + other.x, y + other.y)

    }
}
--------------------------------------------------------------------




22

Comments

Popular Posts