{"id":264,"date":"2017-07-27T07:17:49","date_gmt":"2017-07-27T07:17:49","guid":{"rendered":"http:\/\/www.onux.com\/jspp\/blog\/?p=264"},"modified":"2017-07-27T07:29:52","modified_gmt":"2017-07-27T07:29:52","slug":"js-0-5-2-bsd-license-interfaces-abstract-classes-virtual-methods-non-generic-standard-library-classes","status":"publish","type":"post","link":"https:\/\/www.onux.com\/jspp\/blog\/js-0-5-2-bsd-license-interfaces-abstract-classes-virtual-methods-non-generic-standard-library-classes\/","title":{"rendered":"JS++ 0.5.2: BSD License, Interfaces, Abstract Classes, Virtual Methods, Non-generic Standard Library Classes"},"content":{"rendered":"<p>You&#8217;ve asked and we&#8217;ve listened. JS++ is now licensed under the 3-clause BSD License.<\/p>\n<p>When we <a href=\"https:\/\/www.onux.com\/jspp\/blog\/js-0-5-0-basic-classes\/\">first announced 0.5.1<\/a> back in March, we introduced bare minimum classes. Specifically, I noted the following class features were unavailable:<\/p>\n<ul>\n<li><del datetime=\"2017-07-19T03:07:46+00:00\">Generic classes<\/del><\/li>\n<li>Abstract classes<\/li>\n<li><del datetime=\"2017-07-19T03:07:46+00:00\">Inner classes<\/del><\/li>\n<li>Interfaces<\/li>\n<li>Virtual methods<\/li>\n<li><em>Custom Conversion Rules as defined in my book, &#8220;The JS++ Type System&#8221;, Chapter 6.2<\/em><\/li>\n<\/ul>\n<p>Every feature in the above list that isn&#8217;t crossed out is now available except the last feature (custom conversion rules) which will be arriving next. In addition, today&#8217;s release marks the introduction of the <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\">Standard Library<\/a>. I&#8217;m going to briefly introduce the new features.<\/p>\n<h3>Update to Hello World<\/h3>\n<p>The JS++ Hello World program is now written as:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\nConsole.log(\"Hello World\");\r\n<\/pre>\n<p>Notice we no longer have to declare <code>console<\/code> as <code>external<\/code>. <code>external<\/code> is used for importing JavaScript libraries, and since we didn&#8217;t have a JS++ console implementation yet, we resorted to using the JavaScript console. However, now that we have a <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Console\">Console class<\/a> in the Standard Library, it&#8217;s no longer a problem.<\/p>\n<p>It is always recommended that you use the Standard Library&#8217;s <code>Console<\/code> class over the external JavaScript <code>console<\/code>. JS++ will detect if a console is available and will not crash if you try to log to an unavailable console. This can be a problem for web browsers like older versions of Internet Explorer, which are still used heavily in enterprise web applications.<\/p>\n<h3>Standard Library<\/h3>\n<p>The following Standard Library classes are now available:<\/p>\n<ul>\n<li>System.Boolean<\/li>\n<li>System.Character<\/li>\n<li>System.Console<\/li>\n<li>System.Date<\/li>\n<li>System.Double<\/li>\n<li>System.Exception<\/li>\n<li>System.Exceptions<\/li>\n<li>System.Integer8<\/li>\n<li>System.UInteger8<\/li>\n<li>System.Integer16<\/li>\n<li>System.UInteger16<\/li>\n<li>System.Integer32<\/li>\n<li>System.UInteger32<\/li>\n<li>System.Math<\/li>\n<li>System.Object<\/li>\n<li>System.RegExp<\/li>\n<li>System.String<\/li>\n<\/ul>\n<p>Many of the above classes, such as <code>System.String<\/code> and <code>System.Integer32<\/code>, are wrapper classes for <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language-Guide\/boxing-unboxing\">auto-boxing<\/a>. Currently, these wrapper classes simply provide a type-safe (and sometimes optimized) version of their JavaScript-equivalent methods. For example:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\nstring s = \"my string\";\r\nConsole.log(s.replace(\/^[a-z]\/, string(string match){ return match.toUpperCase(); })); \/\/ Prints \"My string\"\r\n<\/pre>\n<p>The above example provides the exact same functionality as JavaScript&#8217;s <code>String.prototype.replace<\/code>. However, you get safety guarantees that you wouldn&#8217;t get with JavaScript. For example, if you try to call <code>System.String.replace<\/code> using the wrong arguments:<\/p>\n<pre class=\"brush:jspp\">Console.log(s.replace(1));<\/pre>\n<pre class=\"cli\">[  ERROR  ] JSPPE5023: No overload for `System.String.replace' that takes `1' argument(s) at line 4 char 12 at replace.jspp<\/pre>\n<h3>Optimizations<\/h3>\n<p>Always favor using the JS++ Standard Library over &#8220;rolling your own&#8221; functions. Consider the following code (which you can run yourself with the latest JS++):<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\ndouble t = (new Date).getTime();\r\nstring z;\r\nfor (int i = 0; i < 5000000; ++i) {\r\n\tz += Double.POSITIVE_INFINITY.toString();\r\n}\r\nConsole.log((new Date).getTime() - t);\r\n<\/pre>\n<p>And the nearly equivalent JavaScript code:<\/p>\n<pre class=\"brush:js\">\r\nvar t = (new Date).getTime();\r\nvar z = \"\";\r\nfor (var i = 0; i < 5000000; ++i) {\r\n\tz += Number.POSITIVE_INFINITY.toString();\r\n}\r\nconsole.log((new Date).getTime() - t);\r\n<\/pre>\n<p><b>JS++ average time:<\/b> 124.4ms<br \/>\n<b>JavaScript average time:<\/b> 211ms<\/p>\n<p>In this case, JS++ is roughly 70% faster... for nearly identical code.<\/p>\n<p>You may think JS++ adds overhead (based on perceptions of what fast code may look like), but well-written JS++ will be faster than JavaScript. <a href=\"https:\/\/www.onux.com\/jspp\/blog\/code-written-jspp-faster-than-same-code-javascript\/\">See my other article on optimization<\/a> for more details.<\/p>\n<h3>Typed Exceptions and Multiple Catch Clauses<\/h3>\n<p>JS++ 0.5.2 introduces the <code>System.Exception<\/code> class and enables you to create your own custom exceptions.<\/p>\n<p>Here's an example:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\nclass CustomException : System.Exception\r\n{\r\n    CustomException() {\r\n        super();\r\n    }\r\n    CustomException(string message) {\r\n        super(message);\r\n    }\r\n}\r\n\r\ntry {\r\n    throw new CustomException(\"This is a custom exception object.\");\r\n}\r\ncatch(CustomException e) {\r\n    Console.log(\"Caught CustomException\");\r\n}\r\ncatch(System.Exception e) {\r\n    Console.log(\"Caught System.Exception\");\r\n}\r\n<\/pre>\n<h3>Variadic Parameters<\/h3>\n<p>The latest version of JS++ also introduces variadic parameters, which allow you to supply an arbitrary number of arguments to a function:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\nvoid log(Date date, ...string messages, bool silent) {\r\n    if (silent) return;\r\n\r\n    foreach(string message in messages) {\r\n        Console.log(date.toString() + \": \" + message);\r\n    }\r\n}\r\n\r\nlog(new Date(), \"1\", \"2\", \"3\", false);\r\n<\/pre>\n<h3>Interfaces<\/h3>\n<p>An interface creates a contract. Methods defined in an interface must be implemented by all inheriting classes. Classes can inherit more than one interface.<\/p>\n<p>According to JS++ <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language-Guide\/naming-conventions\">naming conventions<\/a>, interfaces should be prefixed with \"I\" and should be UpperCamelCase.<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\ninterface IWalkable {\r\n\tvoid walk();\r\n}\r\ninterface ITalkable {\r\n\tvoid talk();\r\n}\r\n\r\nclass Person : IWalkable, ITalkable\r\n{\r\n\tvoid talk() {\r\n\t\tConsole.log(\"Talking...\");\r\n\t}\r\n\tvoid walk() {\r\n\t\tConsole.log(\"Walking...\");\r\n\t}\r\n}\r\n\r\nPerson person = new Person();\r\nperson.talk();\r\nperson.walk();\r\n<\/pre>\n<h3>Callback Type Parameter Names<\/h3>\n<p>Callback types can have parameters. Previously, you could only specify the parameter types for a callback\/function type. However, you can now add names for these parameters. While these names cannot be used and have no meaningful effect on the executed code, they improve the readability of the code.<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\nclass Bird\r\n{\r\n\tvoid fly() {\r\n\t\tConsole.log(\"Flying...\");\r\n\t}\r\n}\r\n\r\nvoid(Bird bird) fly = void(Bird bird) {\r\n\tbird.fly();\r\n};\r\nBird bird = new Bird();\r\nfly(bird);\r\n<\/pre>\n<h3>Removal of 'Convert' Module<\/h3>\n<p>We have removed from the <code>Convert<\/code> module from the latest release. It was always used as a stopgap until we implemented the Standard Library wrapper classes, which provide <code>toString()<\/code> and other methods.<\/p>\n<h3>Bug fix: 'typeof' for internal types<\/h3>\n<p>For non-external types, <code>typeof<\/code> will always return the string <code>\"internal\"<\/code>.<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\nint x;\r\nConsole.log(typeof x); \/\/ \"internal\"\r\n<\/pre>\n<h3>Virtual Methods<\/h3>\n<p>JS++ 0.5.2 introduces the <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language\/Reference\/Modifiers\/virtual\">virtual keyword<\/a> and the <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language\/Reference\/Modifiers\/override\">override keyword<\/a> to enable virtual methods on classes.<\/p>\n<p>Virtual methods enable late binding and runtime polymorphism.<\/p>\n<pre class=\"brush:jspp\">\r\nclass Shape\r\n{\r\n    public virtual double area() {\r\n        return 0;\r\n    }\r\n}\r\n \r\nclass Rectangle : Shape\r\n{\r\n    private int length, width;\r\n \r\n    public Rectangle(int length, int width) {\r\n        this.length = length;\r\n        this.width = width;\r\n    }\r\n \r\n    public override double area() {\r\n        return length * width;\r\n    }\r\n}\r\n \r\nclass Triangle : Shape\r\n{\r\n    private int base, height;\r\n \r\n    public Triangle(int base, int height) {\r\n        this.base = base;\r\n        this.height = height;\r\n    }\r\n \r\n    public override double area() {\r\n        return (base * height) \/ 2;\r\n    }\r\n}\r\n<\/pre>\n<h3>Abstract Classes<\/h3>\n<p>Use the <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language\/Reference\/Modifiers\/abstract\">abstract modifier<\/a> to create abstract classes and methods.<\/p>\n<pre class=\"brush:jspp\">\r\nabstract class Shape\r\n{\r\n    public abstract int area();\r\n}\r\nclass Rectangle : Shape\r\n{\r\n    private int length, width;\r\n \r\n    public override int area() {\r\n        return length * width;\r\n    }\r\n}\r\n<\/pre>\n<h3>Enumerations<\/h3>\n<p><a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language\/Reference\/Statements\/enum\">Enumerations (enums)<\/a> can be used to restrict values and write type-safe code:<\/p>\n<pre class=\"brush:jspp\">\r\nenum Importance { None, Regular, Critical }\r\n \r\nImportance errorLevel = Importance.Critical;\r\n<\/pre>\n<h3>The one missing feature...<\/h3>\n<p>Sadly, there is still one major missing feature from JS++. The Standard Library does not support <code>System.Array<\/code> yet because it is a generic class, and generics have not yet been implemented. In the meantime, you can resort to declaring your arrays as <code>var<\/code>:<\/p>\n<pre class=\"brush:jspp\">\r\nvar arr = [ 1, 2, 3 ];\r\n<\/pre>\n<h3>BSD License<\/h3>\n<p>Last, but not least, JS++ 0.5.2 is the first version of JS++ licensed under the 3-clause BSD License.<\/p>\n<p>The download for JS++ 0.5.2 is available from our <a href=\"https:\/\/www.onux.com\/jspp\/\">home page<\/a>.<\/p>\n<p>Enjoy JS++!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ve asked and we&#8217;ve listened. JS++ is now licensed under the 3-clause BSD License. When we first announced 0.5.1 back in March, we introduced bare minimum classes. Specifically, I noted the following class features were unavailable: Generic classes Abstract classes Inner classes Interfaces Virtual methods Custom Conversion Rules as defined in my book, &#8220;The JS++ &hellip; <a href=\"https:\/\/www.onux.com\/jspp\/blog\/js-0-5-2-bsd-license-interfaces-abstract-classes-virtual-methods-non-generic-standard-library-classes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JS++ 0.5.2: BSD License, Interfaces, Abstract Classes, Virtual Methods, Non-generic Standard Library Classes&#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\/264"}],"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=264"}],"version-history":[{"count":19,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/264\/revisions"}],"predecessor-version":[{"id":309,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/264\/revisions\/309"}],"wp:attachment":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/media?parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/categories?post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/tags?post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}