Is it really all about message passing in smalltalk

前端 未结 2 1657
清歌不尽
清歌不尽 2021-02-13 04:59

I\'m new to smalltalk and I\'m impressed with the fact that there are only just 6 keywords in the language (self, super, true, false, nil & thisContext), and how pure it is

2条回答
  •  甜味超标
    2021-02-13 05:28

    One thing that might help clarify : self, super, true, false, nil & thisContext are data primitives, rather than keywords.

    They are the only 6 data primitives. These 6 are also known as pseudo-variables. Absolutely every other thing is an instance of Class Object or its subclasses.

    There are very few pre-defined keywords in Smalltalk. They can be written in a very condensed form.

    A famous example is Smalltalk Syntax on a Postcard (link)

     exampleWithNumber: x
    
        | y |
        true & false not & (nil isNil) ifFalse: [self halt].
        y := self size + super size.
        #($a #a "a" 1 1.0)
            do: [ :each |
                Transcript show: (each class name);
                           show: ' '].
        ^x < y
    

    Here's the comment for this method - which is larger than the method itself:

    "A method that illustrates every part of Smalltalk method syntax except primitives. It has unary, binary, and keyboard messages, declares arguments and temporaries, accesses a global variable (but not an instance variable), uses literals (array, character, symbol, string, integer, float), uses the pseudo variables true, false, nil, self, and super, and has sequence, assignment, return and cascade. It has both zero argument and one argument blocks."

提交回复
热议问题