satyr/coco

[Feature request] Add . (dot) or other symbol(s) as variable reference in assignment block

demark opened this issue · 5 comments

Currently we have this sugar: some.long.long.long.variable .= to-upper-case!

But we often have a lot of functions from some libs that takes var as argument and returns new value.

I suggest to add . (dot) as variable reference in the assignment block:

some.long.long.long.variable = '<b>hello</b> world!'

some-external-lib.sanitize = (s) -> s.replace /<\/?[^>]+>/g, ''

some.long.long.long.variable =  some-external-lib.sanitize .
                                .replace /\s+/g, '_'
                                .to-upper-case!

And we can use some.long.long.long.variable = . .to-upper-case!

Maybe instead of dot use &. or $& (like in regex)

I think this will be very usable.

I've considered this feature, but been dismissing it due to:

  • difficulty wrt destructuring, e.g.: [a, b] = f(.).
  • the overhead (extra traversing of RHS on each assignment) it would add.

So, what if strict assignment with reference only to ordinary variables (e.g. a = ..., b = ...)
Second, afaik this overhead will only cause on preprocess. In js-output there only need to replace reference symbols. Am i right?

only to ordinary variables

Yup, we'd have to disallow it on destructuring LHS.

this overhead will only cause on preprocess

Right.

Whipped up a trial impl there. Some notes:

  • Simbol is <>. Standalone . as proposed is too iffy.
  • Does not cross-scope. E.g. f = -> <> is syntax error.
  • Turns out destructuring is naturally prohibited because RHS is evaluated first and gets stray error.

Merged as is.