← Back to context

Comment by lioeters

3 hours ago

Yes, we can look at an object as a function that accepts a key and returns a value (or null). Depends on language, it's called a set, map, or associative list.

  type AnObject = {
    [key: any]: any
  }

  type FunkyObject = (key: any) => Maybe<any>

Then we can see arrays as a limited type of object/function that only accepts a number (index) as key.

  type FunkyList = (index: number) => Maybe<any>

We can even consider any variable as a function whose key is the name.

  type FunkyVariable = (name: string) => Maybe<any>

And any operation as a function whose key is the operator, with arguments, and the return value is the result.

  type FunkyOperator = (name: string, ...values: any) => any

  FunkyOperator('+', 1, 2) // => 3

Even an `if` statement can be a function, as long as the values are functions that return values instead of the values themselves, to allow for "short-circuiting" (latter values are unevaluated if an earlier value is true).

So we approach some kind of functional language land where all data structures are modeled using functions.

Yes, and as you point out even conditional statements.

In Smalltalk constructs like

   b ifTrue: [ ... ] 

mean that the boolean value 'b' has its method (-function) ifTrue: called with the argument [...] which is a "closure" meaning it is a free-standing function (as opposed to bound functions which are the methods).

There are similarly library methods in class Boolean for whileTrue: etc. Functions all the way.

What would be the point of implementing conditional statements as methods/functions? It makes it posssible to extend the set of conditional statements to for instance 3-valued logic by adding a method #ifTrue:ifFalse:ifUnknown: . And of course it makes the syntax of the language very simple.