{"id":4320,"date":"2014-03-30T09:51:26","date_gmt":"2014-03-30T09:51:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-c-and-java-duck-typed-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:42:27","modified_gmt":"2022-08-30T15:42:27","slug":"is-c-and-java-duck-typed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-c-and-java-duck-typed-collection-of-common-programming-errors\/","title":{"rendered":"Is C# and Java duck-typed?-Collection of common programming errors"},"content":{"rendered":"<p>Interfaces are not Duck Typing.<\/p>\n<p><em>Nominative<\/em> Typing:<\/p>\n<blockquote>\n<p>.. compatibility and equivalence of data types is <em>determined by explicit declarations and\/or the name of the types<\/em>.<\/p>\n<\/blockquote>\n<p>This includes Java\/C# Classes and Interfaces. All types and relationships between types are defined <em>by name<\/em>.<\/p>\n<p><em>Structural<\/em> Typing:<\/p>\n<blockquote>\n<p>.. compatibility and equivalence are <em>determined by the type&#8217;s actual structure or definition<\/em>, and not by other characteristics such as its name or place of declaration.<\/p>\n<\/blockquote>\n<p>This includes Scala Structural Types and C++ Templates.<\/p>\n<p><em>Duck Typing<\/em>:<\/p>\n<blockquote>\n<p>.. object&#8217;s <em>methods and properties determine the valid semantics<\/em>.<\/p>\n<\/blockquote>\n<p>This includes dynamically typed languages (e.g. Ruby, Python, JavaScript) and C#&#8217;s <code>dynamic<\/code>. I would also tentatively assert that Duck Typing is a subset\/untyped form of Structural Typing; it is orthogonal to Nominative Typing.<\/p>\n<blockquote>\n<p>Why does the compiler accept the Closer.close() method as implementation of the interface method?<\/p>\n<\/blockquote>\n<p>Because the Close method is public and has a conforming signature. <em>Since ConcreteCloser inherits from Closer it also obtains all the base classes methods<\/em> &#8211; per Inheritance Subtyping and Liskov Substitution Principle (not all OOP languages use LSP) &#8211; <em>and thus conforms-to ICloseable<\/em>; it then chooses to implement the ICloseable interface <em>by name<\/em>. I am not sure what warning would be expected here.<\/p>\n<p><em>If C# was Structurally (or Duck) Typed, then Closer could be used in place of ICloseable, but it cannot;<\/em> <code>ICloseable c = new Closer()<\/code> is invalid as Closer is not defined to be <em>nominatively<\/em> related ICloseable.<\/p>\n<blockquote>\n<p>For me, C# and Java, are somewhat ducktyped in this case.<\/p>\n<\/blockquote>\n<p>No; not unless talking about <code>dynamic<\/code> in C#. See above.<\/p>\n<blockquote>\n<p>Can someone explain me the language considerations behind that please? As both C# and Java do it the same way, there seem to be good reasons for that.<\/p>\n<\/blockquote>\n<p><em>This is by language design choice<\/em>; Interfaces are one method to support Nominative Typing in a <em>Single Inheritance<\/em> model. Scala (and Ruby) supports Traits; C++ supports Multiple Inheritance. Eiffel supports MI and also breaking LSP at the type level. Go figure &#8211; there is no &#8220;one right way&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interfaces are not Duck Typing. Nominative Typing: .. compatibility and equivalence of data types is determined by explicit declarations and\/or the name of the types. This includes Java\/C# Classes and Interfaces. All types and relationships between types are defined by name. Structural Typing: .. compatibility and equivalence are determined by the type&#8217;s actual structure or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-4320","post","type-post","status-publish","format-standard","hentry","category-semantic","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4320","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=4320"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4320\/revisions"}],"predecessor-version":[{"id":8790,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4320\/revisions\/8790"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}