"I was actually curious about this in Ruby as well, since Ruby also doesn't have the Smalltalk message syntax. " Actually, it does when i said in my reply "The only other languages that have such an implementation are ObjC and Ruby which borrow this directly from Smalltalk." What I mean is that both ObjC and Ruby has a message based OOP system , like Smalltalk. http://rubylearning.com/blog/2010/11/03/do-you-understand-rubys-objects-mess... On Sun, Oct 16, 2016 at 11:50 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')".
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.