• Lexical Binding

    From Lawrence D'Oliveiro@21:1/5 to All on Wed Jul 3 06:07:48 2024
    With so many tutes and books about Smalltalk, it seems hard to find
    anything resembling a formal language description.

    For example, I wanted to define a simple function factory, to demonstrate lexical binding. There are plenty of how-tos on defining objects, but
    almost nothing that talks about bare functions.

    Anyway, here’s what I managed to come up with:

    f := [:n||i| i := n. [i := i + 1. i printNl]].
    f1 := f value: 5.
    f2 := f value: 2.

    f1 value.
    f2 value.
    f1 value.
    f2 value.

    And here’s the output:

    6
    3
    7
    4

    As intended, each invocation of f creates a new instance of the inner
    function, with its own environmental bindings for the values of n and i.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)