java - Is it useful to provide a type-safe method? -
i'm designing interface follows:
public interface parameters { public <t> t getvalue(parametername pn, class<t> valuetype) throws classcastexception; }
an implementation obligated throw classcastexception
if class
instance of value returned not assignableform of class
passed parameter.
does make sesnse? provides compile-time type-safety, can same explicit cast.
or it's better declare
public object getvalue(parametername pn)
leaving class-cast issues client.
i have used form of api add ability convert type 1 desired. e.g. if it's string need integer attempt parse it.
otherwise, suggest not adding method doesn't provide.
public <t> t getvalue(parametername pn);
this avoid needing explicit cast.
Comments
Post a Comment