coffeescript existential function operator else statement?

coffeescript, javascript

Solution

You can add another existential operator:

x = f?() ? 'pancakes'

That won't work if `f()` returns `null` or `undefined` but it will do the Right Thing if `f()` returns `false`. For example:

f = 'not a function'
console.log f?() ? 'pancakes'
# pancakes

f = -> 'is a function'
console.log f?() ? 'pancakes'
# is a function

f = -> null
console.log f?() ? 'pancakes'
# pancakes

f = ->
console.log f?() ? 'pancakes'
# pancakes

f = -> false
console.log f?() ? 'pancakes'
# false

Demo: http://jsfiddle.net/ambiguous/f6yvN/1/

So you can get close to what you want and that might be close enough depending on what sort of things you're expecting the function to return.

Problem

In coffee script, using the existential operator on a function like so: ``` myFunc?() ``` compiles to ``` typeof myFunc === "function" ? myFunc() : void 0; ``` Is there a way to elegantly define what would go in place of "void 0"? or must I write it all out instead of using the original notation?

Original source