117

It seems like CoffeeScript automatically returns the last item in a scope. Can I avoid this functionality?

4
  • Why would you want to? Can't you just discard the returned value when you like? Commented Sep 12, 2011 at 17:22
  • 12
    @Chris: When there's no meaningful return value, leaking whatever happens to be the result of the last statement isn't a good idea. Commented Sep 12, 2011 at 17:24
  • 12
    @Chris this can have performance implications with comprehensions / for / while loops too, as coffeescript may collect the result of each iteration in an array, if the return value of the loop is used (or if it's the last expression of a function) Commented Sep 12, 2011 at 17:34
  • 1
    I just had this issue with a contructor. I defined a function in the last line of the constructor. So this was returned instead of my "Class-Function". I had to explicitly return this. Just saying, that this can be an issue. Commented May 9, 2014 at 14:56

5 Answers 5

148

You have to explicitly return nothing, or to leave an expression evaluating to undefined at the bottom of your function:

fun = ->
    doSomething()
    return

Or:

fun = ->
    doSomething()
    undefined

This is what the doc recommends, when using comprehensions:

Be careful that you're not accidentally returning the results of the comprehension in these cases, by adding a meaningful return value — like true — or null, to the bottom of your function.


You could, however, write a wrapper like this:

voidFun = (fun) ->
    ->
        fun(arguments...)
        return

(Notice the splat operator here (...))

And use it like this when defining functions:

fun = voidFun ->
    doSomething()
    doSomethingElse()

Or like this:

fun = voidFun(->
    doSomething()
    doSomethingElse()
)
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for this informative answer. I too have been wondering about this as I'm presently learning coffeescript. I'm curious if you can shed some light on what the best choice, or what the worst choice would be when preventing the return of a value? Specifically, I have been told the options are to add either return, undefined, or null to the end of my function. However, adding null seems wrong to me for some reason. Am I right to assume it the worst choice out of the three?
In javascript a function without return statement (or an empty return statement) returns undefined, so the best option is to either add an empty return or leave an undfined at the end. Using an empty return statement seems to express return nothing better that leaving an undefined at the end of the function, so the empty return seems to be the better option.
Instead of fun(arguments...), it would be better to call fun.apply(this, arguments).
Actually, "return" and "undefined" generate different javascript. Using "Try Coffeescript" at coffeescript.org, one can see that explicitly including "return" in coffeescript removes the return from the javascript; whereas, with "undefined", the javascript function ends with "return void 0;"
I think you might need to be careful with voidFun because I suspect the inner function will still return whatever it wants which could possibly afaik cause the performance degradation.
|
9

Yes , with a return as the last line of a function.

For example,

answer = () ->
  42

extrovert = (question) -> 
  answer()

introvert = (question) ->
  x = answer()
  # contemplate about the answer x
  return 

If you'd like to see what js the coffee compiles to, look at this. (I've used coffeescript redux for my example)

1 Comment

I think it was downvoted because this answer does not add any value apart from the one already there in the top-voted answer, which also happens to be given 2 years before this one!
5

Just something fun(ctional)

suppressed = _.compose Function.prototype, -> 'do your stuff'

Function.prototype itself is a function that always return nothing. You can use compose to pipe your return value into this blackhole and the composed function will never return anything.

Comments

1
longRunningFunctionWithNullReturn = ->
  longRunningFunction()
  null

Comments

0

It seems functions in CoffeeScript must always return something, even null. In C, you have void as a return type. ->, the empty function, compiles to (function() {}), so it's the only function that doesn't return anything.

1 Comment

That's not true. return; works in C, JavaScript and CoffeeScript to return a void value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.