{"id":591,"date":"2022-08-30T15:03:54","date_gmt":"2022-08-30T15:03:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-to-dynamic-cast-ensure-typesafety-from-collection-with-varying-generics-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:03:54","modified_gmt":"2022-08-30T15:03:54","slug":"how-to-dynamic-cast-ensure-typesafety-from-collection-with-varying-generics-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-dynamic-cast-ensure-typesafety-from-collection-with-varying-generics-collection-of-common-programming-errors\/","title":{"rendered":"How to dynamic cast\/ensure typesafety from collection with varying generics-Collection of common programming errors"},"content":{"rendered":"<p>I don&#8217;t believe you can assign a value to an <code>?<\/code> &#8220;typed&#8221; variable. An unbounded &#8220;?&#8221; means &#8220;the original type of the variable is unknown&#8221;, which means there is no known type it&#8217;s safe to assign to it. It doesn&#8217;t mean that the actual value of the type parameter is retrieved at runtime.<\/p>\n<p>The entire idea of using a generic method to do a cast doesn&#8217;t make sense in Java generics. The only way to do a cast of an unknown type object in a type-safe way is with an actual cast.<\/p>\n<p>The only solution is to get rid of the cast() method, and use raw types:<\/p>\n<pre><code>public void handle()\n{\n    for (IView view : this.views)\n    {\n        view.render(new Object());\n    }\n}\n<\/code><\/pre>\n<p>Assuming the cast() method is only doing casting and not type conversion. If the implementing method that ends up being called is FirstView.render(One object), I believe Java will actually cast the parameter to One. (This can also cause amusing bugs where a ClassCastException is thrown on a line of code with no casts.)<\/p>\n<p>If <code>cast()<\/code> can do a type conversion (in which case, &#8220;cast&#8221; is a bad name for the operation), that means that every IView <em>must<\/em> be able to accept Object, in which case it doesn&#8217;t make much sense have the parameter of <code>render()<\/code> be the generic type to begin with. The design I would use is something like the following:<\/p>\n<pre><code>interface IView {\n    void renderObject(Object o);\n    void render(T);\n    T coerce(Object o);\n}\n\nabstract class ViewBase implements IView {\n    void renderObject(Object o) {\n        render(coerce(o));\n    }\n}\n\nclass FirstView extends ViewBase {\n    \/\/ \u2026\n}\n<\/code><\/pre>\n<p>encapsulating the &#8220;cast&#8221; operation.<\/p>\n<p>There is no statically typesafe way to work with a collection of objects of different types &#8211; in Java, <code>IView<\/code> and <code>IView<\/code> are types as different as <code>String<\/code> and <code>Integer<\/code> and there are no safe conversions between the two.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:06:37. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t believe you can assign a value to an ? &#8220;typed&#8221; variable. An unbounded &#8220;?&#8221; means &#8220;the original type of the variable is unknown&#8221;, which means there is no known type it&#8217;s safe to assign to it. It doesn&#8217;t mean that the actual value of the type parameter is retrieved at runtime. The entire [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-591","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=591"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/591\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}