Intro to Smalltalk

From Programmer to Programmers

Created by @ajlopez

https://github.com/ajlopez/Talks/SmalltalkIntro

Using reveal.js

Smalltalk

More than a programming language

Smalltalk

  • Object Environment
  • Objects everywhere
  • Dynamic language
  • Many dialects

Pharo

http://www.pharo-project.org/home

A Quick Tour

Starting Engines

World Menu / Workspace

Hello World

System Browser

Syntax

## Variables ```smalltalk startPoint "a variable name" Transcript "a global variable name" self "pseudo-variable" ```
## Other Declarations and Expressions ```smalltalk | x y | "declaration of variables x and y" x := 1 "assign 1 to x" [ x + y ] "a block that evaluates to x+y" <primitive: 1> "virtual machine primitive or annotation" ```
## Messages ```smalltalk 3 factorial "unary message" 3+4 binary "messages" 2 raisedTo: 6 modulo: 10 "keyword message" ```
## Blocks ```smalltalk [ 1 + 2 ] value "3" [ :x | 1 + x ] value: 2 "3" [ :x :y | x + y ] value: 1 value: 2 "3" [ :x :y | | z | z := x+ y. z ] value: 1 value: 2 "3" ``` Closure ```smalltalk | x | x := 1. [ :y | x + y ] value: 2 "3" ```
## Conditional and Loops ```smalltalk (17 * 13 > 220) ifTrue: [ 'bigger' ] ifFalse: [ 'smaller' ] "bigger" ``` ```smalltalk n := 1. [ n < 1000 ] whileTrue: [ n := n*2 ]. n "1024" ``` ```smalltalk n := 1. [ n > 1000 ] whileFalse: [ n := n*2 ]. n "1024" ``` ```smalltalk n := 1. 10 timesRepeat: [ n := n*2 ]. n "1024" ```

Smalltalk Object Model

## The Rules - Rule 1. Everything is an object. - Rule 2. Every object is an instance of a class. - Rule 3. Every class has a superclass. - Rule 4. Everything happens by sending messages. - Rule 5. Method lookup follows the inheritance chain.
## Every object is an instance of a class ```smalltalk 1 class "SmallInteger" 20 factorial class "LargePositiveInteger" 'hello' class "ByteString" #(1 2 3) class "Array" (4@5) class "Point" Object new class "Object" ```
## The instance side and the class side ```smalltalk aColor := Color blue. "Class side method blue" aColor "Color blue" aColor red "0.0 Instance side accessor method red" aColor blue "1.0 Instance side accessor method blue" ```

SUnit

## TestCase ```smalltalk TestCase subclass: #IntegerTest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'KernelTests-Numbers' ```
## Test Method ```smalltalk testIsPrime "The following tests should return 'true'" self assert: 17 isPrime. self assert: 78901 isPrime. self assert: 104729 isPrime. self assert: 15485863 isPrime. self assert: 2038074743 isPrime. self assert: 29996224275833 isPrime. "The following tests should return 'false' (first 5 are Carmichael integers)" self deny: 561 isPrime. self deny: 2821 isPrime. self deny: 6601 isPrime. self deny: 10585 isPrime. self deny: 15841 isPrime. self deny: 256 isPrime. self deny: 29996224275831 isPrime. ```

Test Runner

Web Development

Seaside Web Framework

http://www.seaside.st/

Local server

THE END

BY Angel 'Java' Lopez / www.ajlopez.com / @ajlopez