Skip to content

Latest commit

 

History

History
237 lines (207 loc) · 6.61 KB

st1.md

File metadata and controls

237 lines (207 loc) · 6.61 KB


 home | syllabus | src | submit | chat | ©2019 by Tim Menzies

Smalltalk1

"Smalltalk's the source. It'll change your life. Swear to God" --Point Break (sort of)

Tips:

  1. Try completing all of https://people.eecs.berkeley.edu/~fateman/264/papers/smalltalk-tutorial.html
  2. Use me as a method lookup (I'm expecting lots of texts in our how2smalltalk channel).
  3. Browse the source code
    • cd /usr/share/gnu-smalltalk/kernel/
    • methods are indendefed four characters from start of line grep '^ x:' *
    • but do NOT rewrite that code

Basics

  • self =pointer to self
  • super =pointer to superclass
  • true =Only intance of class True
  • false =Only intance of class False
  • nil =Only instance of class UndefinedObject
  • Smalltalk =global, holds all globals; e.g. classes
  • #abc =symbol
  • $x =character
  • " =comments
  • ' =strings
    • BTW, strings are Collections of characters
  • . =end statement
  • ^ =return
  • := =assignment
  • blocks (of code)
    • [] =block
    • [:x| ... ] =block with one argument
    • basis of all control, loops
    • e.g whileTrue:
  • #() =array
  • : =keyword argument
    • 5 between: 3 and: 8 = selector between:and:

Moving on

  • Novel precedence: unary, binary, keywords, brackets
    • _Binary are one,two letter selectors for maths, equality, etc.
    • So 1 + 8/4 is... surprising
  • Postfix messages
    • 1 negated
    • -1 negated
    • false not
    • false not not
    • -1 abs
    • 1 abs
    • 10 factorial
    • 10 factorial sqrt
    • 5 sqrt
    • 1 isNumber
    • $a isNumber
    • $a isNumber not
    • 1 isCharacter
    • $a isCharacter
    • 'someString' first
    • 'hello world' size
    • 'hello world' asUppercase
    • 'hello world' copy
    • 'hello world' copy sort
    • #( 17 99 1 57 13) copy sort
    • 1 class name
    • 1 class name asUppercase

Blocks

  • Blocks. aBlock value: x.
    • e.g. 1 to: 10 by:2 do: [:x| x oo]
    • Number.st

Polymorphism

  • Polymorphism: decentralized control

E.g.

$ cd /usr/share/gnu-smalltalk/kernel/
$ grep  '^    =='  *.st
Object.st:    == arg [
SmallInt.st:    == arg [

$ grep '^    = ' *.st
AnsiDates.st:    = aDateTime [
Association.st:    = anAssociation [
Bag.st:    = aBag [
BindingDict.st:    = arg [
ByteArray.st:    = aCollection [
Character.st:    = char [
CharArray.st:    = aString [
Class.st:    = aClass [
CObject.st:    = anObject [
CompildCode.st:    = aMethod [
CompildMeth.st:    = aMethod [
CompiledBlk.st:    = aMethod [
CType.st:    = anObject [
CType.st:    = anObject [
CType.st:    = anObject [
Date.st:    = aDate [
Delay.st:    = aDelay [
Dictionary.st:    = aDictionary [
ExcHandling.st:    = anObject [
FileSegment.st:    = aFileSegment [
File.st:    = aFile [
FloatD.st:    = arg [
FloatE.st:    = arg [
FloatQ.st:    = arg [
Fraction.st:    = arg [
HashedColl.st:    = aHashedCollection [
Interval.st:    = anInterval [
LargeInt.st:    = aNumber [
LookupKey.st:    = aLookupKey [
Magnitude.st:    = aMagnitude [
MethodInfo.st:    = aMethodInfo [
Object.st:    = arg [
OtherArrays.st:    = anObject [
OtherArrays.st:    = aLargeArray [
Point.st:    = aPoint [
Rectangle.st:    = aRectangle [
RunArray.st:    = anObject [
ScaledDec.st:    = arg [
SeqCollect.st:    = aCollection [
SmallInt.st:    = arg [
String.st:    = aCollection [
Symbol.st:    = aSymbol [
Time.st:    = aTime [
URL.st:    = anURL [
VFS.st:    = aFile [
VFS.st:    = aFile [

E.g. Point =

Everything is an object, even a class.

  • Point is an instance of Class
  • Point class methods are managers of Point instances
    • e.g. Point x: 10 y:10
    • class message to Point that returns an instance with x=10,y= 20

After that, you got to understand the class Hierarchy. Literally, everything is an object.

Object
    Behavior
      ClassDescription
        Class -- and Class is instance of Metaclass
        Metaclass
    BlockClosure -- []
    Boolean
      False -- false
      True  -- true
    CObject
      -- C stuff
    Collection
      Bag
      MappedCollection
      SequenceableCollection
        ArrayedCollection
          Array
          Interval
          CharacterArray
            String
              Symbol
        LinkedList
          Semaphore
        OrderedCollection
          SortedCollection
      HashedCollection
        Dictionary
          IdentityDictionary
          RootNamespace
            SystemDictionary -- Smalltalk
        Set
          IdentitySet
    File
      Directory
    Magnitude
      Association
      Character
      Date
      Number
        Float
        Fraction
        Integer
          SmallInteger
      Time
    Message
      DirectedMessage
    Point
    Rectangle
    Signal -- exception handling. see on:do:
      Exception
        Error
          Halt
            ArithmeticError
              ZeroDivide
            MessageNotUnderstood
          UserBreak
        Notification
          Warning
    Stream
      PositionableStream
        ReadStream
        WriteStream
          ReadWriteStream -- why not under ReadStream?
            ByteStream
              FileStream
      Random
    UndefinedObject -- nil

* on:do: