Search code examples
oopfunctional-programmingprogramming-languagessmalltalklanguage-design

Why does Smalltalk support first class functions, but not other functional features?


An answer to Is Javascript a Functional Programming Language? said that:

Functional programming means that the program is conceptualized as a evaluation of a function, rather than a control flow. The code is a description of functions, and has no inherent concept of a control flow.

I've learnt that when a language supported first class functions, and had no control flows, from its design objective, it must be defined as a functional language.

So why does Smalltalk, a functional language, not support other functional features, such as immutability, algebraic data types, pattern matching, partial application?


Solution

  • Smalltalk was designed on top of the following features provided by the Virtual Machine

    1. Object allocation: The #basicNew and #basicNew: primitives
    2. Automatic deallocation: The GC
    3. Message sends: The send family of bytecodes
    4. Blocks: The [:arg | ...] syntax (see below)
    5. Non-local returns: The [:arg | ... ^result] syntax
    6. Late binding: The method lookup mechanism
    7. Native code compilation: The interpreter (see below)

    Modern implementations added

    1. Block Closures: Which replaced blocks
    2. Fast compilation: The JIT compiler, which replaced the interpreter
    3. Stack unwind: The #ensure: message

    Note that other "features" such as the Smalltalk Compiler, the Debugger or the Exception mechanism are not in the list because they can be derived from others (i.e., they are implemented in user code.)

    These features where identified as the fundamental building blocks for a general purpose Object Oriented environment meant to run on the bare metal (i.e. with no Operating System support.)

    What the designers had in mind wasn't Functional Programming. Instead they had in mind the every thing is an object and every computation is a message send uniform metaphor. To this end, blocks and non-local returns played the role of modeling "functions" as objects too so to make sure that every known concept got included in the OO paradigm. This doesn't mean that they had functional programming as a goal. They didn't include other features (functional or not) because they were trying to identify a minimal set of primitive elements that would support a general purpose system with no hindrances.