Working on TypeScript 0.9: Generics, Overload on Constants and Performance
blogs.msdn.com
blogs.msdn.com
Javascript, on the other hand, is duck-typed. Everything is the equivalent of generic already. Adding generics to the Javascript wrapper feels awkward and overcomplex and somehow suggests that a point has been missed. Perhaps you feel differently?
If you want strongly typed language support (and I do), then generics make it vastly easier to write maintainable code.
A generic doesn't mean "anything goes" - it means "You will definitely get the thing that you specified".
You don't actually care that it's a Dog BTW, just that it has a method called Bark(), if that's what you call on it.
You make sure that your code only puts dog-like objects into the list, either by writing tests or by reading the code and praying. But the system of putting objects into lists is really, really simple.
I see the benefit of strong typing and I use it all the time. Generics are an epicycle that IMHO sits poorly with JavaScript.
This is exactly where TypeScript helps.
With the way TypeScript interfaces work, you can define an interface
interface Barker {
Bark();
}
And have an array of Barkers where each object's class doesn't have to explicitly say that it implements Barker. Ex. class Dog {
Bark() {console.log('woof');}
}
class Cat {
Meow() {console.log('meow');}
}
var barkers: Barker[]; // array of Barkers
barkers.push(new Dog()); // compiles
barkers.push(new Cat()); // won't compile
Here Dog doesn't have to explicitly declare that it implements Barker. The TypeScript compiler automatically verifies that it does by the fact that it has a Bark() method.In TypeScript, Arrays already support this level of type checking. With generics, the static type checking will be able to cover a lot more code so that you don't have to depend on writing tests.
I may as well say "Interfaces are an epicycle that IMHO sits poorly with JavaScript."
There are good things about strong typing, but it's astonishing how many language constructs can be thrown away if you don't have it.
It would look something like this:
class DataTable<T> { Insert(element: T) : void { //insert code }
Get(row: number) : T {
//get code
}
//more code
}Then I could subclass that like:
class ProductDataTable extends DataTable<Product> { //Done... No need to rewrite everything }
Or I could just instantiate the base class in an ad hoc fashion:
var productTable = new DataTable<Product>();
In summary, generics will help immensely with common coding tasks.
that is a truism, so I assume you mean "needless complexity".
You can do without generics, as pre-generics java did. The problem is you end up with a lot of type casts (e.g. cause you can't get something out of a container method that isn't typed "Object"), more run time errors, worse tool support.
Generics aren't that hard or verbose for 90% of the cases, so it's a trade off worth making, imvho.
It's useful, needed complexity, if your language is strongly typed to start off with. Which JavaScript is not.
Of course all complexity comes with cost, and the costs multiply, (a new feature of the type system interacts with all the existing features) even if the complexity is needed it becomes harder to use and understand the more features are added. JavaScript does away with all of that. It has loads of other flaws, but the basic design philosophy is not one that suggests the addition of generics to improve the language.
Otherwise you have two options: define separate methods for mapArrayOfNumbers, mapArrayOfStrings, etc. or have a lot of casts to the ground type and write a lot of code that isn't actually statically typed.