coffee-script icon indicating copy to clipboard operation
coffee-script copied to clipboard

?= and ||= cant be used on defer function's argument

Open saatchiCEO opened this issue 12 years ago • 5 comments

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?

saatchiCEO avatar Dec 21 '12 12:12 saatchiCEO

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.

maxtaco avatar Dec 24 '12 15:12 maxtaco

Perhaps, something like the behavior below can be implemented?

await foo(defer(bar=x)); -> translates into -> await foo(defer(bar)); bar = bar || x;

saatchiCEO avatar Jan 17 '13 07:01 saatchiCEO

I'm running into this same unfortunate behavior. +1

ELLIOTTCABLE avatar Mar 22 '13 05:03 ELLIOTTCABLE

same issue :+1: simple example

await doStuff defer err, result
err ?= new Error '...' unless result?

alexgorbatchev avatar Jun 12 '13 02:06 alexgorbatchev

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?

vendethiel avatar Mar 24 '15 10:03 vendethiel