3 ms·
> set(User, function() { return {name: "John"}; }, silent: true); That's not even valid JavaScript, so I would not expect that. It is also somewhat standar
by arnarbi 14y ago
> set(User, function() { return {name: "John"}; }, silent: true);
That's not even valid JavaScript, so I would not expect that. It is also somewhat standard that lambda-like operators eat as many tokens as possible.
But I agree with many points. The anonymous function sugar is extremely handy in callback-oriented programming. I wonder if a small syntax extension to JS, consisting of Python like indetation and Ruby like blocks would ease the biggest pains:
function f(some_param, callback)
bla()
return 1
f(10) |u,t| ->
callback_code_here
The call to f would receive the anonymous callback as its last argument. Callbacks in other positions, or multiple callbacks could be named:
f(10, success, failure)
succss: |data| ->
console.log("Success!", data)
failure: |error| ->
console.log("Error: ", error)
- insraq 14y agoSorry, that's my bad, it should be { silent: true }. I translate myself and mix CoffeeScript into this. I guess that's a proof of the fact that I cannot live without CoffeeScript. I have corrected in the post.
- mratzloff 14y agoThis function f(some_param, callback) bla() return 1 f(10) |u,t| -> callback_code_here or f(10, success, failure) succss: |data| -> console.log("Success!", data) failure: |error| -> console.log("Error: ", error) are not simpler than valid CoffeeScript, such as f = (someParam, callback) -> bla() callback 1 # presumably your intent f 10, (u, t) -> # callback code here or success = (data) -> console.log("Success!", data) failure = (data) -> console.log("Error: ", error) f 10, success, failure
- arnarbi 14y agoI agree, and I don't want to simplify CS, but rather JS. The thing that bothers me about CoffeeScript is the call syntax without parenthesis but with commas - which seems to me the main source of "surprises". The main benefit of it however is allowing you to write anonymous function arguments without chasing closing delimiters. I would like something that lies in between JS and CS, with that particular benefit but without the weird grammar.
- mratzloff 14y agoMy solution above (naming functions) is one solution to the problem. Wrapping ambiguous objects or arrays is another: f 1, 2, { foo: "bla" bar: "bla" }, (error, result) -> # do something with result For a series of function parameters, the leading comma is another: f.asyncMap (item) -> foo item , (error, result) -> # do something with result
- arnarbi 14y agof.asyncMap (item) -> foo item , (error, result) -> # do something with result The readability of that is terrible. I (and most people) can easily figure out how to make myself understood to the parser, that's not an issue at all. The itch that CS scratches is severe, but I believe there is an opportunity to have something much better in terms of readability and predictability.
- mratzloff 14y agoWhich is why it was the third alternative, and not the first. :-)