WoogieNoogie
WoogieNoogie

Reputation: 1258

Function declaration error when compiling with Kotlin 12

I've inherited an older Kotlin codebase, and attempting to compile with the newest compiler had many issues. The one that I'm having trouble figuring out are these strange functions that are hanging out in the middle of a class, without any apparent call. I'm wondering if anyone knows what this used to be, and what it was replaced with in newer versions of Kotlin?

public class SomeAdapter(val friends: SomeAdapterProvider, val listener: OnItemClickedListener) : RecyclerView.Adapter<SomeAdapter.ViewHolder>() {

    trait OnItemClickedListener {
        fun onItemClicked(f: Friendship)
    }

    private inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v), View.OnClickListener {
        override fun onClick(v: View) {
            listener.onItemClicked(somethings[getPosition()])
        }

        val text: TextView by inject(android.R.id.text1)
        val image: Picture by inject(R.id.imageview);

        {
            itemView setOnClickListener this
        }
    }

    {
        setHasStableIds(true)
    }
}

Specifically, the lines in question are the itemView setOnClickListener this and setHasStableIds(true) , both in-between braces just hanging out.

Upvotes: 0

Views: 161

Answers (2)

Jayson Minard
Jayson Minard

Reputation: 85936

As noted by @Andrey in his comment to the question, and along with the answer from @D3xter (adding init to the initialization blocks), here is the updated code (for reference):

public class SomeAdapter(val friends: SomeAdapterProvider, val listener: OnItemClickedListener) : RecyclerView.Adapter<SomeAdapter.ViewHolder>() {

    trait OnItemClickedListener {
        fun onItemClicked(f: Friendship)
    }

    private inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v), View.OnClickListener {
        override fun onClick(v: View) {
            listener.onItemClicked(somethings[getPosition()])
        }

        val text: TextView by inject(android.R.id.text1)
        val image: Picture by inject(R.id.imageview);

        init { // FIXED here
            itemView setOnClickListener this
        }
    }

    init { // FIXED here
        setHasStableIds(true)
    }
}

Upvotes: 0

D3xter
D3xter

Reputation: 6435

Prefix those 2 function blocks with "init", see Prefixes For Initializer Blocks

Upvotes: 3

Related Questions