Why does findFirst() throw a NullPointerException if the first element it finds is null?

The reason for this is the use of Optional<T> in the return. Optional is not allowed to contain null. Essentially, it offers no way of distinguishing situations “it’s not there” and “it’s there, but it is set to null“. That’s why the documentation explicitly prohibits the situation when null is selected in findFirst(): Throws: NullPointerException … Read more

std::optional specialization for reference types

When n3406 (revision #2 of the proposal) was discussed, some committee members were uncomfortable with optional references. In n3527 (revision #3), the authors decided to make optional references an auxiliary proposal, to increase the chances of getting optional values approved and put into what became C++14. While optional didn’t quite make it into C++14 for … Read more

How do you unwrap Swift optionals?

There are many similarities and just a handful of differences. (Regular) Optionals Declaration: var opt: Type? Unsafely unwrapping: let x = opt!.property // error if opt is nil Safely testing existence : if opt != nil { … someFunc(opt!) … } // no error Safely unwrapping via binding: if let x = opt { … … Read more