Smart Cast suggestion


#1

I have this code:

return element is XmlTag && (element as XmlTag).getNamespace().startsWith(taglibPrefix) && isDetailsAccepted(element as XmlTag)

Will be fair enough to have this expresion writing as?:

return element is XmlTag && element.getNamespace().startsWith(taglibPrefix) && isDetailsAccepted(element)

The first part of boolean expression already is assuring that element is XmlTag.

I could write this expression as:

return if(element is XmlTag) {   element.getNamespace().startsWith(taglibPrefix) && isDetailsAccepted(element) } else {   false }

but is too verbose


#2

This code works for me:

``

fun foo(element: Any, taglibPrefix: String): Boolean {
  return element is XmlTag && element.getNamespace().startsWith(taglibPrefix) && isDetailsAccepted(element)
}

class XmlTag {
  fun getNamespace(): String = “”
}

fun isDetailsAccepted(t: XmlTag): Boolean = true

Do you mean that it doesn’t work for you?


#3

element is a var and not a val, isn't it?


#4

If it were, the if example at the end of the post would not compile.


#5

Now works!, Surely my bad