Java 7 : The new java.util.Objects class
baptiste-wicht.com
baptiste-wicht.com
-Right now it's possible to use the haXe Enum to implement a form of "Maybe" type: calling some "get" function returns the Maybe, and then you do a switch statement on it. From there the compiler forces you to cover all cases.
Caveat: Enum is quite a slow feature.
-I can force the "get" function to pass in handling code for null and non-null cases as arguments.
Caveat: Requires the handlers to be full functions, not just expressions, which is bulky and inflexible. This should be doable in Java too, but without inference, you might have to fight the type system more to get it right.
-Macros are proposed: Then I can turn my "get" function into a "get" macro that forces me to always provide the null handler.
Caveat: It's a bit more involved than a Maybe type, which would do the compile checks automatically.
Overall, I'm really happy with where haXe is going. It's growing some really useful features while remaining a cross-target language. If macros get in(hotly anticipated, but probably not soon) it'll be a very exciting time for the language.
This seems like recognition that such commonly used library functionality might also be useful to be in the base API.
Though I would think it would make more sense for Objects.nonNull() to throw IllegalArgumentException, not a NPE.
As an aside, I was surprised that the article did not take advantage of static imports to rid the code of the "Objects" references. One could just say:
if (notNull(foo).equals("bar")){...}
I also much prefer the readability of static imports, but it won't work for the "equals," "toString" and "hashCode" methods, obviously.
Oh and while you're about it, please re-parent InterruptedException onto RuntimeException, and make Object's default toString method use reflection to print the contents of all fields in a readable format with recursive toString calls.
Oh no... it's Java.
Yes, I know it throws an exception if it's null... but when your id function has side effects, it's a sign that your language is Just "not that great".
public <T> T id(T t) {
return t;
}
That's an id function. It works fine with nulls.Where did anyone say that java.util.Objects was providing an id function anyway?
Is the inclusion of this monstrosity in a standard library more evidence that null references are a misfeature? A explicit construction like Maybe<T> seems so much of a better idea.
Saying that this is "a monstrosity" because nulls shouldn't exist in the first place is completely unhelpful. Whether we like them or not, we're going to be stuck with them for a long while, and attempts to make them less painful to deal with aren't a bad thing.
Just because Java is popular doesn't mean you have to use it.
In addition, there's no reason we can't write a language without nulls which works on the JVM.
There are languages which work (mostly) without nulls on the JVM though. Scala does something very similar to Haskell's Maybe. (Although for interoperability with Java it can deal with nulls if you insist.)
Just adding a Maybe type to Java wouldn't do anything; each method would still have to handle the Nothing case in some way.
For Maybe to be useful in Java, control flow would have to change from running one line after another to "running" by applying functions, so that whether or not to run each "line of code" would be determined by a combinator that understands the semantics of Maybe. Then, of course, you just have Haskell, which already exists!
A better option for Java would be a type that could not be null. I don't think the language allows the compiler to determine this, though, which means it probably won't exist (or if it does exist, it won't be too useful).
Basically, if you want a good type system, you want to ditch Java.
In Java 8:
java.util.Strings.isBlank(string) -- returns true if the string is null or "", else returns false.
Its wishful thinking hoping for this in Java 7... So I'll just lower some standards.
Can't stand it these days.
I write one small java app every 4 years and that subset of it served me well.