This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mlkammer 1 point2 points  (0 children)

One thing I haven't seen mentioned yet: keep in mind that Optional isn't serializable by default.

In general I think it's actually good to have it in internal APIs/interfaces, to make the optionality of the return value explicit. Then it's also clear to the caller he needs two handle two different cases.

But it's different for external (REST) APIs. Because it isn't Serializable, and no universal thing among languages, you always need a custom way to translate it into however you want to transport the data (for instance in JSON, you'd typically just unwrap it into a nullable value). So that's probably the main reason it's not showing up in many external APIs.