Method Chaining, Postfix Operator, and OOP
x.f.g. its extremely convenient, widely loved, in ruby too. ♥ ♥ ♥
- however, there's a troublesome aspect, namely, object and properties.
- method chaining relies on object properties to work, which is a semantic issue.
- in most languages there's no postfix operator.
- To chain functions, f must return a object and that object must have next f as property.
- then, you use the dot notation for property access, to achieve method chaining.
- the problem with this is that we rely on a semantic property for its side effect of syntactic convenience.
- if dot notation doesn't exist in js, then
x.f(a).g(b)is written as
x[f](a)[g](b). Would you still want to chain?
- as a practical example, in js, if f g are functions and their return value are not objects, you cannot chain them, as in x.f.g
- to be able to chain arbitrary functions, you need a postfix operator. unix pipe x|f|g, Wolfram Language x//f//g
Note: a related concept is function composition. It has somewhat the same purpose of postfix operator with respect to syntax convenience. Because, it avoids nesting, and lets you write sequential application of functions in a sequential manner. [see What's Function, What's Operator?]
In lisp language that doesn't use operators, function composition is the natural solution. For example, in Clojure, it's called “threading”. [see Clojure: Function Chaining]
[see JS: Pipe Function Instead of Method Chaining]
Unix Pipe, Dot Notation, Postfix Notation
- OOP Dot Notation, Dot Before Data or After?
- OOP Dot Notation, is Object Data or Namespace?
- Unix Pipe as Functional Language
- Method Chaining, Postfix Operator, and OOP
- Ontology of Postfix Notation, Method Chaining, and Unix Pipe
- Piping and Function Composition Equivalence
If you have a question, put $5 at patreon and message me.