coffee-script
coffee-script copied to clipboard
?= and ||= cant be used on defer function's argument
The following code can't be translated:
foo = (x, cb)->
cb(x)
f = (x)->
await foo(x, defer(bar))
bar ||= ""
#bar ?= ""
alert bar
#THE VARIABLE "BAR" CAN'T BE ASSIGNED WITH ||= (or ?=)
# BECAUSE IT HAS NOT BEEN DEFINED
but this one can:
foo = (x, cb)->
cb(x)
f = (x)->
await foo(x, defer(bar))
bar = bar || ""
alert bar
Is that OK?
Good point, seems like this example is running afoul of a compile-time check that's inherited from CS. For now I'd recommend the workaround of assigning bar = "" before the await. Thanks for your feedback.
Perhaps, something like the behavior below can be implemented?
await foo(defer(bar=x)); -> translates into -> await foo(defer(bar)); bar = bar || x;
I'm running into this same unfortunate behavior. +1
same issue :+1: simple example
await doStuff defer err, result
err ?= new Error '...' unless result?
Seems like the class Defer (around here ?) could use some o.scope.add, or am I missing something?
Only compileRoot, class Code should creates new scopes (and class Class through func.makeScope), so I don't think this should be an issue, but I might be missing something?