Open jarble opened 4 years ago
Generics aren't available yet, aside from the built-in collection types List<T>
, Dictionary<TKey, TValue>
and SortedDictionary<TKey, TValue>
.
Generics weren't available in the first releases of Java and C# either. They ended up being implemented differently.
Could you please describe your usecase for generics?
I'm planning to write a compiler that translates TypeScript to Ć, so the translation would be easier if generics were available in this language.
Since Ć targets several languages that already have generics, it might be a relatively easy feature to implement.
In TypeScript:
function implicitly_generic(a,b){
return a + b;
}
function another_example(a:number,b:number):number{
return Math.sqrt(implicitly_generic(a,b));
}
In Ć:
T1 implicitly_generic<T1,T2,T3>(T2 a, T3 b){
return a + b;
}
double another_example(double a,double b){
return Math.Sqrt(implicitly_generic<double,double,double>(a,b));
}
I'd like to have generics in Ć, of course!
However, your example translates easily to C++, but not C# or Java. It uses duck typing. C# or Java won't allow arithmetic on generic types.
BTW. do you think adding a TypeScript backend to cito
would be useful? What will be the advantages versus JavaScript?
@pfusik outputting TypeScript declarations (.d.ts
files) would be great for writing an in API Ć because the TypeScript defs would provide better Intellisense in IDEs than JavaScript. Anytime I'm using an API it's nice to have all the Intellisense I can get.
And for someone who wants to use the API in a TypeScript project, they would get better compile-time checks.
Outputting full TypeScript source (not just type declarations) wouldn't be as worthwhile though since then you would have to compile those to JS separately.
The implementation of generics is much more difficult than I expected: if the goal of this compiler is to target as many languages as possible, then this feature should probably be left out for now. A TypeScript backend should be relatively easy to implement, since it has many features in common with C# and Java.
@jarble yeah I assume there are some tricky differences between C++ templates and generics.
@jarble I'm working on TypeScript declarations output: #13
Back to the topic. I'd like to see real code examples where generics are useful. From my experience, it's mostly collections. And yes, some collections are not implemented yet, most notably Set
.
I've added Stack<T>
and HashSet<T>
.
Go introduces generics after 12 years.
Added OrderedDictionary<TKey, TValue>
and Queue<T>
.
Is it possible to define generic classes and functions in Ć, like in Java, C#, and C++?
If Ć doesn't have generics yet, then the syntax could be based on C#, like this: