cwal

Tech-note 22d7126c12
Login

Tech-note 22d7126c12

s2's "new" keyword is not official, nor enabled by default, but here's some of the current experimentation:

s2> var o = new {__new:proc(a,b){this[a]=a; this[b]=b},x:0}(1,2)
result: object@0x10fb360[scope=#1@0x7ffe957dc380 ref#=1] ==> {
  "2": 2,
  "1": 1
}
s2> assert 0 === o.x
result: bool@0x69be18[scope=#0@(nil) ref#=0] ==> true

s2> var o = new scope { var a = {}; a.x=-1; a.__new=proc(x,y){var c=[3,2,1];c.prototype=this;c[x]=y;return c}; a; }(4,5)
result: array@0x10faa80[scope=#1@0x7ffe957dc380 ref#=1] ==> [
  3,
  2,
  1,
  null,
  5
]
s2> assert -1 === o.x
result: bool@0x69be18[scope=#0@(nil) ref#=0] ==> true

A more detailed demo can be found in the unit test script.