introduction of statefull functions (inspired by Scala language) - advanced specification of "operator()" for arbitrary objects

Igor Baklan io.baklan at gmail.com
Thu May 12 14:42:35 UTC 2016


It would be nice to "bring sense" to expressions like "obj(a1, ... , aN) =
val".  (like "obj(x)  = y")
In Scala langue it defined in pretty clear and simple way:

   "obj(a1, ... , aN)" <==> "obj.apply(a1, ... , aN)"
   "obj(a1, ... , an) = *val*" <==> "obj.update(a1, ... , aN, *val*)"

Of course this applied only to that cases when obj was not defined like
method (in case of regular methods "mth(args) = val" will cause compile
time error).
So in Scala even arrays and maps are accessed and updated using "()"
operator

   js( arr[index] = val ) <==> scala( arr(index) = val )

(see http://www.scala-lang.org/api/2.10.0/index.html#scala.Array ,
http://docs.scala-lang.org/overviews/collections/maps#operations-in-class-mutablemap
,
etc)

*So the proposals are:*

*(1)* to introduce symbols like *@@apply* and *@@update* with very similar
to Scala meaning (for cases when 'obj.sttFunc' is not a function)

  "obj.sttFunc(a1, ... , aN) = *val*" ==> "obj.sttFunc[Symbol.update]
(obj.sttFunc, obj, [a1, ... , aN], *val*)"
  "obj.sttFunc(a1, ... , aN)" ==> "obj.sttFunc[Symbol.apply] (obj.sttFunc,
obj, [a1, ... , aN])"

*(2)* to extend *Prxoy* object specification to support *update* action
along with *apply* action:

I would like to write something similar to

var target = {
   jsApply: function(key){
      console.log("apply: ", key);
      return key;
   },
   jsUpdate: function(key, val){
      console.log("update: (", key, ") = ", val);
      return val;
   }
};
var sttFunc = new Proxy(target, {
   apply: function(target, thisArg, argumentsList) {
      target.jsApply.apply(thisArg, argumentsList);
   },
   update:  function(target, thisArg, argumentsList, assignValue) {
      target.jsApply.apply(thisArg,
Array.from(argumentsList).concat([assignValue]));
   }
});

And then run it as following

> sttFunc ("key") = "value";

update: ( key ) =  value

> sttFunc ("key");

apply:  key
"key"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160512/fef01dd4/attachment.html>


More information about the es-discuss mailing list