kotlin-for-java-developers | Solutions for the Kotlin for Java Developers | Machine Learning library
kandi X-RAY | kotlin-for-java-developers Summary
kandi X-RAY | kotlin-for-java-developers Summary
These are solutions for the popular Kotlin course on Coursera.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of kotlin-for-java-developers
kotlin-for-java-developers Key Features
kotlin-for-java-developers Examples and Code Snippets
Community Discussions
Trending Discussions on kotlin-for-java-developers
QUESTION
While taking the "Kotlin for Java Developers" Coursera course, I came across this construction, where an extension function for a contained instance seems to have access to the containing class' private fields. Note how the Data
class is implemented outside of Container
and does not have access to Container
's fields, but the Data.printMe()
extension function can access Container
's private containerVal
member:
ANSWER
Answered 2020-Apr-13 at 18:21The printMe()
method has two receivers: the dispatch receiver and the extension receiver. The dispatch receiver is the instance of the class in which the extension is declared, and the extension receiver is the instance of the receiver type of the extension method. You are accessing contaiverVal
in the context of a Container
instance (dispatch receiver), not a Data
instance (extension receiver).
If you try to do the opposite and access a private property of the Data
class inside the printMe()
, you will get a compiler error.
when you define an extension function as a member of a class, it is only usable in the context that class. You can access it inside the class or using a scope function where the context object is available as a lambda receiver (this). Those functions are run
, with
and apply
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install kotlin-for-java-developers
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page