destructuring - caffeine-suite/caffeine-script GitHub Wiki
See also: Structuring and Restructuring
-
Extract-Destructuring:
a extract b
. Extract is more capable and uses less syntax. Extract returns the last value extracted. -
Assignment-Destructuring:
{b} = a
. Assignment-Destructuring's return-value supports Restructuring.
Assignment-style will feel familiar to JavaScript and CoffeeScript veterans. With the exception of the return-value, assignment-style is identical to JavaScript ES6 Destructuring Assignment, and is a subset of CoffeeScript Destructuring Assignment.
CoffeeScript and Javascript, as of EcmaScript6 (ES6), have "destructuring assignment." At first it seems cool and elegant, but after using it for a few years, I've found it awkward and largely ineffective in all but the simplest cases:
One of CaffeineScript's core goals is Eliminating Bracket Matching.
Nesting ends up taking more tokens than the simpler, old-school, non-pattern assignment.
# Assignment-destructuring:
{a:{b}} = c # 8 tokens
# old-school
b = a.c # 5 tokens
# CaffeineScript's `extract`
a extract b extract c # 5 tokens
Why? Data-flow actually changes directions. In assignment, data flows right-to-left:
{a} = c
# ^----/ # right-to-left data-flow
But in with nested assignment-destructuring, data flows left-to-right:
{a:{b}} = c
# /--^ # left-to-right data-flow
# \--------/ # right-to-left data-flow
With extract, data always flows left-to-right:
a extract b
a extract b extract c
CoffeeScript/JavaScript destructuring returns the object being destructured. I have never found that useful. Instead, I usually want the extracted value returned, usually for testing in an if-statement if it is present. In CaffeineScript, instead of one (almost) useless return value, you have two, equally useful options:
-
a extract b, c
returnsc
, the last value extracted -
{b, c} = a
returns a new object:{b, c}
, destructuring-Assignment's return-value, if used, provides Restructuring