Which do you think reads more clearly/naturally? :...
# codereview
s
Which do you think reads more clearly/naturally? 🅰️
Copy code
val typeParameters: Array<Class<*>> by lazy {
  (fieldType.type as? ParameterizedType)
      ?.actualTypeArguments
      ?.map { it as Class<*> }
      ?.toTypedArray()
    ?: emptyArray()
}
🅱️
Copy code
val typeParameters: Array<Class<*>> by lazy {
  when (val type = fieldType.type) {
    is ParameterizedType -> type.actualTypeArguments
        .map { it as Class<*> }
        .toTypedArray()
    else -> emptyArray()
  }
}
(
fieldType.type
is a property that has open or custom getter, so smart-casting can't be used with it)
🅱️ 9
Bonus:
Copy code
.map { it as Class<*> }
or
Copy code
.map(Class::class::cast)
.filterIsInstance<Class<*>>()
might also work here, but could potentially cause a bug by quietly filtering out elements that aren't
Class
instances
j
Ideally, make this return a list, not an array, and avoid the extra
.toTypedArray()
j
and you can us
.orEmpty()
with that
s
@Joffrey ideally, yes, but nothing about this is "ideal" really. this array eventually gets passed to a method provided by an external library, and that method consumes an array rather than a list. I could keep the result of
map
and then force any user of said method to call
.toTypedArray()
when they use it, or I could call it once here and cache the results.