Friday 17 April 2020

Kotlin : Infix Functions (Part 10)

Hi ,

Here I am sharing another topic i.e. Infix Function


  • Infix functions can be a Member Function or Extension Function
  • These functions can have only single parameter
  • They have prefix of "infix"
  • All infix functions are extension function But all extension function are not Infix


Take a look :


 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var  i : Int  = 45
        var j : Int = 91
        var k = i findTheGreaterNum j //calling the infix function
        println("In I: $i & in J: $j , greater number is : "+ k)
    }
    
    /**
     * Extension Function
     */
    infix fun Int.findTheGreaterNum(j :Int) : Int {
        if(this > j)
            return this
        else
            return j
    }

So here , we can say that it improves the readability.

I will catch you later , till then Happy Coding :)






No comments:

Post a Comment

Advanced Kotlin Coroutines : Introduction

 Hi,  Today I am unwraping the topic in Kotin world i.e. Coroutine . If you want to get started with Kotlin coroutine and ease your daily de...