Why Avoid Optional Semantics?
• A type should mean: “what an object can do for me.” • Here’s a different approach: package java.util; public interface Iterator { // Returns true if the iteration has more elements. boolean hasNext(); // Returns the next element of the iteration. Object next(); } package java.util; public interface Removerator extends Iterator { // Removes from the underlying collection the last // element removed by the iterator void remove(); } • Here, Removerator means an Iterator that can remove elements from the underlying collection • Optional code is likely slower than types code, because throwing exceptions is slow • Optional code robs you of strong typing at compile time that can help you catch errors sooner.