How can I merge two `List` and the iterate over th...
# getting-started
r
How can I merge two
List
and the iterate over them? ChatGPT seems to be about as confused as I am, as most functions I'd be looking for don't exist.
j
(list1 + list2).forEach { ... }
What do you mean by "merge"?
r
Yeah, that's where I get the
+
is not defined
j
Did you try Alt+Enter or Ctrl+space on it? It should suggest an import
r
Ah duh, a bit too many nulls flying around
j
Ah, yes, if your lists are nullable that won't work as is :)
e
you could
Copy code
sequenceOfNotNull(list1, list2).filterNotNull().flatten().forEach { ... }
although I find that having nullable lists may be a bad idea to start with
l
Nulls: The cause of, but not solution to, all the world’s problems.
kodee loving 1
s
every time you make a list nullable instead of using the empty list, god kills a kitten
🙀 1
j
A null list is not the same as an empty list, though. There are valid reasons to use nulls to represent the absence of a list, and for needing to differentiate that from the presence of an empty list. The biggest problem is to let nulls permeate through the whole code base. Usually they are useful at the system boundaries, but less so inside of it.
l
I have a case in my current codebase where we have a mutable builder object internally that has a non-nullable empty list, and then in the controller I have an extension function that converts it to an immutable sibling object that has a nullable list property. The former is used for building up the response, the latter is what gets serialized to JSON.