Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Head First Kotlin: Extension Functions vs. Java Utils  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I am totally new to Kotlin and researching the right book to use for teaching myself.
I love other Head First publications on programming topics due to the format.

I was reading online about special features that Kotlin has that Java does not.
I cam across Extension Functions.
The examples given on the https://kotlinlang.org/docs/reference/extensions.html site are functions like sort.
Paraphrasing: The argument is that this extension function allows you to extend the class without having to inherit from the class.
I was wondering if you could elaborate on the benefits of using this new functionality in Kotlin and why this feature is beneficial?
How would you use this functionality in a real world situation?
 
Author
Posts: 105
5
Android Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Extension functions give you the ability to teach existing classes new tricks. :-) When you create an extension function, it feels like you are teaching some existing class a new method. In reality, the compiler will track down any calls to the new extension function and replace it with a call to the extension. This means that although it *looks* like you are adding new methods to existing classes, you not. It's a trick that's played at compile time, and so calls to extensions are always statically resolved (i.e. the code they call is always resolved at compile time, rather than run time).

Some people like to use extension functions because they make your code feel more "object-y". Rather than passing an object to a function, it feels like you are calling a method on the object. It is similar in some ways to add methods to prototypes in JavaScript, except it is a trick played by the Kotlin compiler, rather than a change to the underlying code.

That was a great question. Hope you enjoy the book

D+D
 
V. Keating
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response.
The way you explain that made it much easier to understand than the explanation on the Kotlin site.
I will add that to my tool bag for later and see what other questions may be of use to others reading up on Kotlin.

Thank you again!
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!