-
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
Does support planned for type annotations? http://flowtype.org/ #3743
Comments
None. |
Thanks |
Out of curiosity, if Flow takes off, can someone comment on how difficult would it be to add its type annotations to CS? Down the road it, would be really neat to have an option for static type checking in CS! |
Parameter type annotations should be fairly easy to do. "type" declarations should be fairly easy as well. You need to find a new syntax for return type annotations. JSX support would be kinda hard (if you want that...) to do, though it exists on github for CoffeeScript (you need to make it parse and reoutput the type annotations, though) |
Not terribly hard. It's been done before. Check out typed-coffee-script, and Closure Mode for CoffeeScript. Have fun! |
thank you! |
Curious, what is the reason for resistance to this? It seems like a very valuable feature that many have been repeatedly requesting over time (#1852, #3765, this one, and maybe others). I can't think of any negative impact that doing optional parameter type annotations (ala Flow) would have on the language, only benefits. Personally I prefer the Flow-like solution of annotating parameters. TypedCoffeeScript and Contracts.coffee both seem to add too much verbosity and duplication to the language. So why not let someone make a PR with a Flow-like solution? |
You ask the question — it's been answered before, but here it is again: Because it's not in the style of CoffeeScript. CoffeeScript wants to be effective, and minimalist. It doesn't want to be safe and hand-holdy, and bubblewrapped. If you write a complete optionally-typed CoffeeScript program, and then remove all of the optional types, the program will run in precisely the same way. They're literally useless to the functioning of the program, and only serve as crutches during development. (And only if you happen to be the kind of person that happens to write code with a bunch of TypeErrors in it as you go.) Why you would want to add a bunch of extra do-nothing annotations in a fundamentally dynamic language is beyond me. But, that said, if it floats your boat then go for it. CoffeeFlowScript would be great to have. I just won't be using it. |
Thanks for the answer @jashkenas! 👍 Really appreciate it, and sorry I wasn't able to find wherever you shared it previously. |
@taoeffect, I think you can do it easily. |
These discussions always boil down to opinions about static typing and whether even tacit support for such is "CoffeeScripty", however I think a generic pass-through annotation mechanism would still be useful. Annotations don't only have to be used for typing. As @jashkenas has mentioned before you can fake some of this at the function level, but inline/parameter annotations take forking the language itself (not saying that's an illegitimate approach, just fragments the community). |
Woohoo, got this working with https://github.com/jareware/flotate ! Just annotate your functions with block comments (###) and you're good to go. The line numbers don't match up to the CS but I can live with that... |
@srb- do you have an example? I was not able to get it to work with CS methods as they are defined as function expressions and there is no way to interpose the block comment. jareware/flotate#7 |
I haven't tested extensively, so maybe I spoke too soon, or I'm trying too simple of a case. But the following works for me:
|
@srb- try |
Sure. In that case it gives me an error:
Don't forget to perform your CS to JS conversion before running flotate. |
What I have been able to determine is that the type check is failing only because Flow knows that |
@srb- or @ahamid or anyone else: some help with Flotate and CS (if you happen to know the answer): jareware/flotate#10 |
I am trying to run examples above. ### @flow ###
###: (x: string, y: number): boolean ###
foo2 = (x, y) ->
x.length * y is 5
foo2('Hello', '42') # gives an error in FlowType
class Bob
###: (x: number): number ###
calculate: (x) ->
x * x
b = new Bob()
b.calculate(4) # no error in FlowType Gets for me compiled into: // Generated by CoffeeScript 1.9.1
/* @flow */
/*: (x: string, y: number): boolean */
(function() {
var Bob, b, foo2;
foo2 = function(x, y) {
return x.length * y === 5;
};
foo2('Hello', '42');
Bob = (function() {
function Bob() {}
/*: (x: number): number */
Bob.prototype.calculate = function(x) {
return x * x;
};
return Bob;
})();
b = new Bob();
b.calculate(4);
}).call(this); But this fails with:
### @flow ###
###: (x: string, y: number): boolean ###
foo2 = (x, y) ->
x.length * y is 5
foo2('Hello', '42') # gives an error in FlowType
class Bob
###: (x: number): number ###
calculate: (x) ->
x * x
b = new Bob()
b.calculate(4) # no error in FlowType Compiles to: // Generated by CoffeeScript 1.9.1
/* @flow */
(function() {
var Foo;
Foo = (function() {
function Foo() {}
/*: (x: string, y: number): boolean */
Foo.prototype.foo = function(x, y) {
return x.length * y === 5;
};
return Foo;
})();
new Foo().foo('Hello', 42);
new Foo().foo(100, 42);
}).call(this); And fails with:
I am using:
What I am doing wrong? Also, does float works with class Bob
###: (x: number): number ###
calculate: (x) =>
x * x And types on |
flotate supports
but not
wondering as well how to manage this |
@ahamid made this pull request which should fix it, but it does not change anything for me: jareware/flotate#9 (Maybe I am running the branch in some wrong way. I am getting the same error message as with default flow.) |
CS often doesn't put comments exactly where you expect them to be in the compiled JS, which tends to break systems that require "magic comments" like this. Instead of using
I'm not sure if it will work, but I think it's worth a try. |
"Magic" comments still do not help. Yes, they move the comments inside blocks, but the major problem is that Coffee generates |
Hmm... I can't find my old exact setup as I abandoned FlowType for TypeScript, but I think my setup would've been CS 1.8 and whatever Flotate was current at the time of my writing. I used Gulp maybe that played a role? |
Note that if you do...
...it'll add a semi-colon to your comment, which can break stuff like source and source map URIs. Use this instead...
|
@mitar the patch was on |
Check this: This works! Hope help you. ;) |
You can say exactly the same for tests. They are also literally useless to the functioning of the program, and only serve as crutches during development. Types do contribute to the speed of development, by reducing delay between introducing a bug and noticing it. With types it happens instantly, without only when you run the program / tests. |
See also coffeescript6/discuss#12 |
Flow’s comments syntax is now supported, since #4572. |
Would like to know if there are any plans to support http://flowtype.org/ for CoffeeScript compiler
The text was updated successfully, but these errors were encountered: