News for hackers. Stuff that matters.
Re:Show HN: I built an extension of JavaScript in a day by dmitrig01 on Saturday August 16, 2014 @05:02PM (#8187569)
1. Instead of implementing it in vanilla JavaScript, consider creating a Sweet.js macros (or series of macros), so it can be used with other flavors of JS as well (e.g. JSX, etc)

2. On the async functions – what happens if you put an async function inside a conditional? This is effectively solved by ES6's yield + co or an equivalent.

3. A nitpick, but, is there a way to create static functions on classes? E.g. Car.x instead of Car.prototype.x?

Re:Show HN: I built an extension of JavaScript in a day by chmod775 on Saturday August 16, 2014 @05:06PM (#8187582)
1. uh... oh

2. I'm not sure if I understand your correctly, but it should work if you wrap it in (). I actually have that very same example in 'broken.js' [0]

3. Sure. As you can write normal JavaScript, just use:

   Car.x = (arg){
       //etc
   }
The ':' operator is supposed to be a shortcut for X.prototype.Y

[0]: https://github.com/binlain/designtojs/blob/master/test/broke...

Re:Show HN: I built an extension of JavaScript in a day by dmitrig01 on Saturday August 16, 2014 @05:43PM (#8187676)
Sorry, should have clarified on 2. As an expansion of one of the examples given:

    loadAndPrintFile: (contents) {
       if (!contents) {
         fs.readFile() -> (error!, contents)
       }
       console.log(contents);
    }
i.e., expected behavior would be as if readFile was not an async function.

I'm asking because I built something similar (though it only worked with promises, not general async functions), and I'm curious to compare approaches :-) - mine is a sweet.js macro: http://pastie.org/9410315 (uses https://github.com/petkaantonov/bluebird and https://github.com/BranchMetrics/promise-accum). The basic syntax would be:

    task {
      item1 <- LoadItem(1);
      item2 <- LoadItem(item1.foreign_key);
      ret <- DoSomethingWithItems(item1, item2);
    }
Re:Show HN: I built an extension of JavaScript in a day by chmod775 on Saturday August 16, 2014 @05:56PM (#8187706)
The JavaScript result of your example looks like this:

(again, beautified because DesignToJS crams everything in one line to preserve the line numbers of your actual code):

  function loadAndPrintFile(contents) {
    if (!contents) {
      fs.readFile(function(error, contents){
        if(error !== null && error !== undefined){
          return error;
        }
      });
    }
    console.log(contents);
  }
If you omit the function body, it will only expand to the end of the current logical 'context' (or whatever it's called)

I'd suggest writing it like this in DesignToJS:

  loadAndPrintFile: (contents! console.log) {
    fs.readFile() -> (error!, contents! console.log)
  }
Result looks like this:

  function loadAndPrintFile(contents) {
      if(contents !== null && contents !== undefined){
          return console.log(contents);
      }
      fs.readFile(function(error, contents){
          if(error !== null && error !== undefined){
              return error;
          }
          if(contents !== null && contents !== undefined){
              return console.log(contents);
          }
      });
  }

Edit: I just submitted a patch that avoids unnecessary binding of 'this' if 'this' is not actually used.
Re:Show HN: I built an extension of JavaScript in a day by asynchrony on Sunday August 17, 2014 @12:32PM (#8189717)
In Javascript (contents !== null && contents !== undefined) is equivalent to (contents != null).
Re:Show HN: I built an extension of JavaScript in a day by chmod775 on Sunday August 17, 2014 @03:52PM (#8190250)
Now that is good to know, thank you. I'll make that change asap.
Re:Show HN: I built an extension of JavaScript in a day by dmitrig01 on Saturday August 16, 2014 @07:01PM (#8187889)
Ah, very interesting use of the ! operator.
Re:Show HN: I built an extension of JavaScript in a day by m90 on Saturday August 16, 2014 @03:44PM (#8187296)
You'll have a hard time fighting GitHub classifying `.djs` as DogeScript....

Nevertheless, good luck with the project and everything.

Re:Show HN: I built an extension of JavaScript in a day by arknave on Saturday August 16, 2014 @04:53PM (#8187538)
Looks like the readme example doesn't work. I didn't compile the djs sayHello function to verify it matches the given javascript, but running sayHello("nyc") spits out

  Hello to undefined, nyc
  You didn't tell me your age
When I'd assume you wanted

  Hello to nyc, Tim
  You didn't tell me your age
Re:Show HN: I built an extension of JavaScript in a day by chmod775 on Saturday August 16, 2014 @05:02PM (#8187570)
The first argument is the name, the second is city.

If you give a third, the second will be your age and the third your city.

Example:

   sayHello('Andreas', 'Berlin')
   sayHello('Andreas', 21, 'Berlin')
Re:Show HN: I built an extension of JavaScript in a day by pearknob on Saturday August 16, 2014 @05:04PM (#8187574)
Firstly, congratulations on making something useful in one day. Great job. I skimmed over the documentation/examples and this is excellent.
Re:Show HN: I built an extension of JavaScript in a day by tshadwell on Saturday August 16, 2014 @03:53PM (#8187325)
By the way, `typeof X === 'undefined'` doesn't need to be used for variable names that exist such as arguments, you can use X == undefined without opportunity for failure.
Re:Show HN: I built an extension of JavaScript in a day by woogley on Saturday August 16, 2014 @05:34PM (#8187659)
Technically, all edge cases considered, `typeof` is the only fool-proof check, since you can't override the string constant `'undefined'` but the `undefined` identifier is fair game.

Here is one such edge case:

    (function () {
      var undefined = true;
      return (function foo(x) {
        return x === undefined;
      }());
    }()); // false
Language pedantry aside, browsers these days will freeze `window.undefined` so you'd have to shoot yourself in your own scope ;)
Re:Show HN: I built an extension of JavaScript in a day by tshadwell on Thursday August 21, 2014 @11:15AM (#8207721)
if you're that determined to shoot yourself in the foot you might as well rebind valueOf
Re:Show HN: I built an extension of JavaScript in a day by chmod775 on Saturday August 16, 2014 @03:55PM (#8187337)
Thank you! I'll fix it asap.
Re:Show HN: I built an extension of JavaScript in a day by phpnode on Sunday August 17, 2014 @01:55AM (#8188696)
This looks cool but I think your syntax clashes with JS's standard LabeledStatement
Re:Show HN: I built an extension of JavaScript in a day by chmod775 on Sunday August 17, 2014 @04:07PM (#8190291)
Good thinking, but I already do detect and handle that case correctly.

There's a test case in 'test/namedThings.(d)js'