[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] [RFC] Smalltalk scripting syntax

From: David Given
Subject: Re: [Help-smalltalk] [RFC] Smalltalk scripting syntax
Date: Wed, 14 Mar 2007 00:44:55 +0000
User-agent: Thunderbird (X11/20070221)

Paolo Bonzini wrote:
> Now, let's assume we can modify the compiler (as in Stephen Compall's
> Presource thing!).  Now, the `(...) delimiters for message patterns
> are close to useless, because the syntax for binary or keyword messages
> is unique and, for unary messages, the compiler could see the special
> #defineAs: method and distinguish variables from message patterns.

This worries me slightly --- as soon as you start adding special cases to your
language to add support for certain features, your language syntax starts
becoming inconsistent, which is a Bad Thing. There ought to be a way to do
this that avoids such nastiness.

Is there anything wrong with:

SomeClass method: 'printOn:' is: [ :aStream |
        ^ foo

...? Parsing the string can be fast, because method:is: can hand it off to the
compiler and do it in a single operation.

This is still a bit wordy. It would be nice to simplify things slightly and
keep the elegant, scope-oriented structure of the original syntax, while still
keeping the incremental approach which works well for scripts (where you
execute each statement as you read it, rather than parsing the entire file in
one go).


How about something along these lines:


'Hello, world!' printNl. "A simple statement."

Class subclass: 'MyClass' is: [ :class
    class classMethod: 'new' is: [
        | i |
        i := super new.
        i message: 'Hello, world!'.

    class method: 'go' is: [
        message printNl

    class variable: 'message'.
    class method: 'message:' is: [ :m |
        message := m

c := MyClass new.
c message: 'Ia Ia Cthulhu Fthagn!'.
c go.


This is all conventional syntax. Class>>subclass:is: takes a Block as its
second parameter, and the new subclass (or a factory object) is passed in.
(Alternatively, subclass:is: could tweak the Block's dictionary to predefine
class, but that's a bit nasty.) It then evaluates the Block, which cause
methods and variables to get programmatically added to the new class.

The script merely consists of a sequence of these statements which are read in
and executed in order. No fuss, no pain. You *might* have a little difficulty
adding instance variables incrementally --- doesn't GST require you to declare
how many you've got at class creation time?

(I'm assuming heavily that it's possible to turn a Block into a method without
any horrible side effects. This approach *does* allow you to do ghastly
scoping monstrosities such as defining a local variable in the class:is:
block, and then referring to it in a method... I'm not sure whether this is an
advantage or a disadvantage. It does give very quick and easy class variables.)

┌── ─── ───────────────────
│ "I have always wished for my computer to be as easy to use as my
│ telephone; my wish has come true because I can no longer figure out how to
│ use my telephone." --- Bjarne Stroustrup

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]