the documentation is really cryptic to me
Usually, the cast operator throws an exception if the cast is not possible. >Thus, we call it unsafe. The unsafe cast in Kotlin is done by the infix operator >as (see operator precedence):
val x: String = y as String
because the word "as" is not easy to find in the internet engines, I cannot understand the use of as and in which way is related to the operator is
Given this code:
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, item: ViewType) {
holder as NewsViewHolder//why is not called holder2 considering is adressed to NewsViewHolder?
holder.bind(item as RedditNewsItem)
}
if I get rid of NewsViewHolder, intelliJ points bind in red. The reason is because there is into the same class an inner class with the method bind
inner class NewsViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
parent.inflate(R.layout.news_item)) {
private val imgThumbnail = itemView.img_thumbnail
private val description = itemView.description
private val author = itemView.author
private val comments = itemView.comments
private val time = itemView.time
fun bind(item: RedditNewsItem) {
imgThumbnail.loadImg(item.thumbnail)
description.text = item.title
author.text = item.author
comments.text = "${item.numComments} comments"
time.text = item.created.getFriendlyTime()
super.itemView.setOnClickListener { viewActions.onItemSelected(item.url)}
}
}
but I pointed in the comments, if the method pass holder with the class Recycler.ViewHolder why the variable holder should be pointed to a method of an inner class, this is really confusing to me. I would rather have done val holderOther =NewsViewHolder.bind(item)