Hi,
On Oct 16, 2016, at 10:41 AM, CodeDmitry <dimamakhnin@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);
-- View this message in context: http://forum.world.st/How-do-Smalltalk-disambiguate-messages-tp4918946p49189... Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
-- www.tudorgirba.com www.feenk.com âThe smaller and more pervasive the hardware becomes, the more physical the software gets."