Pablo
05/05/2021, 3:49 PMclass ProductModel {
val productListModel: List<ProductListModel>? = null,
}
And I have the mapper like this
fun map(input : ProductModel) : List<Product>{
return input.productListModel?.map{Product(....)}.orEmpty()
}
Is there another elegant way? If the list is null or empty I should return an empty List of Product and if not, the list mappedtddmonkey
05/05/2021, 3:50 PMemptyList()
and avoid the whole issueRob Elliot
05/05/2021, 3:57 PMproductListModel
has to be nullable for some reason, then you can save yourself one character by doing input.productListModel.orEmpty().map{Product(....)}
Joan Colmenero
05/05/2021, 5:15 PMRob Elliot
05/05/2021, 5:21 PMList<T>.map
would have an if empty return this
or perhaps an if empty return emptyList()
, which always returns the same object
, as an optimisation. But looks like it instantiates a new list instance regardless. Generally I prefer to get away from null as soon as possible, but perhaps in this case that’s bad advice.Pablo
05/05/2021, 5:24 PMmap