Search code examples
language-design

Has anyone invented a programming environment or experimented with one, where spelling of functions etc... can be approximate?


I'm trying to (and failing) find references to languages or environments or maybe editor plugins, where the programmer doesn't have to correctly spell methods/objects/etc....

Instead, the language uses smart heuristics during look up and chooses the most likely interpretation for the call, given context.

This is without changing the source code! Ie. not a spell checker - the source code gets saved as is.

Has anyone built something approximately like this, and where/how might I find it?


Solution

  • Well there is this code I originally saw in this tweet:

    >>> def method_missing(n, *a, &b); send(methods.min_by { |m| levenshtein(n.to_s, m.to_s) }, *a, &b); end
    >> p [1, 2, 3].elngth
    3
    

    What that code is doing is overriding ruby's method_missing method which is called if an object is called with a method that does not exist on the object. Inside the overridden method_missing method it is looking at all the methods on the given object, and finding one whose name has the shortest Levenshtein distance, and then calling it, assuming that it is what the programmer meant!

    The second line then demonstrates that this causes the length method on Array to be called when the non-existent elngth method is called.

    Whether or not the code actually works instead of going into an infinite recursive loop appears to depend heavily on your particular ruby environment, according to this answer.