{"id":637,"date":"2018-06-07T05:07:31","date_gmt":"2018-06-07T05:07:31","guid":{"rendered":"http:\/\/www.onux.com\/jspp\/blog\/?p=637"},"modified":"2021-10-12T19:43:52","modified_gmt":"2021-10-12T19:43:52","slug":"structural-typing-auto-type-inference","status":"publish","type":"post","link":"https:\/\/www.onux.com\/jspp\/blog\/structural-typing-auto-type-inference\/","title":{"rendered":"Tips &#038; Tricks: Structural Typing (in a Nominal Type System) with &#8216;auto&#8217; Type Inference"},"content":{"rendered":"<p>By default, JS++ uses <a href=\"https:\/\/en.wikipedia.org\/wiki\/Nominal_type_system\" rel=\"noopener\" target=\"_blank\">nominal typing<\/a> over structural typing. However, structural typing can be achieved using <code>auto<\/code>.<\/p>\n<p>The following is a contrived example but illustrates a consideration in your application when using <code>auto<\/code>:<\/p>\n<pre class=\"brush:jspp\">\r\nclass Foo\r\n{\r\n    bool isError() { return false; }\r\n}\r\n\r\nauto foo = new Foo();\r\nif (foo.isError()) {\r\n    exit(1);\r\n}\r\n<\/pre>\n<p>Now, during refactoring:<\/p>\n<pre class=\"brush:jspp\">\r\nclass Foo\r\n{\r\n    bool isError() { return false; }\r\n}\r\nclass Bar\r\n{\r\n    bool isError() { return true; }\r\n}\r\n\r\nauto foo = new Bar(); \/\/ type changed\r\nif (foo.isError()) { \/\/ this effectively becomes static duck typing\r\n    exit(1);\r\n}\r\n<\/pre>\n<p>Since the variable <code>foo<\/code> above effectively becomes &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Duck_typing\" rel=\"noopener\" target=\"_blank\">duck typed<\/a>&#8221; (or, more precisely, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Structural_type_system\" rel=\"noopener\" target=\"_blank\">structurally typed<\/a>), it becomes harder for the compiler to potentially catch your errors.<\/p>\n<p>While the above example is contrived, it&#8217;s not difficult to see how this can crop up in more complex applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default, JS++ uses nominal typing over structural typing. However, structural typing can be achieved using auto. The following is a contrived example but illustrates a consideration in your application when using auto: class Foo { bool isError() { return false; } } auto foo = new Foo(); if (foo.isError()) { exit(1); } Now, during &hellip; <a href=\"https:\/\/www.onux.com\/jspp\/blog\/structural-typing-auto-type-inference\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Tips &#038; Tricks: Structural Typing (in a Nominal Type System) with &#8216;auto&#8217; Type Inference&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,18],"tags":[],"_links":{"self":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/637"}],"collection":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/comments?post=637"}],"version-history":[{"count":18,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":1259,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/637\/revisions\/1259"}],"wp:attachment":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/media?parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/categories?post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/tags?post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}