Hacker News new | ask | show | jobs
by tonyg 3171 days ago
Some Smalltalks (Squeak and Pharo) have a wonderful tool called a "Method Finder".

The way it works is you write a sequence of arguments and an expected result, and it suggests a method to call.

For example, I just now tried it, entering

  3. 4. 7
in the input. It suggested the following methods:

  3 + 4 --> 7
  3 bitOr: 4 --> 7
  3 bitXor: 4 --> 7
Clicking on any of these opens a browser on the class and method concerned.

Another example: input of

  'hello world'. #('hello' 'world')
yielded the single suggestion

  'hello world' substrings --> #('hello' 'world')
Another:

  '  abc  '. 'abc'
yields

  '  abc  ' asLegalSelector --> 'abc'
  '  abc  ' withBlanksTrimmed --> 'abc'
;;--

It's a total hack, of course, but none the less effective or useful for that.

It has a list of methods marked "safe to experiment with", and simply tries them out.

It gets a big boost from being able to evaluate the receiver (the first in the input list) to a concrete object, and then only consider methods on that object's class.

2 comments

I worked with Visual Smalltalk for years and early on I created my own tool which found all methods whose source-code contained a given string or string with wild-cards. It was surprisingly effective because it easily integrated into the Smalltalk browser-windows in general so rather than printing out the set of methods found it opened a list-browser with all the found methods in it, so I could then easily browse for their senders or methods called in them and so on.

So how did you know what to search for? A simple case was to look up the ready-built application's GUI which usually always contains some strings. So if you wanted to change something in the system which would result in a change to its GUI just search for source-code with some of the words you saw in the GUI.

That's pretty cool. Is the technique for this documented somewhere, or do I have to read the Squeak or Pharo source?
I've looked into it a bit. Visualworks smalltalk documentation gave me an idea[1].

In Smalltalk everything is an object, that is including numbers, characters etc. The standar method-lookup searches from the innermost class methods to the outermost class methods.

The method finder could simply iteratively go through all applicable methods that return the class requested and match results.

[1]: http://esug.org/data/Old/vw-tutorials/vw25/vw25ug.pdf (page 42 method-lookup)

I don't recall reading about it anywhere, but I did once look at the implementation (it's wonderful having a reflective system where a single click gets you source of anything onscreen), and it's very straightforward but also kind of gross.

One neat trick I'd forgotten is that if it doesn't find anything interesting with the inputs in the order written, it permutes them and tries again!