Smalltalk
1972fragletMCP + fragletc
Quoting Wikipedia:
Smalltalk is a purely object oriented programming language (OOP), which was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learning Research Group (LRG) scientists, including Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Diana Merry, and Scott Wallace.
In Smalltalk, executing programs are built of opaque, atomic, so-called objects, which are instances of template code stored in classes. These objects intercommunicate by passing of messages, via an intermediary virtual machine environment (VM). A relatively small number of objects, called primitives, are not amenable to live redefinition, sometimes being defined independently of the Smalltalk programming environment.
Smalltalk is notable for influencing a wide range of languages, including Ruby and Crystal, which both copy a lot of Smalltalk's syntax and idioms.
In this container we make use of the GNU Smalltalk implementation of Smalltalk-80 to demonstrate a simple program which defines a class, instantiates it, and sends it a message.
Hello World
#!/usr/bin/env sh
cd /hello-world
gst hello-world.st -a "$@"Coding Guide
Language Version
GNU Smalltalk 3.2
Execution Model
- Interpreted; the script file is loaded and executed in order
- Top-level expressions run immediately; class definitions are compiled then instantiation and message sends run
- No mandatory "main" — execution is top-to-bottom
Key Characteristics
- Pure object-oriented; everything is an object and a message
- Syntax:
receiver messageorreceiver message: arg. Keywords:Object subclass: Name [ ... ] - Single inheritance; blocks for control flow:
[ ... ] value,condition ifTrue: [ ... ] - Case-sensitive; comments start with
!
Fragment Authoring
Your fragment is the entire script. Write a complete, valid GNU Smalltalk program: class definitions (if needed), then top-level expressions. You can use one-liners or full classes.
Stdin and Arguments
- Stdin: Use
stdin(standard input stream). Read a line withstdin nextLine; loop until empty or usestdin contentsfor full input. - Arguments: Use
Smalltalk arguments(returns a collection of command-line argument strings). Join with spaces:(Smalltalk arguments) joinUsing: ' '.
Common Patterns
- Print (newline):
'Hello' displayNlor'Hello' printNl - Print (no newline):
'Hello' displayor'Hello' print - Class:
Object subclass: Foo [ method [ ... ] ]. - Block:
[ 1 + 2 ] value→ 3 - Condition:
x > 0 ifTrue: [ 'yes' displayNl ] ifFalse: [ 'no' displayNl ] - Loop:
1 to: 5 do: [ :i | i printNl ] - String join:
#('a' 'b') joinUsing: ' '
Examples
'Hello, World!' displayNlObject subclass: Greeter [
greet: name [
('Hello, ', name, '!') displayNl
]
].
(Greeter new) greet: 'Alice'.| numbers squared sum |
numbers := #(1 2 3 4 5).
squared := numbers collect: [ :x | x * x ].
sum := squared inject: 0 into: [ :a :b | a + b ].
('Sum of squares: ', sum printString) displayNl(Smalltalk arguments) joinUsing: ' ' displayNl[ stdin atEnd ]
whileFalse: [ stdin nextLine displayNl ]Caveats
- Class names are global; avoid clashing with existing names in the image
Smalltalk argumentsis the standard way to get command-line args in GNU Smalltalk- Stdin is the default input stream; use it for pipe input
Fraglet Scripts
Echo Args
#!/usr/bin/env -S fragletc --vein=smalltalk
'Args: ' display. ((Smalltalk arguments) inject: '' into: [:acc :arg | (acc = '') ifTrue: [arg] ifFalse: [acc, ' ', arg]]) displayNlSmalltalk Fortune
#!/usr/bin/env -S fragletc --vein=smalltalk
'In Smalltalk, even "Hello World" sends a message.' displayNlStdin Upper
#!/usr/bin/env -S fragletc --vein=smalltalk
[ stdin atEnd ]
whileFalse: [ (stdin nextLine) asUppercase displayNl ]Test
#!/usr/bin/env -S fragletc --vein=smalltalk
Object subclass: Hello [
greet [
'Hello World!' displayNl
]
].
greeting := Hello new.
greeting greet.