dimanche 22 février 2015

Would it be a good idea if compiler resolved nulls when Optional



That would be so obviously useful that I am starting to think I am missing a rationale to avoid it, since I am sure Oracle would have made it that way. It would be the most valuable feature on Optional for me.



public class TestOptionals{
public static void main(String[] args) {
test(null);
}

public static void test(Optional<Object> optional){
System.out.println(optional.orElse(new DefaultObject()));
}
}


(This throws a NullPointerException)


Without that feature I see too verbose using Optional for the argument. I prefer a simple Object optional signature and checking it by if (null = optional) that creating the object Optional for comparing later. It is not valuable if that doesn't help you checking the null




Aucun commentaire:

Enregistrer un commentaire