Live data from Hacker News

Kal – a clean JavaScript alternative without callbacks

rzimmerman.github.io

51–60 of 130 posts

Re: Kal – a clean JavaScript alternative without callbacks

#51

JS without callbacks... so JS without the best part of JS?

How exactly are callbacks the best part of JS? Callback aren't a language feature, they're a pattern of doing async code with anonymous functions. Anonymous functions are used for far more than just async code and cannot be replaced with an await keyword which only replaces the need to use callback for async code.

Re: Kal – a clean JavaScript alternative without callbacks

#52

For the curious, the equivalent example from the OP using promises[1]: function getUserFriends(userName) { return db.users.findOne({ name: userName }).then(function (user) { return db.friends.find({ userId: user.id }); }); } [1]: http://promises-aplus.github.io/promises-spec/

It's more obvious if you have multiple levels.

  function getUserFriends(userName) {
    return db.users.findOne({ name: userName })
      .then(function (user) {
        return db.friends.find({ userId: user.id });
      })
      .then(function (friends) {
        return somethingAsync(friends);
      });
  }

Re: Kal – a clean JavaScript alternative without callbacks

#53
post #46

I'm puzzled by the bootstrapping process. It's written in Kal itself, and you distributed compiled code as JS. Do you have a way of re-boostrapping from only the source in github?

The only way to re-bootstrap is to download a compiled (JS) release from npm, then use that to compile the .kal source in the git repository. Then you can use your newly compiled version of kal to recompile itself. This is how I create the npm releases.

It's not turtles all the way down, though. If you look way back, the 0.1ish releases of the compiler were written in CoffeeScript. I ported the compiler source over from CoffeeScript file by file as it matured and eventually got rid of the dependency entirely. Technically, you could start with the last release that was written in CoffeeScript and compile up to the current release.

Re: Kal – a clean JavaScript alternative without callbacks

#54
post #19

Excellent work. Especially useful since it appears to throw exceptions. That means it's a step up from Iced Coffeescript. The syntax is a little verbose and might be shortened to something like C#: `user = await db.users.findOne {name:userName}`. Would be a lot more clear to me.

As far as I can tell from reading the readme, it does NOT support throwing exceptions from within callbacks.

Because, of course, there's no way to technically do that in anything that is JavaScript at its core -- at least as far as I'm aware of. Correct me if I'm wrong?

It says "This includes error handling via callbacks." -- not error handling via exceptions. Elsewhere it says "Any errors reported by fs.readFile (returned via callback) will be thrown automatically.", so maybe it converts certain callback error functions to exceptions? But in any case, that's not the same as throwing an exception inside of a callback, and having it "bubble up". Unless I'm misunderstanding (which would be great!).

Re: Kal – a clean JavaScript alternative without callbacks

#55
post #47

Does anyone know if there is a JS dialect out there that adds support for callback-less async code without also adding a bunch of other coffeescripty syntax changes at the same time?

Yes, it's called "JavaScript". Specifically, combining generators with promises.

http://blog.alexmaccaw.com/how-yield-will-transform-node

Definitely not as clean as a whole new language, but you can program like this today, at least in bleeding edge node and some recent browsers.

Re: Kal – a clean JavaScript alternative without callbacks

#56

For the curious, the equivalent example from the OP using promises[1]: function getUserFriends(userName) { return db.users.findOne({ name: userName }).then(function (user) { return db.friends.find({ userId: user.id }); }); } [1]: http://promises-aplus.github.io/promises-spec/

Thanks for the comparison, I should probably add promise syntax to the landing page to cover more bases.

I think a useful case would be the trickier stuff where Kal really shines. For example, doing async stuff inside of loops, conditional calls (where some paths need async waits and others don't) and error handling. I also think the Kal syntax might appeal to people who are less experienced with JavaScript and it's more eclectic features.

Re: Kal – a clean JavaScript alternative without callbacks

#57

It's remarkable how similar this code looks to the monadic way of doing it. For this Kal code... task getUserFriends(userName) wait for user from db.users.findOne {name:userName} wait for friends from db.friends.find {userId:user.id} if user.type is 'power user' for parallel friend in friends wait for friendsOfFriend from db.friends.find friend for newFriend in friendsOfFriend friends.push newFriend unless newFriend…

Thanks! I had not thought to make the comparison to Haskell and monads. That's pretty interesting.

A lot of people prefer symbols over keywords, and I think that's mainly a readability issue. I personally prefer more keywords with good syntax highlighting, so that's what I went with (and why I almost immediately made a .tmbundle).

Re: Kal – a clean JavaScript alternative without callbacks

#58

It's remarkable how similar this code looks to the monadic way of doing it. For this Kal code... task getUserFriends(userName) wait for user from db.users.findOne {name:userName} wait for friends from db.friends.find {userId:user.id} if user.type is 'power user' for parallel friend in friends wait for friendsOfFriend from db.friends.find friend for newFriend in friendsOfFriend friends.push newFriend unless newFriend…

This is beautiful work! I see small shades of inspiration from the .Net TPL[1] and the more recent async-await paradigm[2] in C#. But, this is way more concise and beautiful implementation :-)

[1] - http://msdn.microsoft.com/en-us/library/vstudio/system.threa...

[2] - http://msdn.microsoft.com/en-us/library/vstudio/hh191443.asp... and http://msdn.microsoft.com/en-us/library/vstudio/hh156528.asp...

Re: Kal – a clean JavaScript alternative without callbacks

#59
post #43

Earlier quoted context omitted.

Definitely play with the exception handling. Latent bugs aside, asynchronous statements (wait fors) work in any crazy horrible nested combination of ifs, fors (parallel or series), and trys that you can think of. Take a look at the JS output with: kal -o output.js -f beautify input.kal to see the sausages being made. The http server demo is a good example ( https://github.com/rzimmerman/kal/blob/master/examples/async…

A side-by-side line-by-line interactive display would be wonderful in writing Kal. Reading the JS explains what it's doing quite well, except that the Kal compiler uses way too many compound comma-separated statements instead of semicolons.

That's actually a side effect of uglify.js's beautify, I believe. I'm going to try to get rid of that in upcoming releases.

Re: Kal – a clean JavaScript alternative without callbacks

#60
post #7

The title is kinda funny to the russian ear because kal means feces in Russian :)

That is really unfortunate... I chose it because it in Hebrew it roughly means something like easy/simple/BASIC. edit: More importantly, does that make you more or less likely to use it?

Actually, doesn't affect the likelihood of me using it :)
Post reply on HN