Skip to content
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

Closed
rstormsf opened this issue Nov 28, 2014 · 30 comments
Closed

Does support planned for type annotations? http://flowtype.org/ #3743

rstormsf opened this issue Nov 28, 2014 · 30 comments
Labels

Comments

@rstormsf
Copy link

Would like to know if there are any plans to support http://flowtype.org/ for CoffeeScript compiler

@jashkenas
Copy link
Owner

None.

@rstormsf
Copy link
Author

rstormsf commented Dec 1, 2014

Thanks

@srb-
Copy link

srb- commented Dec 2, 2014

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!

@vendethiel
Copy link
Collaborator

Out of curiosity, if Flow takes off, can someone comment on how difficult would it be to add its type annotations to 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)

@jashkenas
Copy link
Owner

Not terribly hard. It's been done before.

Check out typed-coffee-script, and Closure Mode for CoffeeScript. Have fun!

@rstormsf
Copy link
Author

thank you!

@taoeffect
Copy link

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?

@jashkenas
Copy link
Owner

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.

@taoeffect
Copy link

Thanks for the answer @jashkenas! 👍 Really appreciate it, and sorry I wasn't able to find wherever you shared it previously.

@molszanski
Copy link

@taoeffect, I think you can do it easily.
I didn't test it yet, but I think I will move some small stuff for fun to Flowtype.
Here is the way (flotate). facebook/flow#3

@ahamid
Copy link

ahamid commented Feb 4, 2015

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). Even a more concise inline (not block) comment syntax might suffice. flotate mentioned above might do the trick

@srb-
Copy link

srb- commented Feb 5, 2015

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...

@ahamid
Copy link

ahamid commented Feb 5, 2015

@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

@srb-
Copy link

srb- commented Feb 6, 2015

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:

### @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

@ahamid
Copy link

ahamid commented Feb 6, 2015

@srb- try b.calculate('i am a string')

@srb-
Copy link

srb- commented Feb 8, 2015

Sure. In that case it gives me an error:

flow/flow2.js:32:15,29: string
This type is incompatible with
  flow/flow2.js:23:14,18: number

Found 1 error

Don't forget to perform your CS to JS conversion before running flotate.

@ahamid
Copy link

ahamid commented Feb 8, 2015

@srb- indeed it does! apparently the CS header was disabling Flow checking, once I --no-header the errors occur. this is very good news.
Edit: still getting weird behavior when, e.g., defining multiple classes in a file, or in different positions in the file

What I have been able to determine is that the type check is failing only because Flow knows that x * x is a numeric expression (change to x + x and the type error should go away since + is string concat). It does not appear that flotate is correctly transpiling the JS comments - if I inspect the intermediate flotate output, the comments are intact and the source is unmodified.

@taoeffect
Copy link

@srb- or @ahamid or anyone else: some help with Flotate and CS (if you happen to know the answer): jareware/flotate#10

@mitar
Copy link
Contributor

mitar commented Mar 13, 2015

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:

...:6:1,3: Unexpected token /*:
### @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:

...:12:5,7: Unexpected token /*:

I am using:

coffee --version
CoffeeScript version 1.9.1
flow --version
Flow, a static type checker for JavaScript, version 0.5.0

What I am doing wrong?

Also, does float works with =>? So:

class Bob
   ###: (x: number): number ###
  calculate: (x) =>
    x * x

And types on constructor? What if you return from constructor something different?

@priezz
Copy link

priezz commented Mar 13, 2015

flotate supports

/*: (x: string, y: number): boolean */
function foo(x, y) {

but not

/*: (x: string, y: number): boolean */
foo = function(x, y) {

wondering as well how to manage this

@mitar
Copy link
Contributor

mitar commented Mar 13, 2015

@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.)

@bjmiller
Copy link

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 ### comment ###, try this:

`/* magic comment as a pass-through */`

I'm not sure if it will work, but I think it's worth a try.

@priezz
Copy link

priezz commented Mar 13, 2015

"Magic" comments still do not help. Yes, they move the comments inside blocks, but the major problem is that Coffee generates foo = function(x, y) and not function foo(x, y). I am interested how @srb- was able to succeed, what were versions of software pieces.

@srb-
Copy link

srb- commented Mar 13, 2015

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?

@carlsmith
Copy link
Contributor

Note that if you do...

`// comment`

...it'll add a semi-colon to your comment, which can break stuff like source and source map URIs. Use this instead...

`// comment
`

@ahamid
Copy link

ahamid commented Mar 14, 2015

@mitar the patch was on fe-assign branch in https://github.com/ahamid/flotate, i just checked and it appears to still be working with Flow 0.6.0. It looks like comment support was added to Flow and flotate is now defunct, but unfortunately Flow comments also don't support function expressions.

@eduveks
Copy link

eduveks commented Mar 30, 2015

Check this:

#3923

This works! Hope help you. ;)

@al6x
Copy link

al6x commented Dec 11, 2016

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.

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.

@GeoffreyBooth
Copy link
Collaborator

See also coffeescript6/discuss#12

@GeoffreyBooth
Copy link
Collaborator

Flow’s comments syntax is now supported, since #4572.

This was referenced Feb 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests