{"id":514,"date":"2018-03-27T18:57:13","date_gmt":"2018-03-27T18:57:13","guid":{"rendered":"http:\/\/www.onux.com\/jspp\/blog\/?p=514"},"modified":"2018-03-27T19:11:19","modified_gmt":"2018-03-27T19:11:19","slug":"js-0-8-1-auto-catch-all-and-time","status":"publish","type":"post","link":"https:\/\/www.onux.com\/jspp\/blog\/js-0-8-1-auto-catch-all-and-time\/","title":{"rendered":"JS++ 0.8.1: auto, catch-all, and Time"},"content":{"rendered":"<p>JS++ 0.8.1 fixes two major bug fixes that appeared when we introduced generics (0.8.0) and introduces some very useful new features. If you come from a C++ background, you might appreciate some of the new syntax :-).<\/p>\n<h3>&#8216;auto&#8217; Keyword<\/h3>\n<p>With the introduction of generic programming in 0.8.0, we need a more concise syntax to instantiate generic classes. The <code>auto<\/code> keyword solves this problem.<\/p>\n<p>Now, instead of the following:<\/p>\n<pre class=\"brush:jspp\">\r\nVeryLongClassName&lt;string&gt; foo = new VeryLongClassName&lt;string&gt;();\r\n<\/pre>\n<p>You can instead instantiate classes using <code>auto<\/code>:<\/p>\n<pre class=\"brush:jspp\">\r\nauto foo = new VeryLongClassName&lt;string&gt;();\r\n<\/pre>\n<p><code>auto<\/code> can be used for more than just generic classes though:<\/p>\n<pre class=\"brush:jspp\">\r\nclass Foo {}\r\n\r\nauto foo = new Foo();\r\n<\/pre>\n<p>The <code>auto<\/code> keyword can only be used for variable declarations, and it reduces the redundancy of having to specify the type on the left-hand side only to repeat yourself on the right-hand side. This is known as <strong>local-variable type inference<\/strong> (to use the Java 10 terminology). For fans of C++, you may recognize this syntax.<\/p>\n<h3>Catch-all Clauses<\/h3>\n<p>This is another syntax that comes from C++.<\/p>\n<p>Sometimes, you&#8217;ll want to catch an exception, but you don&#8217;t care to do anything with the exception object. In other cases, you may want to catch a specific type of exception but ignore all other exceptions. The &#8220;catch-all&#8221; clause can help you:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\ntry {\r\n    throw new System.Exception();\r\n}\r\ncatch(System.Exception e) {\r\n    Console.log(e.getMessage());\r\n}\r\ncatch(...) {\r\n    Console.log(\"The catch-all clause provides no exception object but will catch all exceptions - whether the type of the exception is internal or external.\");\r\n}\r\n<\/pre>\n<pre class=\"brush:jspp\">\r\nimport Externals.JS;\r\n\r\ntry {\r\n    throw new ReferenceError();\r\n}\r\ncatch(...) {\r\n    Console.log(\"Do nothing\");\r\n}\r\n<\/pre>\n<h3>System.Time.TimeUnits<\/h3>\n<p>JS++ has an emphasis on readability. Have you ever seen code that looks like this?<\/p>\n<pre class=\"brush:js\">\r\nsetInterval(function() { \/* ... *\/ }, 3600000);\r\n<\/pre>\n<p>The <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Time\">System.Time<\/a> module has been introduced for dealing with time. Specifically, we&#8217;ve introduced a <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Time\/TimeUnits\">TimeUnits<\/a> module to directly deal with the above case:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System.Time;\r\nexternal setInterval;\r\n\r\nsetInterval(void() { \/* ... *\/ }, TimeUnits.hours(1));\r\n<\/pre>\n<p>Or, more succinctly:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System.Time.TimeUnits;\r\nexternal setInterval;\r\n\r\nsetInterval(void() { \/* ... *\/ }, hours(1));\r\n<\/pre>\n<p>As always, we&#8217;ve had a focus on readable code. Here&#8217;s a preview of how well this composes:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\nimport System.Time.TimeUnits;\r\n\r\nConsole.log(hours(1) + minutes(5));\r\n<\/pre>\n<h3>Externals.Time<\/h3>\n<p>In JavaScript, functions such as <code>setTimeout<\/code> and <code>setInterval<\/code> are not part of the ECMAScript standard; they are provided by the host environment. Web browsers and Node.js ship with these functions, but it would not have been correct to add this to the <code>Externals.JS<\/code> module. Therefore, we&#8217;ve introduced the <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/Externals\/Time\">Externals.Time<\/a> external module so you can more clearly work with timers.<\/p>\n<h3>Bug Fixes<\/h3>\n<ul>\n<li>Fixed methods with variadic parameters passing by reference<\/li>\n<li>Fixed implementing of generic interfaces from generic classes<\/li>\n<li><strong>MAJOR BUG FIX<\/strong>: Fixed double calls being generated for getters<\/li>\n<li><strong>MAJOR BUG FIX<\/strong>: Fix <code>this<\/code> binding in code generation<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>JS++ 0.8.1 fixes two major bug fixes that appeared when we introduced generics (0.8.0) and introduces some very useful new features. If you come from a C++ background, you might appreciate some of the new syntax :-). &#8216;auto&#8217; Keyword With the introduction of generic programming in 0.8.0, we need a more concise syntax to instantiate &hellip; <a href=\"https:\/\/www.onux.com\/jspp\/blog\/js-0-8-1-auto-catch-all-and-time\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JS++ 0.8.1: auto, catch-all, and Time&#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,4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/514"}],"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=514"}],"version-history":[{"count":26,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/514\/revisions"}],"predecessor-version":[{"id":541,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/514\/revisions\/541"}],"wp:attachment":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/media?parent=514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/categories?post=514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/tags?post=514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}