Static typing?
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?
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.
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.
https://github.com/Steida/este uses Google Closure with CoffeeScript syntax, so static typing with compilation is possible.
Obviously, static typing cannot be done, because javascript is a dynamic language
Really? http://typescriptlang.org/
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!
is it completely discarded? it would be a great option if CS could compile to something like asm.js or JS with closure annotations.
There's been discussions about this already. We don't have more informations than JS JITs
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.
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.
Have you folks seen TypedCoffeeScript by @TobiaszCudnik?
(Maybe?) there's also the possibility of using Flow with CoffeeScript?
We are working using CoffeeScript v2.0 now. I propose to consider the possibility of implementing "Static Types" as language feature.
@denya133 are static types in consideration now or was it left as possible future feature?
@idchlife Sorry, I've been misunderstood. I'm not a contributor, I've made just a suggestion only.
https://coffeescript.org/#type-annotations
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.