For an example of a working bridge between the two ways of methods and function see PyObjc which allows Python code to call Objective C messaging (e.g in a similar form to your mapping Javascript to Smalltalk - except Javascript is not class based)


https://pythonhosted.org/pyobjc/core/intro.html


Mark

On 16/10/2016 12:59, Tudor Girba wrote:
Hi,


On Oct 16, 2016, at 10:41 AM, CodeDmitry <dimamakh...@gmail.com> wrote:

I was actually curious about this in Ruby as well, since Ruby also doesn't
have the Smalltalk message syntax.

I figure that the magic behind it is that Smalltalk takes strings like "dict
at: 'foo' put: 'bar'" and evaluates them into a JavaScript equivalent of
"dict['at:put:']('foo', 'bar')”.

There is no magic. This is how the syntax is. Instead of parentheses that 
accumulate all parameters in a single place like:
        x.atput(a,b);

we can insert the argument inside the message name, like:
        x at: a put: b.

It’s not magic, is just different. That’s all. Except that one makes it 
possible to write code that resembles natural language.

Doru



Basically, my proof of concept(I cheated with regex to cut time):

'use strict';

var Dictionary = function Dictionary() {
   this.dict = {};
};
Dictionary.prototype['at:put:'] = function(index, value) {
   this.dict[index] = value;
};
Object.defineProperty(Dictionary, 'new', {
   get: function () {
       return new this;
   }
});

function st_eval(str) {
   var x = str.match(/(dict)\s+(at):\s+'(foo)'\s+(put):\s+'(bar)'/);
   var target = x[1];
   var callName = x[2] + ':' + x[4] + ':';
   var argv = [x[3], x[5]];
   var realTarget = eval(target);
   realTarget[callName].apply(realTarget, argv);
}

var dict = Dictionary.new;
console.log(dict);
//dict['at:put:']('foo', 'bar');
console.log(dict);
st_eval("dict at: 'foo' put: 'bar'");
console.log(dict);







--
Mark


Reply via email to