Elsewhere, an argument has arisen over the terminology of a named function in CoffeeScript. In particular someone referred to something like this:
foo = ->
console.log("bar")
as a named function. But its been objected that everything in CoffeeScript is anonymous functions and there are no named functions. This is certainly true, CoffeeScript only has function expressions which can then be stored in a variable. But I don’t think that means it is wrong to call this a named function.
As I see it, it is a named function because its a function that has been given a name. True, its not a named function in the same way that some other languages have named functions, but I think its close enough that it’s not inappropriate to call it a named function. To insist otherwise just seems to be nitpicking.
Am I out to lunch in thinking that insisting that this isn’t a named function is just nitpicking?
7
CoffeeScript is inexorably tied to JavaScript, and JavaScript differentiates between the following expressions:
function foo() { ... }
var foo = function () { ... }
In fact, you can even write:
var foo = function bar () { ... }
Since this difference matters in JavaScript, it makes sense to use the same terms when talking about CoffeeScript. However, CoffeeScript does not support anything like the function foo ()
syntax, so we can say it does not have “named” functions.
In a sense, the name is part of the function definition in the function foo() { ... }
, where in the other case you just create a function and assign it to a variable. This is reflected, for example, in the (nonstandard) name
property of functions: in the first case, foo.name
will give you "foo"
and in the second it will give you ""
.
Additionally, in JavaScript, these also differ in terms of how they get introduced to the scope: the first version is “hoisted” and available throughout its scope where the second definition is only available after it’s assigned.
Basically, just think of it as JavaScript-specific jargon, which is transferred to CoffeeScript because CoffeeScript is so closely related to JS.
7
It’s worth noting that the user explicitly stated they were turning an “anonymous function into a named function”, both terms having a strong, existing meanings and notably different functionalities in in the JavaScript world. Given the existing meaning, the were doing no such thing, and I pointed this out.
CoffeeScript is not so far removed from JavaScript that you should be redefining terms they both share to mean something else in one language. CoffeeScript doesn’t exist in some bubble, removed from its JavaScript implementation the way you might argue that C++ is separated from Assembly. Knowing the difference between an anonymous function and a named function matters, because if you expect your “named” CoffeeScript function to behave like an actual named function, you’re going to be disappointed:
doStuff() # I cause an error
# ... later
doStuff = (x,y) ->
alert("Were I actually a named function, this would work!")
The equivalent JavaScript would work fine, with a real named function:
doStuff(); // I work just fine!
// later....
function doStuff() {
alert("I'm a real named function!")
}
You might be right that I’m just “nitpicking”, but so what? The subtle points in computer programming matter, and being “technically” correct is important. It’s the difference between writing code that happens to work, and actually understanding why your code is correct.
4
Am I out to lunch in thinking that insisting that this isn’t a named function is just nitpicking?
No. After all, in terms of semantics, your function reference is stored in a variable, which you can refer to via a variable name.
Definitely not a nitpick, imo. I use it extensively for readability:
readfile()
dothis()
dothat()
thistoo()
writefile()
function readfile() {
...
}
...
Thus:
Real named function in “coffeescript”
hello()
`function hello() {`
console.log 'hello'
dothings()
`}`
You escape pure JS via the backtick `
Note that you can’t indent on your function body.
Cheers
Don’t waste time arguing with pedants. It’s never fruitful. Yes, everyone knows what you mean by a “named function” in CoffeeScript, even if it’s technically incorrect. No, using technically incorrect but widely understood terminology has no bearing on the correctness or incorrectness of the proposed solution. Is it possible to be more precise without gaining some awkwardness of phrasing? Probably not. However, that doesn’t mean people will let it slide. Just imagine this guy on the other end of the conversation.
The reason it’s technically incorrect is because you haven’t named the function, you’ve named a reference to the function. Consider:
foo = bar = ->
console.log "What's my name?"
What’s the function’s name? foo
and bar
are both referencing the same function, but have different names. Also, either foo
or bar
could be reassigned at any time to reference a different function, or even a different type altogether, without changing the function itself.
So, the way I read this is like this:
When you declare a function in a JavaScript console using
var foo = function() { ... }
and then invoke foo
without parentheses, it returns
function() { ... }
However, if you define it using
function foo() { ... }
and then invoke foo
again without parentheses, it returns
function foo() { ... }
In the first case, I’d say you’re declaring a variable named “foo” storing an anonymous function in it. In the second case, I’d say you’re actually declaring a named function called “foo”.
Because CoffeeScript uses the first pattern, I’d agree that it’s technically correct that CoffeeScript’s functions are all anonymous functions being stored in named variables.