coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Static typing?

Open ghost opened this issue 15 years ago • 16 comments

Would it be a good idea to introduce static typing in CS?

Eg.

string greet = (Coder me, number|string age, string name) -> "Hi. I am a #{me.getTitle()}. My name is #{name} and I am #{age} years old"

This could solve some issues we have with JS:

  • Provide a way for IDEs to autocomplete. So when I type "me." it will give me all the instance methods for Coder. This is impossible atm.
  • Good for documentation and clean code.
  • No need for manual checking code.
  • Less bugs.

Thoughts?

UPDATE:

Found this one that seems to be very popular: http://disnetdev.com/contracts.coffee/

Any thoughts on implementing it into the core of CS?

ghost avatar Nov 10 '11 15:11 ghost

Obviously, static typing cannot be done, because javascript is a dynamic language. Contracts.coffee is an implementation of design-by-contract and its predicates are checked in runtime.

There were some issues in coffee-script bugtracker and they was closed (#52, #116, #1833).

1 year-after edit: sorry folks, I knew a little about typing at this point. Yes it’s possible to make typed coffee.

paulmillr avatar Nov 10 '11 18:11 paulmillr

What Paul said. Type-checking is way beyond the intended scope of CoffeeScript. If you want a statically-typed language that compiles to JS, look at Contracts.coffee or Google Dart.

TrevorBurnham avatar Nov 10 '11 19:11 TrevorBurnham

https://github.com/Steida/este uses Google Closure with CoffeeScript syntax, so static typing with compilation is possible.

steida avatar Sep 13 '12 22:09 steida

Obviously, static typing cannot be done, because javascript is a dynamic language

Really? http://typescriptlang.org/

applejohnny avatar Oct 24 '12 05:10 applejohnny

Later versions of ECMAScript have optional typing using the i:int format, ActionScript and UnityScript are two derivations that use it. However, CoffeeScript is a pre-processor, not a Javascript compiler! So while it could accept the typing syntax that later ECMAScript versions support, it wouldn't in any way enforce this static typing. It validates its own syntax and converts it to JavaScript, it doesn't prevent you from writing bad code!

Soviut avatar Oct 24 '12 07:10 Soviut

is it completely discarded? it would be a great option if CS could compile to something like asm.js or JS with closure annotations.

olanod avatar Jul 15 '13 17:07 olanod

There's been discussions about this already. We don't have more informations than JS JITs

vendethiel avatar Jul 15 '13 21:07 vendethiel

If Michael Ficarra ever finish CoffeeScript Redux, I will use it for EsteScript. CoffeeScript with type annotations for Google Closure Compiler. But annotations based static typing is good enough for now.

steida avatar Jul 15 '13 23:07 steida

Ive did a workaround for this particular usecase, reusing typescript's static typing via external d.ts files, which are merged into the source during compilation.

Its called CompiledCoffee. Enjoy.

TobiaszCudnik avatar Feb 12 '14 21:02 TobiaszCudnik

Have you folks seen TypedCoffeeScript by @TobiaszCudnik?

taoeffect avatar Dec 27 '14 22:12 taoeffect

(Maybe?) there's also the possibility of using Flow with CoffeeScript?

taoeffect avatar Dec 27 '14 23:12 taoeffect

We are working using CoffeeScript v2.0 now. I propose to consider the possibility of implementing "Static Types" as language feature.

denya133 avatar Jun 14 '17 11:06 denya133

@denya133 are static types in consideration now or was it left as possible future feature?

idchlife avatar Sep 28 '18 14:09 idchlife

@idchlife Sorry, I've been misunderstood. I'm not a contributor, I've made just a suggestion only.

denya133 avatar Sep 28 '18 14:09 denya133

https://coffeescript.org/#type-annotations

GeoffreyBooth avatar Sep 28 '18 14:09 GeoffreyBooth

For those interested I have created Floweret, a flow-inspired dynamic type checking library specially for CoffeeScript.

The type annotations example

# @flow

###::
type Obj = {
  num: number,
};
###

f = (str ###: string ###, obj ###: Obj ###) ###: string ### ->
  str + obj.num

can be rewritten using a decorator-like syntax:

import { fn } from 'floweret'

Obj =
  num: Number

f = fn String, Obj, String,
  (str, obj) ->
    str + obj.num

It is not static typing, types are checked at runtime. But it is nice to trust your functions inputs/outputs in production and there is no humanly-detectable performance impact for my web app.

laurentpayot avatar Oct 15 '18 23:10 laurentpayot