/**/
var samples = [{"tests":"assertEquals(v.color, \"red\");","code":"// this is some sample ECMAScript NG\n// the second line should generate a warning\nvar x : int = 17;\nvar y : int = 18.5;\n\n// example of generics:\nvar d = new Array.<Date>;\nd[1] = new Date();\nd[2] = \"hello\"; // error!\n\n// nullable types\nvar a : ?boolean = null; // OK!\nvar b : !Object = null; // error!\n\n// example of classes and inheritance:\nclass Car {\n   var color : String;\n   function Car(color : String) { \n     this.color = color; \n   }\n}\nclass Volvo extends Car {\n    function Volvo() : super(\"red\") {}\n}\n\nvar v : Car = new Volvo();\nv.color;","title":"Classes and inheritance"},{"tests":"assertEquals(a, 100);","code":"import console;\n/* \nGetter and setter functions allow us \nto hide the implementaion of what looks \nlike an ordinary variable or property.\n\nFor example, to save storage space, we want\nto only save half the value of x:\n*/\nvar halfx : double = 0;\nfunction get x() : double {\n\treturn halfx * 2;\n}\nfunction set x(value : double) {\n\thalfx = value / 2;\n}\nx = 100; // set it as if it was a variable\nvar a = x; // get it back\nprint(\"Should be 100: \" + a);\n\n/* a setter without a getter\nis equivalent to a read-only variable: */\nfunction get message() {\n\treturn \"Hello\";\n}\n//message = \"Goodbye\"; // ERROR - no setter","title":"Getters and setters"},{"tests":"assertEquals(x, 14);\nassertEquals(y, 4);","code":"// named parameters with an optional value\nfunction namedfun(a=1,b=2,c=3) {\n\treturn a + b + c;\n}\nvar x = namedfun(b=10);\n\n// \"...\" prefixed parameter becomes an array\n// containing the rest of the parameters\nfunction restfun(a :String, ...b) {\n\treturn b.length;\n}\nvar y = restfun(\"x\", 1, 2, 3, 4);","title":"Optional and rest parameters"},{"tests":"","code":"// declare structural type A\ntype Point = { x : int, y : int };\n// try to assign \nvar p : Point = {x:10, y:20}; // OK\n\nvar px : int = p.x; // OK\n\nvar p2 : Point = {x:10, y:\"str\"}; // type mismatch\nvar p3 : Point = {x:10}; // type mismatch\n\n// lets try with a nested structure!\ntype A = { a : int, b : String, c : {c1 : Date} };\n\nvar a : A = {a:10, b:\"Hello\", c: {c1 : new Date()}};\nvar b : String = a.b;\nvar c : Date = a.c.c1;\n\nvar a2 : A = {a:10, b:\"Hello\", c: {c1 : \"\"}}; // type mismatch (c1)","title":"Structural types"},{"tests":"","code":"// Parameterized types\n// (Similar to generics in Java)\nclass Point.<T> {\n  var x: T, y: T;\n}\nvar p = new Point.<double>;\np.x = 3.0;\np.y = 5.0;\n\nvar d = new Array.<Date>;\nd[1] = new Date();\nd[2] = \"hello\"; // ERROR!","title":"Parameterized types"},{"tests":"assertEquals(\"2!,4!,6!\", z);","code":"// Fun with parameterized types and higher-order functions\nfunction intToString(x:int):String {\n\treturn x.toString()+\"!\";\n}\nvar lst : Array.<int> = [2,4,6];\nfunction map.<TIn, TOut>(f:(function(TIn):TOut), lst1:Array.<TIn>) : Array.<TOut> {\n\tvar lst2 = new Array.<TOut>();\n\tfor (var ix=0; ix<lst1.length; ix++) {\n\t\tlst2[ix] = f(lst1[ix]);\n\t}\t\n\treturn lst2;\n}\nvar z = map.<int,String>(intToString, lst);\nz; // should be 2!,4!,6!","title":"Typed higher-order functions"},{"tests":"","code":"namespace ns1;\nns1 var q = 10;\nvar h = ns1::q;\n\nnamespace ns2;\nns2 var q = true;\nvar j = ns2::q;\n\nuse namespace ns1;\n\nvar k = q; // gets ns1::q\n\nuse namespace ns2;\n//var g = q; // error: ambiguous","title":"Namespaces"},{"tests":"","code":"// Union type:\n// A set of allowed types\nvar test : (String|int|double) = \"test\";\ntest = 3; // OK\ntest = false; // ERROR\n\n// star means any type\n// including null and undefined\nvar anything : * = 7;","title":"Union and * types"},{"tests":"","code":"/* Null-errors - \"x has no properties\",\n\"x is null or not an object\", etc. - are a pain!\nESX alleviates the problem by allowing us to\nexplicitly contol if an object can be null. */\n\n// \"!\" indicates a non-nullable type\nvar name : !String = \"John\";\nname = \"Ted\";\nname = null; // ERROR\n\n// primitive types\n// are non-nullable by default\nvar b : boolean = true;\nb = null; // ERROR\n\n// ...they can be declared nullable\n// by prefixing with \"?\"\nvar b2 : ?boolean = true;\nb2 = null; // OK!\n\n/*\nA special syntax in constructors\nallows us to initialize non-nullable\ninstance variables.\n*/\nclass User {\n  var name : !String; // Must be initialized\n  var last : !String;\n  function User(n : !String, l : !String) \n\t: name = n, last = l {\n\t  // ...\n  }\n}","title":"Nullability"},{"tests":"","code":"/* \nImport the browser-API (incl. DOM)\nto get type checking on host objects \n*/\nimport browserapi;\nvar x : int = window.location.href; //ERROR\n\nvar e : HTMLElement;\ne = document.createElement(\"div\");\ne.taggName = \"div\"; // ERROR, spelling","title":"Browser API"},{"tests":"","code":"/*\nThis code creates a runtime error\non line 10. The point is to show that the\noffending line in highlighted in both\nthe source and the corresponding generated\ncode. This makes it easier to debug.\nNote: Only works in Firefox\n*/\nvar a : Object = null;\nvar b = a.toString();\nvar c = \"hello\";","title":"Debugging runtime errors"},{"tests":"","code":"/*\nHere is an example of prototype-based inheritance. \nThese patterns still work in ESX.\n*/\nimport console;\nfunction A() { \n\tthis.x = 8; \n\tthis.y=new Date; \n}\nA.prototype = {\n\tx: 19,\n\tgetx: function() { return this.x;},\n};\nfunction B() { \n\tthis.x = 9; \n\tthis.z=\"hello\"; \n}\nB.prototype = new A;\nvar b = new B;\nprint(\"Wonder what this is? \" + b.getx());\nvar a = new A;\nprint(\"And what this is? \" + a.getx());\n\n// verification works correctly:\n//\nvar q = b.x; // OK\nvar r = b.unknown; // ERROR","title":"Prototype-based inheritance"},{"tests":"","code":"// this function declares what\n// the parameter should look like:\nfunction f(p like {x:int,y:int}) {\n\treturn p.x + p.y;\n}\n\n// this works, since the parameter\n// has the expected structure:\nvar pstruct = {x:7,y:99};\nvar sum = f(pstruct); // works!\n\n// now we define a class\nclass Point {\n\tvar x : int;\n\tvar y : int;\n\tfunction Point(_x:int,_y:int)\n\t\t\t: x=_x,y=_y {}\n}\nvar pobj = new Point(6,78);\n\n// this also works, since the P class\n// looks like the expected structure\n// in terms of member names and types\nvar sum2 = f(pobj);\n\n// but this wont work:\n// (since the type of y is wrong)\nvar p2 = {x:17,y:\"hello\"};\nvar sum3 = f(p2); // ERR","title":"Like-constraints"},{"tests":"","code":"/* With triple quotes, a string can now\n   span multiple lines:\n*/\nvar s = \"\"\"\n  This is \n  a \\\"multiline\\\" string!\n\"\"\";\n\n/* With the 'x'-flag, a regular expression\n   may span multiple lines, and \n   may use whitespace and comments\n   to make the expression more readable\n  (comments starts with #)\n*/\nvar ptn = /  # multiline regex\n   [a-zA-Z]  # one char\n   \\d{1,3}   # 1-3 numbers\n/x;\nptn.test(\"a10\");","title":"Multiline strings and regexes"},{"tests":"","code":"/* Array comprehensions are a convenient\n   syntax for transforming arrays: */\n// create a new array with doubles:\nvar a = [x*2 for each (x in [1,2,3])];\nimport console;\nprint(\"a: \"+a);\n\n// a filter can be appended:\nvar b = [x*2 for each (x in [1,2,3]) if (x>1)];\nprint(\"b: \"+b);\n\n// more than one 'for' can be used, in\n// which case we get all combinations:\nvar c = [x+y \n  for each (x in [1,2,3])\n  for each (y in [10,20,30])\n  if (x>1)\n ];\nprint(\"c: \"+c);\n\n/* Note the use of 'for each' above, \n   which means  we iterate through \n   the *values* of the array. \n   Ordinary 'for' can be used to \n   iterate through properties of an object:\n*/\nimport browserapi;\nvar d = [prop for (prop in window.document)];\nprint(\"d: \" + d);","title":"Array comprehensions"}];
