Re: Let "for" loop treat "null" as empty List ?
Robin Wenger wrote:
Maybe with a workaround like:
for (MyObject mo : mylist; (mylist = NOT(null)))
This is not an improvement over suggestions like Joshua's.
Joshua Cranmer wrote:
If you want to minimize extra lines:
public static <T> List<T> denullify(List<T> list) {
return list == null ? Collections.emptyList<T>() : list;
}
...
for (MyObject mo : denullify(mylist))
Alternatively, you could put that conditional in the for statement
directly, but it might be long for your tabulation requirements.
Instead of relying on the language to enforce invariants for you, you
can use the language to help enforce invariants for you with the
'assert' keyword.
Nothing removes from the programmer the responsibility to enforce
invariants. One way or t'other you have to handle the 'null' case.
Often effective is to prevent the null case. Then you can have code
like:
assert things != null;
for ( Thing thing : things )
{
...
}
This is better on a ton of levels.
Also, what is up with people posting to cljp and cljh, with followups
set to only cljh? It makes it confusing to see no responses...
Yeah, OP and others, don't do that! It's not like people aren't
asking you over and over and over to stop or anything. You're asking
for help but flouting the etiquette - not very smart diplomacy nor
good manners, wouldn't you agree?
Do you wish to antagonize those from whom you request help? Do you
wish to be rude?
This is Usenet - you can be rude directly without playing header
games.
--
Lew