I miss C++. I miss STL. I miss RAII.
The .Net framework is severely lacking in collections and list ops. Where’s my set, my multi_map, etc? And worst of all, what little list ops they do provide are pretty bogus:
T List<T>.Find(Predicate<T> pred).
Tell me, looking at that definition, what will Find do if pred matches nothing? There are only really two possibilities: throw something, or return an arbitrary value. Infinite wisdom ensued, and it ended up being the latter.
So that means for code like:
list.Add(new Animal(3));
list.Add(null);
list.Add(new Animal(5));
Animal anml = list.Find(pred);
if (anml == null)
throw new Exception("No such animal found!")
There’s a possibility that if pred were to match on null, the above would say there’s no animal when there really was.
There are some less contrived examples too, which stem from the fact that, funnily enough, default(T) returns a value for value-types.
list.Add(1);
list.Add(5);
list.Add(9);
int i = list.Find(isEven);
if (i == 0)
throw new Exception("No even numbers found!")
Yep. Find returns 0.
I think the ideal solution would be something along the lines of
T? List<T>.Find(Predicate<T> pred)
It would also be nice if reference types weren’t implicitly nullable: if an Animal was guaranteed to have a valid instance, whereas Animal? was nullable. But that’s a touch too big of a hop for the Java programmers, I suppose.
Leave a Comment