-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constructor Arguments enhancements (4) #92
Comments
First off, you win the prize for prettiest issue of the week: I'll need more time to give you a more than superficial response, but at first glance:
All that aside, your suggestions go together hand in hand, and would make for an interesting branch of CoffeeScript, if you want to take a stab at implementing it. I'm sure there are plenty of interesting avenues you could take, if you decided to allow predefined helper functions and compile-time type checking. Thanks for the lovely ticket. |
I like the idea for a |
I agree about the type-checking, it's not really in the spirit of the language. However, as pointed out in the issue, sometimes it is important an argument be of a specific type, and it's a pain having to run conversions on each one. Having a quick way to ensure the arguments are correct at runtime sure would be handy. |
it would be neat if the pattern matching could work with the [a,b,c] ||= [x,y,z] compiling to var __a, a, b, c; __a = [x, y, z]; a = a ? a : __a[0]; b = b ? b : __a[1]; c = c ? c : __a[2]; |
Getting all flavors of assignment to work with pattern matching is going to be a bit of a trick, but conditional assignment based on existence is now on master.
Compiles to:
Note that we don't have to do the string comparison on ... And finally, you can now use the existential operator infix as well:
|
As for syntax that's a shortcut for
|
Ah - nicely done with all these jash. |
I'm unsure about the ambiguity of saving a property on the context. |
Yeah, I think it's too fuzzy as well -- overloads the colon to the breaking point. Closing the ticket... |
Using |
First, Thanks for the Award! In theory, you could optimize
To compile to:
or
and save the unnecessary assignment of:
when a is undefined. Last time I checked (a long time ago) IE 6 died on I like the idea of the new infix existential operator:
But it may look too close to ternary op I can see JavaScript coders mis-reading the code as:
instead of
|
@Tesco
but does not do primitive types:
is false even though the constructor is true:
So something to ease the pain of doing type checking would be great! Maybe I should just make @weepy:
|
Underscore.js already has a pretty nice isEqual, isEmpty, isElement, isArray, isArguments, isFunction, isString, isNumber, isDate, isRegExp isNaN, isNull, isUndefined |
I have some suggestions for making CoffeeScript Classes even easier to use
in a friendly, "Unfancy" way.
this.var_name = var_name;
default ??
1.) Allow a "default" in function args or ?? in expressions as a shortcut.
Existence (?) is great, but it can be even better if we can use it like the || operator.
// in javascript, all 4 would become:
which is much smarter way to do it than
Bug-prone CoffeeScript example:
See http://gist.github.com/274158
for more examples of common JavaScript class parameter shortcut bugs.
Better CoffeeScript example:
Best, "Unfancy" CoffeeScript example:
my
my: => this.attr: attr
becomes a one liner:
It would be nice to have in the compiler so it works well with default:
// for Horse example: Horse: my name, my speed default 45 =>
mustbe, force
A "mustbe" (throw Error) or "force" (try to convert to new Class) clause, which ideally would be checked at compile time (against primitives, anyway) instead of while running, would be great!
Sample Javascript and tests for ArgTypeError and isType are in this Gist:
http://gist.github.com/277078
Example Usage
// See: http://gist.github.com/gists/274170
Note: without the "force Number" clause, the foal unexpectedly escapes:
instead of
The text was updated successfully, but these errors were encountered: