list flatMap { i => Option(i.accept(this, arg).asInstanceOf[T]) }
or, alternatively, if you want (although this will be more or less converted to your original expression)
for { item <- list itemConverted = item.accept(this, arg).asInstanceOf[T] itemNonNull = itemConverted if itemConverted != 0 } yield itemNonNull
Using collect would be possible, but most likely it would call accept twice for most arguments because of the partial function's isDefinedAt tag:
list collect { case i if i.accept(this, arg).asInstanceOf[T] != null => i.accept(this, arg).asInstanceOf[T] }
To avoid this, you need to use some memoising (or smart extractors).
source share