Description
One comment from my colleagues when trying to persuade them to use Immutables to replace existing code is they don't like the use of Optional for optional fields.
Personally, I'm still undecided about this one, I can see arguments both for and against. If we were starting from a green field then I'd think I'd be happy using Optional.
The one tricky thing was that switching to Optional for legacy beans can cause really subtle bugs if you're not careful. E.g. imagine this legacy code:
class Foo {
private String foo;
String getFoo() {
return foo;
}
}
...
if (value.getFoo() == null) {
... do something
}
Now if I change to an Immutables like this...
abstract class Foo {
abstract Optional<String> getFoo()
}
... I won't receive a compiler error, but my logic in the "if" is broken.
Could we just have, say, an @Value.Optional annotation (Or even use the @javax.annotation.Nullable one), to mark a field optional?
That way, code which uses the old nullable style can work without any changes.
Yes, I understand you're trading off one problem for another (ambiguity about when getFoo() will return null), but this might suit some people better.
Thanks!