fut icon indicating copy to clipboard operation
fut copied to clipboard

Generic functions

Open jarble opened this issue 3 years ago • 11 comments

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:

public class GenericClass<T>
{
    //source code goes here
}

jarble avatar Sep 21 '20 19:09 jarble

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?

pfusik avatar Sep 22 '20 06:09 pfusik

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));
}

jarble avatar Sep 22 '20 14:09 jarble

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 avatar Sep 22 '20 18:09 pfusik

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

jedwards1211 avatar Sep 23 '20 02:09 jedwards1211

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 avatar Sep 25 '20 15:09 jarble

@jarble yeah I assume there are some tricky differences between C++ templates and generics.

jedwards1211 avatar Sep 25 '20 16:09 jedwards1211

@jarble I'm working on TypeScript declarations output: #13

jedwards1211 avatar Oct 06 '20 05:10 jedwards1211

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.

pfusik avatar Sep 18 '21 06:09 pfusik

I've added Stack<T> and HashSet<T>.

pfusik avatar Jan 04 '22 12:01 pfusik

Go introduces generics after 12 years.

pfusik avatar Feb 06 '22 18:02 pfusik

Added OrderedDictionary<TKey, TValue> and Queue<T>.

pfusik avatar Aug 28 '22 11:08 pfusik