Info on Smalltalk DSLs or Metaprogramming...
Ralph Johnson
johnson at cs.uiuc.edu
Fri Aug 18 13:50:43 UTC 2006
On 8/18/06, Rich Warren <rwmlist at gmail.com> wrote:
> In Ruby this could be done as follows (calls the new_run method and
> passes in a hash):
>
> new_run name => 'my name', id => 0, agent_count => 200, target =>
> 30sphere ,error => 1e-10, visualize => true
In fact, this could be Smalltalk, too. new_run, id, agent_count, etc
would be instance variables and => and , would be messages. 30sphere
is not a legal Smalltalk id but you could use 30 sphere or sphere30.
Comma would be a funny message that took a "setter specification" and
returned a setter, and => would be a message to a setter that set the
appropriate variable. It would be a pretty complicated solution,
though.
> The same thing in smalltalk would look like this (note, they can be
> in almost any order--'name:' is a constructor and must come first--
> the others can be in any order and can be dropped):
>
> TrialRun name: 'my name'; id: 0; agentCount: 200; target: 30sphere;
> error: 1e-10; visualize: true
This would work.
> or, for a prettier option (but one that sacrifices flexibility)
>
> TrialRun name: 'my name' id: 0 agentCount: 200 target: 30sphere
> error: 1e-10 visualize: true verbose: false
This would work, too. You could also implement doesNotUnderstand: to
implement optional parameters so you would get back all the
flexibility you want.
> None of them are really as readable as I would like, but any would
> do. The last one is the most readable, but if the number of optional
> flags started to grow--it would quickly become a real pain.
How would you like it? Invent a syntax, and we'll see if we can turn
it into Smalltalk.
More information about the Squeak-dev
mailing list
|