Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Dart's output look like?

Dart, Google's new web language, says it supports outputting to JavaScript.

What does a simple conversion look like?

like image 521
Daniel A. White Avatar asked Oct 10 '11 16:10

Daniel A. White


People also ask

How do you print a Dart output?

A Dart script needs the main() method for execution. print() is a predefined function that prints the specified string or value to the standard output i.e. the terminal.

How do you read data from Dart?

How to take input in Dart. To take input from a user in Dart, you need to import the dart:io library. The input is taken through the console using the . readLineSync() function.

What does ~/ mean in Dart?

~ is currently only used for. ~/ Divide, returning an integer result. and ~/= integer division and assignment.

How do you read a Dart string?

To read a String from console in Dart, read a line from console using stdin. readLineSync() method, and store the returned value in a variable.


1 Answers

main() {       print('Hello, Dart!'); } 

When compiled with dart2js (as of 2013-04-26) (see note at bottom) it is converted into:

// Generated by dart2js, the Dart to JavaScript compiler. // The code supports the following hooks: // dartPrint(message)   - if this function is defined it is called //                        instead of the Dart [print] method. // dartMainRunner(main) - if this function is defined, the Dart [main] //                        method will not be invoked directly. //                        Instead, a closure that will invoke [main] is //                        passed to [dartMainRunner]. function Isolate() {} init();  var $ = Isolate.$isolateProperties; // Bound closures $.Primitives_printString = function(string) {   if (typeof dartPrint == "function") {     dartPrint(string);     return;   }   if (typeof window == "object") {     if (typeof console == "object")       console.log(string);     return;   }   if (typeof print == "function") {     print(string);     return;   }   throw "Unable to print message: " + String(string); };  $.main = function() {   $.Primitives_printString("Hello, Dart!"); };  $.String = {builtin$cls: "String"};  var $ = null; Isolate = Isolate.$finishIsolateConstructor(Isolate); var $ = new Isolate(); // BEGIN invoke [main]. if (typeof document !== "undefined" && document.readyState !== "complete") {   document.addEventListener("readystatechange", function () {     if (document.readyState == "complete") {       if (typeof dartMainRunner === "function") {         dartMainRunner(function() { $.main(); });       } else {         $.main();       }     }   }, false); } else {   if (typeof dartMainRunner === "function") {     dartMainRunner(function() { $.main(); });   } else {     $.main();   } } // END invoke [main]. function init() {   Isolate.$isolateProperties = {};   Isolate.$finishIsolateConstructor = function(oldIsolate) {     var isolateProperties = oldIsolate.$isolateProperties;     isolateProperties.$currentScript = typeof document == "object" ? document.currentScript || document.scripts[document.scripts.length - 1] : null;     var isolatePrototype = oldIsolate.prototype;     var str = "{\n";     str += "var properties = Isolate.$isolateProperties;\n";     var hasOwnProperty = Object.prototype.hasOwnProperty;     for (var staticName in isolateProperties) {       if (hasOwnProperty.call(isolateProperties, staticName)) {         str += "this." + staticName + "= properties." + staticName + ";\n";       }     }     str += "}\n";     var newIsolate = new Function(str);     newIsolate.prototype = isolatePrototype;     isolatePrototype.constructor = newIsolate;     newIsolate.$isolateProperties = isolateProperties;     return newIsolate;   }; } //@ sourceMappingURL=out.js.map 

Note for posterity: The original answer to this question has been modified to reflect the current state of affairs.

On 2012-05-12 the dart output for Hello World was 18,718 characters.

On 2012-08-29 the output was 1531 characters.

On 2013-04-26, the output was 2642 characters.

dart2js can minify code. Here is an example (as of 2013-04-26)

// Generated by dart2js, the Dart to JavaScript compiler. function I(){} init() var $=I.p $.ib=function(a){if(typeof dartPrint=="function"){dartPrint(a) return}if(typeof window=="object"){if(typeof console=="object")console.log(a) return}if(typeof print=="function"){print(a) return}throw "Unable to print message: " + String(a)} $.E2=function(){$.ib("Hello, Dart!")} $.qU={builtin$cls:"qU"}  var $=null I = I.$finishIsolateConstructor(I) var $=new I() if (typeof document !== "undefined" && document.readyState !== "complete") {   document.addEventListener("readystatechange", function () {     if (document.readyState == "complete") {       if (typeof dartMainRunner === "function") {         dartMainRunner(function() { $.E2(); });       } else {         $.E2();       }     }   }, false); } else {   if (typeof dartMainRunner === "function") {     dartMainRunner(function() { $.E2(); });   } else {     $.E2();   } } function init(){I.p={} I.$finishIsolateConstructor=function(a){var z=a.p z.$currentScript=typeof document=="object"?document.currentScript||document.scripts[document.scripts.length-1]:null var y=a.prototype var x="{\n" x+="var properties = I.p;\n" var w=Object.prototype.hasOwnProperty for(var v in z){if(w.call(z,v)){x+="this."+v+"= properties."+v+";\n"}}x+="}\n" var u=new Function(x) u.prototype=y y.constructor=u u.p=z return u}}//@ sourceMappingURL=out.js.map 

On 2013-04-26, the minified code was 1386 characters.

like image 63
Simon Sarris Avatar answered Oct 01 '22 11:10

Simon Sarris