{"id":697,"date":"2018-11-22T04:01:28","date_gmt":"2018-11-22T04:01:28","guid":{"rendered":"http:\/\/www.onux.com\/jspp\/blog\/?p=697"},"modified":"2018-11-22T04:01:28","modified_gmt":"2018-11-22T04:01:28","slug":"tips-tricks-overriding-tostring","status":"publish","type":"post","link":"https:\/\/www.onux.com\/jspp\/blog\/tips-tricks-overriding-tostring\/","title":{"rendered":"Tips &#038; Tricks: Overriding &#8216;toString&#8217;"},"content":{"rendered":"<p>JS++ has a default &#8216;toString&#8217; method implementation but, sometimes, it is necessary to override this implementation. For example, when using <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Console\/log\" rel=\"noopener\" target=\"_blank\"><code>Console.log<\/code><\/a>, it may be desirable to be able to fully log and inspect a complex JS++ object.<\/p>\n<p>In addition to the <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language\/Reference\/Types\/Primitive-Types\/external\" rel=\"noopener\" target=\"_blank\">Unified External Type<\/a>, there is also a &#8220;Unified Internal Type&#8221;: <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Object\" rel=\"noopener\" target=\"_blank\">System.Object<\/a>. All JS++ classes, including user-defined classes, inherit from System.Object. Due to auto-boxing, even primitive types such as <code>int<\/code> (wrapped by <code>System.Integer32<\/code>), inherit from <code>System.Object<\/code>.<\/p>\n<blockquote><p>Aside: Don&#8217;t worry about the performance implications of auto-boxing. JS++ is able to optimize auto-boxing to the point that <code>toString<\/code> is actually 7.2% faster in JS++ than JavaScript in the worst case (assuming the JavaScript variable is monomorphically-typed) and more than 50% faster for polymorphically-typed (and potentially type-unsafe) JavaScript variables as shown in benchmarks <a href=\"https:\/\/www.onux.com\/jspp\/blog\/code-written-jspp-faster-than-same-code-javascript\/\" rel=\"noopener\" target=\"_blank\">here<\/a>.<\/p><\/blockquote>\n<p>System.Object has a <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Object\/toString\" rel=\"noopener\" target=\"_blank\">toString method<\/a> which is marked as <code>virtual<\/code>. In other words, this method can be overridden by derived classes &#8211; which are effectively all classes in JS++. Here&#8217;s an example of how to do it:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\nclass Point\r\n{\r\n    int x;\r\n    int y;\r\n\r\n    Point(int x, int y) {\r\n        this.x = x;\r\n        this.y = y;\r\n    }\r\n\r\n    override string toString() {\r\n        return \"(\" + x.toString() + \", \" + y.toString() + \")\";\r\n    }\r\n}\r\n\r\nPoint p = new Point(1,2);\r\nConsole.log(p); \/\/ \"(1, 2)\"\r\n<\/pre>\n<p>You&#8217;ll notice the <code>Console.log<\/code> statement doesn&#8217;t even make an explicit <code>toString<\/code> call. The reason is because passing any JS++ object to <code>Console.log<\/code> will call the <code>toString<\/code> method on the object for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JS++ has a default &#8216;toString&#8217; method implementation but, sometimes, it is necessary to override this implementation. For example, when using Console.log, it may be desirable to be able to fully log and inspect a complex JS++ object. In addition to the Unified External Type, there is also a &#8220;Unified Internal Type&#8221;: System.Object. All JS++ classes, &hellip; <a href=\"https:\/\/www.onux.com\/jspp\/blog\/tips-tricks-overriding-tostring\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Tips &#038; Tricks: Overriding &#8216;toString&#8217;&#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\/697"}],"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=697"}],"version-history":[{"count":6,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":765,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/697\/revisions\/765"}],"wp:attachment":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/media?parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/categories?post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/tags?post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}