1

I apologize for the bad title because i have difficult to find the correct way to describe my issue. So i will try to achieve it by a code:

    class CompetitionDetailsViewHolder() : RecyclerViewHolder(binding.root) {

        fun bind(competitionDetailsData: CompetitionDetailsData) {
            binding.apply {
                val topItemSections = sectionsMap?.get(true)?.take(2) ?: emptyList()
                setSectionVisibility(topItemSections) {.....}
                ....
                ....
            }
        }
        
         private fun setSectionVisibility(
            sections: List<GeneralCompetitionDetailsSection>,
            block: (sections: List<GeneralCompetitionDetailsSection>, isVisible: Boolean) -> Unit) 
        {
            block(sections, sections.isNotEmpty())
        }
}

I can't understand how the code: setSectionVisibility(topItemSections) {.....} works? The issue i see here is: binding.apply gets a lambda expression. inside the lambda expression the keyword this refers to binding object, therefore i can't understand how the method setSectionVisibility which belongs to CompetitionDetailsViewHolder class can be called by implicit this (which as i already say refers to binding object)?

1 Answer 1

1

inside the lambda expression the keyword this refers to binding object,

Yes

therefore i can't understand how the method setSectionVisibility which belongs to CompetitionDetailsViewHolder class can be called by implicit this

"Implicit this" is exactly the key here. You are not saying this.setSectionVisibility, just setSectionVisibility without any qualifications. This makes all the difference.

When resolving calls, calls with an explicit receiver and calls with an implicit receiver are handled differently, as specified in the two different sections in the language spec: calls with an explicit receiver, calls with an implicit receiver.

Here is a quote from the latter section, describing how it works:

For an identifier named f the following sets are analyzed (in the given order):

  1. Local non-extension callables named f in the current scope and its upwards-linked scopes, ordered by the size of the scope (smallest first), excluding the package scope;

  2. [...]

When analyzing these sets, the first set which contains any callable with the corresponding name and conforming types is picked for c-level partition, which gives us the resulting overload candidate set.

As you can see, it tries to find methods named setSectionVisibility "in the current scope and its upwards-linked scopes". The scope of the CompetitionDetailsViewHolder class is an upwards-linked scope of the scope of the apply lambda, and that is where it will find setSectionVisibility.

Note that if there is also a method called setSectionVisibility with the same signature on the binding object, then that method will be called instead, not CompetitionDetailsViewHolder.setSectionVisibility.

On the other hand, assuming binding does not have an accessible method called setSectionVisibility, this.setSectionVisibility fails to compile because in the "call with explicit receiver" section it is specified that:

A call of callable f with an explicit receiver e is correct if at least one of the following holds:

  1. f is an accessible member callable of the classifier type type(e) or any of its supertypes;
  2. f is an accessible extension callable of the classifier type type(e) or any of its supertypes, including top-level, local and imported extensions.
  3. f is an accessible static member callable of the classifier type e.
5
  • i must admit that i tried to read from the links you have sent me and i didn't understand nothing. If you can clarify a little bit what is written there i will be glad. But more important, it in the code was written this. setSectionVisibility then code won't work?
    – Eitanos30
    Commented Jun 16, 2022 at 19:41
  • @Eitanos30 That is correct, yes, unless binding also has a method called setSectionVisibility, in which case that will be called instead. I've also tried to make it clearer.
    – Sweeper
    Commented Jun 16, 2022 at 19:44
  • @Eitanos30 I've tried to remove some parts that are not too relevant to the situation in your question, and also added a quote from the explicit receiver section. I agree that language specs are quite challenging to read, if this is your first time reading one. If you just want a dumbed down but a little imprecise explanation - setSectionVisibility is resolved differently than how you'd expected, because the spec says so. The compiler looks at all the scopes and receivers available. You expected the behaviour for this.setSectionVisibility, where only the type of this matters.
    – Sweeper
    Commented Jun 16, 2022 at 20:16
  • I'm just disappointed not to understand the spec :( I'm accepting your answer. Can you please explain the following sentence: "where only the type of this matters"?
    – Eitanos30
    Commented Jun 16, 2022 at 20:33
  • 1
    @Eitanos30 I mean, whether this.setSectionVisibility is valid depends on the type of this only (and in this case is the binding's type) - if the type of this has setSectionVisibility (extension or not) then it is valid, otherwise it is not. On the other hand, in the case of setSectionVisibility, parent scopes are searched to see if they contain anything called setSectionVisibility, as well as (I've omitted this from the quote) the overload resolution sets for each implicit receiver, treated as an explicit receiver.
    – Sweeper
    Commented Jun 16, 2022 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.