disnet
I haven't tried yet but I think all the syntax forms in TypeScript can be handled with just plain sweet.js macros, no need for the readtable. Actually handling the types in TypeScript via macros should also be eventually possible (Typed Racket [1] does typing via macros) once we have a bit more feature parity with Racket's macro system.
True, but the same can be said of any API. Reading the definitions can help but for both complex macros and complex APIs built today out of just functions and objects you need to document your abstractions. Macros don't change this, they just give you another abstraction axis (syntactic) to work with.
As with most things it just depends on how you use it. Sure you can abuse macros to make tons of crazy, undocumented, hard to understand syntactic extensions that destroy readability. But you can already do that today. Used wisely macros can increase readability, used poorly they can decrease it.
> And does the effort to integrate macros into the browser mean that it'll be possible to evaluate macros "runtime"?
Not sure what you mean here. By definition macros are expanded at compile time (well, parse time really). The browser doesn't change this.
The description page (like the rest of the project at them moment) is definitely a work in process. Sorry about the confusion!
> 'Wish the function keyword in JavaScript wasn't so long? What if you could define functions with def instead?'
> Erh, no? Why in the lord's name would I ? Is that your big selling point? 'I don't like function() because it's too long?'
This is just a really basic example of what you can do. Obviously you could have the same effect with sed but we need something simple to shows what basic macro definitions look like.
> 'Want a better way to make "classy" objects?'
> <provides example that looks like php vomited on Javascript after mating with ruby>
I'm assuming you refer to the macro definition with $ and whatnot. If you have ideas for better syntax please contribute! The notation is by no means final. That said I think it's pretty good for what it needs to do, matching syntax patterns and transforming them into new bits of syntax. Certainly more readable than a full parser/compiler.
> Am I the only one that doesnt understand the use case of this? Or is it just another lexer/parser?
The idea is to provide a middle way between no syntax extensions and writing your own full compile-to-js language languages like CoffeeScript which allow you to add any kind of syntax you want, but at the expense of being able to compose extensions. If you like the class notation of CS you must buy into all the syntax choices of CS whereas macros allow you to add just the syntax you want.
Whether you want new syntax is of course a different question. Other languages like scheme and closjure have found macros useful so sweet.js is an experiment to find out if the same syntactic extensibility that they provide is useful in JavaScript.
Hope that helps!
It actually does the right thing if the function is named:
a = function foo() {}
/5/
7
correctly translates to: a = function foo() {
} / 42 / 7;
But clearly I missed the unnamed case. You mentioned finding a few other bugs? Would you mind submitting a bug report on github? I would love to fix those too!It's true though that if you're heavily invested in vim plugins there's fair time sink in getting up to speed on the emacs equivalents. Everything's a tradeoff.
If you compile with "coffee -c" (compile, no contracts) instead of "coffee -cC" (compile, with contracts) you get pure JavaScript, no contracts and no runtime checks. Exactly what vanilla CoffeeScript would have given you.
The fact that we have a compiler doesn't give us all that much. Adding types to a dynamic language (where things can be monkey patched, evaled, and just generally mutated willy-nilly) is a hard problem (not impossible but definitely research [1 and 2 to start, but much more]).
>> And we'd like to do it in a more elegant way than constantly writing "if(argument === ...) then ... else ..."
> This doesn't really seem to solve that problem.
It solves the problem of elegance :)
> It just throws consistent errors if the types don't match. Your code would likely throw its own error if you didn't check the type manually. In my experience, native js errors are as easy or easier to track down that throwing errors yourself.
Key word there is likely. Like I mentioned, there are some errors that don't result in a run-time error and we might like to check for those too. As a silly example, consider a binary search tree [3] that you want to keep balanced. If you get the balance wrong, there's no immediate fault (no TypeError/NPE).
> Ah, ok. That makes more sense. They're like python decorators, only single purpose.
I guess you could say that. If we had an expressive enough decorator-like system for CoffeeScript we could probably implement contracts with it. CS doesn't have one of course so path of least resistance is to add the contract language extension directly.
[1] http://ecee.colorado.edu/~siek/blame-forall-2011.pdf
[2] http://dl.acm.org/citation.cfm?id=1863561
[3] https://github.com/disnet/contracts.coffee/blob/master/test/...
But we would still like to express and enforce some invariants about our code. And we'd like to do it in a more elegant way than constantly writing "if(argument === ...) then ... else ..."
Contracts also allow us to check invariants that might not result in a run-time exception but could lead to the code just being subtly wrong.
They both work to enforce program invariants and there's a large overlap in the invariants they can express. Usually contracts are checked dynamically (as in contracts.coffee) but this is not a hard requirement (for example .net's code contracts can be statically checked [1]).
I mention this briefly towards the end of the quick start section of the doc: http://disnetdev.com/contracts.coffee/#quickstart
As long as wave takes off it'll drive adoption.