java - Dynamic dispatch, overloading and generics -


using class:

public class fallible<t> {      private final exception exception;     private final t value;      public fallible(final t value) {         this.value = value;         this.exception = null;     }      public fallible(final exception exception) {         this.value = null;         this.exception = exception;     }  } 

can safely assume value never contain exception object?

no, can't make such assumption. example:

object obj = new exception(); fallible f = new fallible(obj); 

would invoke generic constructor.

the way check check type of value explicitly using instanceof:

public fallible(final t value) {   if (value instanceof exception) {     this.exception = (exception) value;     this.value = null;   } else {     this.value = value;     this.exception = null;   } } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -