Which is more idiomatic: setters taking an optional <T>, or just T?
When used Optional<T>
with a null field, it is more leadiomatic that the receiving setter
- a
Optional<T>
or - just a
T
and then do this:
public class Bar {
private Optional<T> foo;
public void setFoo(T foo) {
this.foo = Optional.<T>fromNullable(foo);
}
public Optional<T> getFoo() {
return foo;
}
}
+3
source to share
3 answers