maxtaco / coffee-script

IcedCoffeeScript
http://maxtaco.github.com/coffee-script
MIT License
726 stars 58 forks source link

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

Open saatchiCEO opened 11 years ago

saatchiCEO commented 11 years ago

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?

maxtaco commented 11 years ago

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.

saatchiCEO commented 11 years ago

Perhaps, something like the behavior below can be implemented?

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

ELLIOTTCABLE commented 11 years ago

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

alexgorbatchev commented 11 years ago

same issue :+1: simple example

await doStuff defer err, result
err ?= new Error '...' unless result?
vendethiel commented 9 years ago

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?