mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Fix content
This commit is contained in:
parent
b598df8e13
commit
b99967e809
@ -3,44 +3,94 @@ language: smalltalk
|
||||
filename: smalltalk.st
|
||||
contributors:
|
||||
- ["Jigyasa Grover", "https://github.com/jig08"]
|
||||
- ["tim Rowledge", "tim@rowledge.org"]
|
||||
---
|
||||
|
||||
- Smalltalk is an object-oriented, dynamically typed, reflective programming language.
|
||||
- Smalltalk is a fully object-oriented, dynamically typed, reflective programming language with no 'non-object' types.
|
||||
- Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis."
|
||||
- It was designed and created in part for educational use, more so for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and others during the 1970s.
|
||||
|
||||
Feedback highly appreciated! Reach me at [@jigyasa_grover](https://twitter.com/jigyasa_grover) or send me an e-mail at `grover.jigyasa1@gmail.com`.
|
||||
|
||||
## Allowable characters:
|
||||
## The Basics
|
||||
### Everything is an object
|
||||
Yes, everything. Integers are instances of one of the numeric classes. Classes are instances of the class Metaclass and are just as manipulable as any other object. All classes are part of a single class tree; no disjoint class trees. Stack frames are objects and can be manipulated, which is how the debugger works. There are no pointers into memory locations that you can dereference and mess with.
|
||||
|
||||
### Functions are not called; messages are sent to objects
|
||||
Work is done by sending messages to objects, which decide how to respond to that message and run a method as a result, which eventually returns some object to the original message sending code.
|
||||
The system knows the class of the object receiving a message and looks up the message in that class's list of methods. If it is not found, the lookup continues in the super class until either it is found or the root of the classes is reached and there is still no relevant method.
|
||||
If a suitable method is found the code is run, and the same process keeps on going with all the methods sent by that method and so on forever.
|
||||
If no suitable method is found an exception is raised, which typically results in a user interface notifier to tell the user that the message was not understood. It is entirely possible to catch the exception and do something to fix the problem, which might range from 'ignore it' to 'load some new packages for this class and try again'.
|
||||
A method (more strictly an instance of the class CompiledMethod) is a chunk of Smalltalk code that has been compiled into bytecodes. Executing methods start at the beginning and return to the sender when a return is encountered (we use ^ to signify 'return the follwing object') or the end of the code is reached, in which case the current object running the code is returned.
|
||||
|
||||
### Simple syntax
|
||||
Smalltalk has a simple syntax with very few rules.
|
||||
The most basic operation is to send a message to an object
|
||||
`anObject aMessage`
|
||||
|
||||
There are three sorts of messages
|
||||
- unary - a single string that may be several words conjoined in what we call camelcase form, with no arguments. For example 'size', 'reverseBytes', 'convertToLargerFormatPixels'
|
||||
- binary - a small set of symbols of the sort often used for arithmetic operations in most languages, requiring a single argument. For example '+', '//', '@'. We do not use traditional arithmetic precedence, something to keep an eye on.
|
||||
- keyword - the general form where multiple arguments can be passed. As with the unary form we use camelcase to join words together but arguments are inserted in the midst of the message with colons used to separate them lexically. For example 'setTemperature:', 'at:put:', 'drawFrom:to:lineWidth:fillColor:'
|
||||
|
||||
#### An example
|
||||
`result := myObject doSomethingWith: thatObject`
|
||||
We are sending the message 'doSomethingWith:' to myObject. This happens to be a message that has a single argument but that's not important yet.
|
||||
'myObject' is a 'MyExampleClass' instance so the system looks at the list of messages understood by MyExampleClass
|
||||
- beClever
|
||||
- doWierdThing:
|
||||
- doSomethingWith
|
||||
|
||||
In searching we see what initially looks like a match - but no, it lacks the final colon. So we find the super class of MyExampleClass - BigExampleClass. Which has a list of known messages of its own
|
||||
- beClever
|
||||
- doSomethingWith:
|
||||
- buildCastleInAir
|
||||
- annoyUserByDoing:
|
||||
|
||||
We find a proper exact match and start to execute the code
|
||||
```
|
||||
doSomethingWith: argumentObject
|
||||
"A comment about what this code is meant to do and any known limitations, problems, where it might be further documented etc"
|
||||
self size > 4 ifTrue: [^argumentObject sizeRelatingTo: self].
|
||||
```
|
||||
|
||||
Everything here except the `^` involves sending more messages. Event the `ifTrue:` that you might think is a language control structure is just Smalltalk code.
|
||||
We start by sending `size` to `self`. `self` is the object currently running the code - so in this case it is the myObject we started with. `size` is a very common message that we might anticipate tells us something about how big an object is; you could look it up with the Smalltalk tools very simply. The result we get is then sent the message `>` with the plain old integer 4 (which is an object too; no strange primitive types to pollute the system here) and nobody should be surprised the `>` is a comparison that answers true or false. That boolean (which is actually a Boolean object in Smalltalk) is sent the message `ifTrue:` with the block of code between the `[]` as its argument; obvioulsy a true boolean might be expected to run that block of code and a false to ignore it.
|
||||
If the block is run then we do some more message sending to the argument object and noting the `^` we return the answer back to our starting point and it gets assigned to `result`. If the block is ignored we seem to run out of code and so `self` is returned and assigned to `result`.
|
||||
|
||||
## Smalltalk quick reference cheat-sheet
|
||||
Taken from [Smalltalk Cheatsheet](http://www.angelfire.com/tx4/cus/notes/smalltalk.html)
|
||||
|
||||
#### Allowable characters:
|
||||
- a-z
|
||||
- A-Z
|
||||
- 0-9
|
||||
- .+/\*~<>@%|&?
|
||||
- blank, tab, cr, ff, lf
|
||||
|
||||
## Variables:
|
||||
- variables must be declared before use
|
||||
- shared vars must begin with uppercase
|
||||
- local vars must begin with lowercase
|
||||
- reserved names: `nil`, `true`, `false`, `self`, `super`, and `Smalltalk`
|
||||
#### Variables:
|
||||
- variable names must be declared before use but are untyped
|
||||
- shared vars (globals, class vars) conventionally begin with uppercase (except the reserved names shown below)
|
||||
- local vars (instance vars, temporaries, method & block arguments) conventionally begin with lowercase
|
||||
- reserved names: `nil`, `true`, `false`, `self`, `super`, and `thisContext`
|
||||
|
||||
## Variable scope:
|
||||
- Global: defined in Dictionary Smalltalk and accessible by all objects in system
|
||||
#### Variable scope:
|
||||
- Global: defined in a Dictionary named 'Smalltalk' and accessible by all objects in system
|
||||
- Special: (reserved) `Smalltalk`, `super`, `self`, `true`, `false`, & `nil`
|
||||
- Method Temporary: local to a method
|
||||
- Block Temporary: local to a block
|
||||
- Pool: variables in a Dictionary object
|
||||
- Method Parameters: automatic local vars created as a result of message call with params
|
||||
- Block Parameters: automatic local vars created as a result of value: message call
|
||||
- Class: shared with all instances of one class & its subclasses
|
||||
- Class Instance: unique to each instance of a class
|
||||
- Instance Variables: unique to each instance
|
||||
- Pool: variables in a Dictionary object, possibly shared with classes not directly related by inheritance
|
||||
- Method Parameters: automatic method temp vars that name the incoming parameters. Cannot be assigned to
|
||||
- Block Parameters: automatic block temp vars that name the incoming parameters. Cannot be assigned to
|
||||
- Class: shared with all instances of a class & its subclasses
|
||||
- Class Instance: unique to each instance of a class. Too commonly confused with class variables
|
||||
- Instance Variables: unique to each instance of a class
|
||||
|
||||
`"Comments are enclosed in quotes"`
|
||||
`"Comments are enclosed in quotes and may be arbitrary length"`
|
||||
|
||||
`"Period (.) is the statement separator"`
|
||||
`"Period (.) is the statement separator. Not required on last line of a method"`
|
||||
|
||||
## Transcript:
|
||||
#### Transcript:
|
||||
```
|
||||
Transcript clear. "clear to transcript window"
|
||||
Transcript show: 'Hello World'. "output string in transcript window"
|
||||
@ -54,7 +104,7 @@ Transcript cr. "carriage return / l
|
||||
Transcript endEntry. "flush the output buffer"
|
||||
```
|
||||
|
||||
## Assignment:
|
||||
#### Assignment:
|
||||
```
|
||||
| x y |
|
||||
x _ 4. "assignment (Squeak) <-"
|
||||
@ -73,7 +123,7 @@ y := x deepCopy. "copy object and ins
|
||||
y := x veryDeepCopy. "complete tree copy using a dictionary"
|
||||
```
|
||||
|
||||
## Constants:
|
||||
#### Constants:
|
||||
```
|
||||
| b |
|
||||
b := true. "true constant"
|
||||
@ -93,7 +143,7 @@ x := #(3 2 1). "array constants"
|
||||
x := #('abc' 2 $a). "mixing of types allowed"
|
||||
```
|
||||
|
||||
## Booleans:
|
||||
#### Booleans:
|
||||
```
|
||||
| b x y |
|
||||
x := 1. y := 2.
|
||||
@ -131,7 +181,7 @@ b := $A isUppercase. "test if upper case
|
||||
b := $A isLowercase. "test if lower case character"
|
||||
```
|
||||
|
||||
## Arithmetic expressions:
|
||||
#### Arithmetic expressions:
|
||||
```
|
||||
| x |
|
||||
x := 6 + 3. "addition"
|
||||
@ -188,7 +238,7 @@ x := Random new next; yourself. x next. "random number strea
|
||||
x := 100 atRandom. "quick random number"
|
||||
```
|
||||
|
||||
## Bitwise Manipulation:
|
||||
#### Bitwise Manipulation:
|
||||
```
|
||||
| b x |
|
||||
x := 16rFF bitAnd: 16r0F. "and bits"
|
||||
@ -204,7 +254,7 @@ b := 16rFF anyMask: 16r0F. "test if any bits se
|
||||
b := 16rFF noMask: 16r0F. "test if all bits set in mask clear in receiver"
|
||||
```
|
||||
|
||||
## Conversion:
|
||||
#### Conversion:
|
||||
```
|
||||
| x |
|
||||
x := 3.99 asInteger. "convert number to integer (truncates in Squeak)"
|
||||
@ -219,7 +269,7 @@ x := 15 printStringBase: 16.
|
||||
x := 15 storeStringBase: 16.
|
||||
```
|
||||
|
||||
## Blocks:
|
||||
#### Blocks:
|
||||
- blocks are objects and may be assigned to a variable
|
||||
- value is last expression evaluated unless explicit return
|
||||
- blocks may be nested
|
||||
@ -237,7 +287,7 @@ Transcript show: (x value: 'First' value: 'Second'); cr. "use block with argu
|
||||
"x := [ | z | z := 1.]. *** localvars not available in squeak blocks"
|
||||
```
|
||||
|
||||
## Method calls:
|
||||
#### Method calls:
|
||||
- unary methods are messages with no arguments
|
||||
- binary methods
|
||||
- keyword methods are messages with selectors including colons standard categories/protocols:
|
||||
@ -265,7 +315,7 @@ Transcript "Cascading - send mu
|
||||
x := 3 + 2; * 100. "result=300. Sends message to same receiver (3)"
|
||||
```
|
||||
|
||||
## Conditional Statements:
|
||||
#### Conditional Statements:
|
||||
```
|
||||
| x |
|
||||
x > 10 ifTrue: [Transcript show: 'ifTrue'; cr]. "if then"
|
||||
@ -297,7 +347,7 @@ switch at: $C put: [Transcript show: 'Case C'; cr].
|
||||
result := (switch at: $B) value.
|
||||
```
|
||||
|
||||
## Iteration statements:
|
||||
#### Iteration statements:
|
||||
```
|
||||
| x y |
|
||||
x := 4. y := 1.
|
||||
@ -309,7 +359,7 @@ x timesRepeat: [y := y * 2]. "times repeat loop (
|
||||
#(5 4 3) do: [:a | x := x + a]. "iterate over array elements"
|
||||
```
|
||||
|
||||
## Character:
|
||||
#### Character:
|
||||
```
|
||||
| x y |
|
||||
x := $A. "character assignment"
|
||||
@ -329,7 +379,7 @@ b := $A <= $B. "comparison"
|
||||
y := $A max: $B.
|
||||
```
|
||||
|
||||
## Symbol:
|
||||
#### Symbol:
|
||||
```
|
||||
| b x y |
|
||||
x := #Hello. "symbol assignment"
|
||||
@ -351,7 +401,7 @@ y := x asBag. "convert symbol to b
|
||||
y := x asSet. "convert symbol to set collection"
|
||||
```
|
||||
|
||||
## String:
|
||||
#### String:
|
||||
```
|
||||
| b x y |
|
||||
x := 'This is a string'. "string assignment"
|
||||
@ -381,7 +431,7 @@ y := x asSet. "convert string to s
|
||||
y := x shuffled. "randomly shuffle string"
|
||||
```
|
||||
|
||||
## Array:
|
||||
#### Array:
|
||||
Fixed length collection
|
||||
|
||||
- ByteArray: Array limited to byte elements (0-255)
|
||||
@ -426,7 +476,7 @@ y := x asBag. "convert to bag coll
|
||||
y := x asSet. "convert to set collection"
|
||||
```
|
||||
|
||||
## OrderedCollection:
|
||||
#### OrderedCollection:
|
||||
acts like an expandable array
|
||||
|
||||
```
|
||||
@ -471,7 +521,7 @@ y := x asBag. "convert to bag coll
|
||||
y := x asSet. "convert to set collection"
|
||||
```
|
||||
|
||||
## SortedCollection:
|
||||
#### SortedCollection:
|
||||
like OrderedCollection except order of elements determined by sorting criteria
|
||||
|
||||
```
|
||||
@ -515,7 +565,7 @@ y := x asBag. "convert to bag coll
|
||||
y := x asSet. "convert to set collection"
|
||||
```
|
||||
|
||||
## Bag:
|
||||
#### Bag:
|
||||
like OrderedCollection except elements are in no particular order
|
||||
|
||||
```
|
||||
@ -548,10 +598,10 @@ y := x asBag. "convert to bag coll
|
||||
y := x asSet. "convert to set collection"
|
||||
```
|
||||
|
||||
## Set:
|
||||
#### Set:
|
||||
like Bag except duplicates not allowed
|
||||
|
||||
## IdentitySet:
|
||||
#### IdentitySet:
|
||||
uses identity test (== rather than =)
|
||||
|
||||
```
|
||||
@ -583,7 +633,7 @@ y := x asBag. "convert to bag coll
|
||||
y := x asSet. "convert to set collection"
|
||||
```
|
||||
|
||||
## Interval:
|
||||
#### Interval:
|
||||
```
|
||||
| b x y sum max |
|
||||
x := Interval from: 5 to: 10. "create interval object"
|
||||
@ -612,7 +662,7 @@ y := x asBag. "convert to bag coll
|
||||
y := x asSet. "convert to set collection"
|
||||
```
|
||||
|
||||
## Associations:
|
||||
#### Associations:
|
||||
```
|
||||
| x y |
|
||||
x := #myVar->'hello'.
|
||||
@ -620,7 +670,8 @@ y := x key.
|
||||
y := x value.
|
||||
```
|
||||
|
||||
## IdentityDictionary:
|
||||
#### Dictionary:
|
||||
#### IdentityDictionary:
|
||||
uses identity test (== rather than =)
|
||||
|
||||
```
|
||||
@ -686,7 +737,7 @@ Smalltalk removeKey: #CMRGlobal ifAbsent: []. "remove entry from S
|
||||
Smalltalk removeKey: #CMRDictionary ifAbsent: []. "remove user dictionary from Smalltalk dictionary"
|
||||
```
|
||||
|
||||
## Internal Stream:
|
||||
#### Internal Stream:
|
||||
```
|
||||
| b x ios |
|
||||
ios := ReadStream on: 'Hello read stream'.
|
||||
@ -716,7 +767,7 @@ x := ios contents.
|
||||
b := ios atEnd.
|
||||
```
|
||||
|
||||
## FileStream:
|
||||
#### FileStream:
|
||||
```
|
||||
| b x ios |
|
||||
ios := FileStream newFileNamed: 'ios.txt'.
|
||||
@ -737,7 +788,7 @@ b := ios atEnd.
|
||||
ios close.
|
||||
```
|
||||
|
||||
## Date:
|
||||
#### Date:
|
||||
```
|
||||
| x y |
|
||||
x := Date today. "create date for today"
|
||||
@ -771,7 +822,7 @@ y := x printFormat: #(2 1 3 $/ 1 1). "print formatted dat
|
||||
b := (x <= Date today). "comparison"
|
||||
```
|
||||
|
||||
## Time:
|
||||
#### Time:
|
||||
```
|
||||
| x y |
|
||||
x := Time now. "create time from current time"
|
||||
@ -791,7 +842,7 @@ x := Time millisecondsToRun: [ "timing facility"
|
||||
b := (x <= Time now). "comparison"
|
||||
```
|
||||
|
||||
## Point:
|
||||
#### Point:
|
||||
```
|
||||
| x y |
|
||||
x := 200@100. "obtain a new point"
|
||||
@ -816,12 +867,12 @@ x := 200@100 min: 50@200. "min x and y"
|
||||
x := 20@5 dotProduct: 10@2. "sum of product (x1*x2 + y1*y2)"
|
||||
```
|
||||
|
||||
## Rectangle:
|
||||
#### Rectangle:
|
||||
```
|
||||
Rectangle fromUser.
|
||||
```
|
||||
|
||||
## Pen:
|
||||
#### Pen:
|
||||
```
|
||||
| myPen |
|
||||
Display restoreAfter: [
|
||||
@ -848,7 +899,7 @@ Display height. "get display height"
|
||||
].
|
||||
```
|
||||
|
||||
## Dynamic Message Calling/Compiling:
|
||||
#### Dynamic Message Calling/Compiling:
|
||||
```
|
||||
| receiver message result argument keyword1 keyword2 argument1 argument2 |
|
||||
"unary message"
|
||||
@ -884,7 +935,7 @@ result := (Message
|
||||
sentTo: receiver.
|
||||
```
|
||||
|
||||
## Class/Meta-class:
|
||||
#### Class/Meta-class:
|
||||
```
|
||||
| b x |
|
||||
x := String name. "class name"
|
||||
@ -917,7 +968,7 @@ b := String isWords. "true if index insta
|
||||
Object withAllSubclasses size. "get total number of class entries"
|
||||
```
|
||||
|
||||
## Debugging:
|
||||
#### Debugging:
|
||||
```
|
||||
| a b x |
|
||||
x yourself. "returns receiver"
|
||||
@ -940,7 +991,7 @@ a := 'A1'. b := 'B2'. a become: b. "switch two objects"
|
||||
Transcript show: a, b; cr.
|
||||
```
|
||||
|
||||
## Misc
|
||||
#### Misc
|
||||
```
|
||||
| x |
|
||||
"Smalltalk condenseChanges." "compress the change file"
|
||||
@ -950,12 +1001,23 @@ Utilities openCommandKeyHelp
|
||||
|
||||
## Ready For More?
|
||||
|
||||
* [GNU Smalltalk User's Guide](https://www.gnu.org/software/smalltalk/manual/html_node/Tutorial.html)
|
||||
* [smalltalk dot org](http://www.smalltalk.org/)
|
||||
* [Computer Programming using GNU Smalltalk](http://www.canol.info/books/computer_programming_using_gnu_smalltalk/)
|
||||
### Online Smalltalk systems
|
||||
Most Smalltalks are either free as in OSS or have a free downloadable version with some payment required for commercial usage.
|
||||
* [Squeak](https://www.squeak.org)
|
||||
* [Pharo](http://pharo.org)
|
||||
* [Smalltalk/X](https://www.exept.de/en/smalltalk-x.html)
|
||||
* [Gemstone](http://gemtalksystems.com/)
|
||||
* [VA Smalltalk](http://www.instantiations.com/products/vasmalltalk/)
|
||||
* [VisualWorks Smalltalk](http://www.cincomsmalltalk.com/)
|
||||
|
||||
### Online Smalltalk books and articles
|
||||
* [Smalltalk Programming Resources](http://www.whoishostingthis.com/resources/smalltalk/)
|
||||
* [Smalltalk Cheatsheet](http://www.angelfire.com/tx4/cus/notes/smalltalk.html)
|
||||
* [Smalltalk-72 Manual](http://www.bitsavers.org/pdf/xerox/parc/techReports/Smalltalk-72_Instruction_Manual_Mar76.pdf)
|
||||
* [GNU Smalltalk User's Guide](https://www.gnu.org/software/smalltalk/manual/html_node/Tutorial.html)
|
||||
|
||||
#### Historical doc
|
||||
* [BYTE: A Special issue on Smalltalk](https://archive.org/details/byte-magazine-1981-08)
|
||||
* [Smalltalk-72 Manual](http://www.bitsavers.org/pdf/xerox/parc/techReports/Smalltalk-72_Instruction_Manual_Mar76.pdf)
|
||||
* [Smalltalk, Objects, and Design](https://books.google.co.in/books?id=W8_Une9cbbgC&printsec=frontcover&dq=smalltalk&hl=en&sa=X&ved=0CCIQ6AEwAWoVChMIw63Vo6CpyAIV0HGOCh3S2Alf#v=onepage&q=smalltalk&f=false)
|
||||
* [Smalltalk: An Introduction to Application Development Using VisualWorks](https://books.google.co.in/books?id=zalQAAAAMAAJ&q=smalltalk&dq=smalltalk&hl=en&sa=X&ved=0CCgQ6AEwAmoVChMIw63Vo6CpyAIV0HGOCh3S2Alf/)
|
||||
* [Smalltalk Programming Resources](http://www.whoishostingthis.com/resources/smalltalk/)
|
||||
|
Loading…
Reference in New Issue
Block a user