Search this site
Embedded Files
Squirrel of Rama
  • Home
  • Institute Coaching Programs
  • Android curriculum
    • Android Topics
      • Notification
      • Broadcast & Broadcast Receiver
      • Content Providers
      • Activities
        • Activity and Lifecycle
        • Saving state
        • Tasks and the back stack
      • Services
        • Services
        • Foreground Service
        • Bound Service
      • Dependency Injection
      • Workmanager
      • Fragment Findings*
      • Navigation findings*
      • Permissions Findings*
      • Room & Datastore findings*
    • Kotlin Topics
      • Nullables
      • Exceptions
      • Conditions and Loops
      • Arrays
      • Collections
      • Collection Functions
      • Class & Object
      • OOP Concepts
      • Scope Function
      • Visibility Modifiers
      • Generics
      • Kotlin Classes Types
        • Enums
        • Sealed Class
        • Data Classes
        • Nested and inner classes
        • Object expressions and declarations
      • Kotlin Functions Types
        • Functions
        • Extension functions
        • Higher-order functions and lambdas
      • Couroutines
      • Kotlin Flows
    • Interview Questions
      • Android Interview Questions
  • Products
    • Shabri
    • WedExhibit
  • About
Squirrel of Rama
  • Home
  • Institute Coaching Programs
  • Android curriculum
    • Android Topics
      • Notification
      • Broadcast & Broadcast Receiver
      • Content Providers
      • Activities
        • Activity and Lifecycle
        • Saving state
        • Tasks and the back stack
      • Services
        • Services
        • Foreground Service
        • Bound Service
      • Dependency Injection
      • Workmanager
      • Fragment Findings*
      • Navigation findings*
      • Permissions Findings*
      • Room & Datastore findings*
    • Kotlin Topics
      • Nullables
      • Exceptions
      • Conditions and Loops
      • Arrays
      • Collections
      • Collection Functions
      • Class & Object
      • OOP Concepts
      • Scope Function
      • Visibility Modifiers
      • Generics
      • Kotlin Classes Types
        • Enums
        • Sealed Class
        • Data Classes
        • Nested and inner classes
        • Object expressions and declarations
      • Kotlin Functions Types
        • Functions
        • Extension functions
        • Higher-order functions and lambdas
      • Couroutines
      • Kotlin Flows
    • Interview Questions
      • Android Interview Questions
  • Products
    • Shabri
    • WedExhibit
  • About
  • More
    • Home
    • Institute Coaching Programs
    • Android curriculum
      • Android Topics
        • Notification
        • Broadcast & Broadcast Receiver
        • Content Providers
        • Activities
          • Activity and Lifecycle
          • Saving state
          • Tasks and the back stack
        • Services
          • Services
          • Foreground Service
          • Bound Service
        • Dependency Injection
        • Workmanager
        • Fragment Findings*
        • Navigation findings*
        • Permissions Findings*
        • Room & Datastore findings*
      • Kotlin Topics
        • Nullables
        • Exceptions
        • Conditions and Loops
        • Arrays
        • Collections
        • Collection Functions
        • Class & Object
        • OOP Concepts
        • Scope Function
        • Visibility Modifiers
        • Generics
        • Kotlin Classes Types
          • Enums
          • Sealed Class
          • Data Classes
          • Nested and inner classes
          • Object expressions and declarations
        • Kotlin Functions Types
          • Functions
          • Extension functions
          • Higher-order functions and lambdas
        • Couroutines
        • Kotlin Flows
      • Interview Questions
        • Android Interview Questions
    • Products
      • Shabri
      • WedExhibit
    • About

Extension & Inline functions↗

Extensions functions
Ambiguity
Scope of extensions
Inline function
Without Inline
With inline
Video Notes

Extensions functions

Kotlin provides the ability to extend a class or an interface with new functionality without having to inherit from the class or use design patterns such as Decorator. This is done via special declarations called extensions.


For example, you can write new functions for a class or an interface from a third-party library that you can't modify. Such functions can be called in the usual way, as if they were methods of the original class. This mechanism is called an extension function.


To declare an extension function, prefix its name with a receiver type, which refers to the type being extended. The following adds a swap function to MutableList<Int>:

fun MutableList<Int>.swap(index1: Int, index2: Int) {

    val tmp = this[index1] // 'this' corresponds to the list

    this[index1] = this[index2]

    this[index2] = tmp

}

The this keyword inside an extension function corresponds to the receiver object (the one that is passed before the dot). Now, you can call such a function on any MutableList<Int>:

val list = mutableListOf(1, 2, 3)

list.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'list'

Ambiguity

Scope of extensions

Note: You can declare extensions for one class inside another class. Not necessarily at the top level only

Inline function

Using higher-order functions imposes certain runtime penalties


In Kotlin, the higher-order functions or lambda expressions, all stored as an object so memory allocation, for both function objects and classes, and virtual calls might introduce runtime overhead. Sometimes we can eliminate the memory overhead by inlining the lambda expression. In order to reduce the memory overhead of such higher-order functions or lambda expressions, we can use the inline keyword which ultimately requests the compiler to not allocate memory and simply copy the inlined code of that function at the calling place.


Without Inline


fun higherfunc( str : String, mycall :(String)-> Unit) {


   // inovkes the print() by passing the string str

   mycall(str)

}


fun main(args: Array<String>) {

   print("GeeksforGeeks: ")

   higherfunc("A Computer Science portal for Geeks",::print)

}


It create to much overhead if we check the bytecode by Tools -> Kotlin -> Show Kotlin Bytecode. 


With inline

inline fun higherfunc( str : String, mycall :(String)-> Unit) {


   // inovkes the print() by passing the string str

   mycall(str)

}


fun main(args: Array<String>) {

   print("GeeksforGeeks: ")

   higherfunc("A Computer Science portal for Geeks",::print)

}


With the help of the inline keyword, the println lambda expression is copied in the main function in the form of System.out.println and no further calls required.


Video Notes

  • Extension Function is a function added in the class which is not open. 

  • Extension function declared outside of the class so it can not access the class members

  • Inline function reduce the overhead of lamda function calling. when you pass a function as a argument then kotlin indirectly create an instance and then call a function which create an overhead. if we make that function inline then a function code is pasted at a place of call and no instance created. you can check by java byte code tool.

Extension function

Inline function. in main function the code of calfulcatetimeandrun()'s code is pasted.

A brand by Jagrut Soni
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse