2 ms·
> The real nice thing about Java is everything is statically typed Optional and generics would like a word... Edit: OK, since this got a downvote, let me be c
by jen20 3y ago
> The real nice thing about Java is everything is statically typed
Optional and generics would like a word...
Edit: OK, since this got a downvote, let me be clearer, with some help from the parent:
You just use Optional<T>, and if you're lucky some of your dependencies BUT NOT ALL OF THEIR TRANSITIVE DEPENDENCIES also use Optional<T> correctly and don't return null, and before you know it you've lost the nice safety and it's a crapshoot whether your Optional<T> will be null.
- kaba0 3y agoI have never ever ever seen a null inside an Optional -- the most common constructor actually Optional::ofNullable makes the correct decision, so you can't even fck it up, and if you deliberately try to, I'm sure intellij would scream your head off.
- waynesonfire 3y agoThe reason you've never seen it is because it won't let you, private Optional(T value) { this.value = Objects.requireNonNull(value); }
- jen20 3y agoThe problem is whether the optional itself is null, not whether the contained value is.
- kaba0 3y agoThat’s not a problem either.
- galdosdi 3y agoYep, this is a great point, and while these days people use stuff like @NonNull/@Nullable, it does not help a lot, for the exact same reason. I wish Java had had non nullable types as default from the getgo, and it's hard to add now for the same reason JS/PY have a hard time adding static typing