|
|
|
|
|
by ebola1717
3435 days ago
|
|
It gives you a type/interface level guarantee. e.g. you know public Optional<File> loadFile(String filename);
can fail to return a file, while public Int sum(Int a, Int b);
can't fail to return an Int.Obviously, because nulls exist in java, this guarantee isn't provable on compilation the way it is in e.g. Scala, but if your entire team writes code this way, at least you can rely on that for internal libraries. Like generics, this is one of those ideas that's ok in java 1.X, but doesn't shine as much until a backwards incompatible java 2.X |
|
Which is why I like Kotlin's approach. Same guarantees, one letter instead of ten. If you're writing Java, though, I can see why Optional has some use.