The Programmers Guide To Kotlin - Inline Functions
Written by Mike James   
Tuesday, 25 June 2019
Article Index
The Programmers Guide To Kotlin - Inline Functions
Non-local returns
Reified Type

If you have a valid reified type parameter T then you can use:

obj  is  T  
obj !is  T
obj  as  T  
Obj  as? T

and reflection.

The way that it works is that the type parameter is replaced by the actual type when the inline code is generated.

So at the calling site:

IS <Int>(23)

becomes:

obj is Int

which is perfectly valid and works.

Type reification is a small step in the right direction but you have to admit that Kotlin is boxed in by what Java and the JVM allows it to do. However, Java compatibility is well worth the trade off.

kotlinlogo

 

Summary

  • You can store a reference to an anonymous function in a variable or pass the function as an argument to another function.

  • You can store a reference to a named function in a variable or parameter using the reflection reference operator ::

  • A reference to a lambda function can also be stored in a variable or passed as a parameter.

  • A lambda is defined as { parameter list -> code }

  • A lambda, anonymous or named function can be used as extension functions.

  • The shortened forms that you can use to define and call functions make them very suitable to implement DSLs and metaprogramming.

  • You cannot have a return in a lambda but you can use a qualified return.

  • Lambdas can be simplified to the point where they are difficult to understand. You can use it as a default parameter of a single parameter lambda, leave out parameters using underscore, and execute a lambda immediately.

  • All Kotlin functions support closure where variables that were in scope at the time the function was created remain accessible to the function even after they have gone out of scope.

  • Inline functions can be used to improve performance and add features such as non-local returns and reification

 

This article is an extract from: 

Programmer's Guide To Kotlin Third Edition

kotlin3e360

You can buy it from: Amazon

Contents

  1. What makes Kotlin Special
  2. The Basics:Variables,Primitive Types and Functions 
  3. Control
         Extract: If and When 
  4. Strings and Arrays
  5. The Class & The Object ***NEW!
  6. Inheritance
  7. The Type Hierarchy
  8. Generics
  9. Collections, Iterators, Sequences & Ranges
        Extract: Iterators & Sequences 
  10. Advanced functions 
  11. Anonymous, Lamdas & Inline Functions
  12. Data classes, enums and destructuring
        Extract: Destructuring 
  13. Exceptions, Annotations & Reflection
  14. Coroutines
        Extract: Coroutines 
  15. Working with Java
        Extract: Using Swing
  16. Compose Multiplatform
        Extract: Compose Layout 

<ASIN:B0D8H4N8SK>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Save On edX Professional Certificates
16/10/2024

The idea of gaining a Professional Certificate is to demonstrate your possession of skills needed to succeed in today's most in-demand fields. News of 30% off edX Professional Certificates prompted us [ ... ]



Apache Fury Adds Optimized Serializers For Scala
31/10/2024

Apache Fury has been updated to add GraalVM native images and with optimized serializers for Scala collection. The update also reduces Scala collection serialization cost via the use of  encoding [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:1871962536>

<ASIN:1871962544>

Summary

  • You can store a reference to an anonymous function in a variable or pass the function as an argument to another function.

  • You can store a reference to a named function in a variable or parameter using the reflection reference operator ::

  • A reference to a lambda function can also be stored in a variable or passed as a parameter.

  • A lambda is defined as { parameter list -> code }

  • A lambda, anonymous or named function can be used as extension functions.

  • The shortened forms that you can use to define and call functions make them very suitable to implement DSLs and metaprogramming.

  • You cannot have a return in a lambda but you can use a qualified return.

  • Lambdas can be simplified to the point where they are difficult to understand. You can use it as a default parameter of a single parameter lambda, leave out parameters using underscore, and execute a lambda immediately.

  • All Kotlin functions support closure where variables that were in scope at the time the function was created remain accessible to the function even after they have gone out of scope.

  • Inline functions can be used to improve performance and add features such as non-local returns and reification



Last Updated ( Tuesday, 25 June 2019 )