The Object Mind

Functional ParadigmPure FunctionsCompositionRecursion

Imagine a chess game as a conversation. The board says "I have pieces here and here." You ask the knight "Where can you move?" It replies "These squares." You tell it "Move to f3." It updates itself. The board notices. The game continues.

This is object-oriented thinking: a world of entities that know things, can do things, and communicate through messages. Each entity is responsible for its own behavior. Collaboration emerges from their interactions.

Thinking in Entities

The object-oriented mind sees computation as collaboration among actors. Each actor---each object---has:

  • Identity: It's a distinct "someone"
  • State: It knows things (its attributes)
  • Behavior: It can do things (its methods)
wollok
    // A knight object (Wollok)
    
    object knight {
        var position = f3
        const color = white
        
        method moveTo(square) { ... }
        method validMoves() { ... }
        method capture(piece) { ... }
    }

Messages: Asking, Not Taking

In object-oriented thinking, we don't reach into an object and change its internals. We send it a message---a request.

wollok
    // Procedural: we manipulate from outside
    knight.position = g5

    // Object-oriented: we ask the knight (Wollok)
    knight.moveTo(g5)

The object decides how to respond. Maybe it validates the move first. Maybe it refuses if the square is occupied. The sender doesn't know or care about the implementation---it just makes the request.

Encapsulation: Private Knowledge

Objects can keep secrets. Some knowledge is private---other objects can't see or change it directly.

wollok
    // Wollok: var without 'property' is private by default
    object piece {
        var hasMoved = false    // private - only the piece knows this
        
        method canCastle() {
            if (hasMoved) return false
            // ... other conditions
        }
    }

This is encapsulation: bundling data with the behavior that operates on it, and controlling access. Each object is a small fortress, exposing only what it chooses to.

Collaboration: Objects Working Together

No object does everything. They collaborate:

wollok
    object game {
        var board
        var whitePlayer
        var blackPlayer
        
        method playTurn() {
            const current = self.currentPlayer()
            const move = current.chooseMove(board)
            if (board.isValid(move)) {
                board.apply(move)
                self.switchPlayer()
            }
        }
    }

The game orchestrates, but delegates. The player knows how to choose. The board knows how to validate. Each object has a clear responsibility.

Polymorphism: Same Message, Different Response

Different objects can respond to the same message in different ways:

    // All pieces respond to validMoves() (Wollok)
    
    pawn.validMoves()     // returns forward moves, diagonal captures
    knight.validMoves()   // returns L-shaped moves
    queen.validMoves()    // returns lines and diagonals

This is polymorphism: many forms, one interface. The game can process pieces without knowing their types:

wollok
    // Wollok: using flatMap to collect all moves
    const allMoves = board.pieces().flatMap { piece => piece.validMoves() }

Classes: Templates for Objects

When many objects share the same structure and behavior, we define a class---a template:

wollok
    class Knight {
        var position
        const color
        
        method validMoves() {
            // L-shaped moves from current position
        }
        
        method moveTo(square) {
            position = square
        }
    }
wollok
    const whiteKnight = new Knight(position = g1, color = white)
    const blackKnight = new Knight(position = g8, color = black)
    
    // Different objects, same behavior

The Object Worldview

The object mind sees the world as:

Languages like Smalltalk, Java, Ruby, and Wollok embody this worldview. In OOP, the world is a society of cooperating entities.

  • Objects: Entities with identity, state, and behavior
  • Messages: Communication between objects (requests, not commands)
  • Encapsulation: Each object controls its own state
  • Polymorphism: Same message, different behaviors
  • Collaboration: Complex behavior emerges from simple interactions

Mutable but Contained

Unlike the functional mind, objects typically are mutable---they change over time. But unlike raw procedural code, the mutation is contained:

wollok
    knight.moveTo(e5)     // the knight changes internally
                          // but we don't see the mechanics

The object manages its own transitions. The outside world sees only before and after through the object's responses to messages.

The object mind thinks: "The world is made of entities that know their own business. I don't control everything---I collaborate with others." It's a social worldview, modeling computation as communication in a community.

We've now seen three ways of thinking: procedural (sequences of commands), functional (transformations of values), and object-oriented (communities of collaborating entities). Each has its own logic, its own strengths, its own way of seeing.

In the chapters ahead, we'll discover that beneath these different surfaces lie common patterns---the building blocks of computation that appear in every paradigm.