fusionlanguage / fut

Fusion programming language. Transpiling to C, C++, C#, D, Java, JavaScript, Python, Swift, TypeScript and OpenCL C.
https://fusion-lang.org
GNU General Public License v3.0
1.76k stars 55 forks source link

Generic functions #4

Open jarble opened 4 years ago

jarble commented 4 years ago

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
}
pfusik commented 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?

jarble commented 4 years ago

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));
}
pfusik commented 4 years ago

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?

jedwards1211 commented 4 years ago

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

jarble commented 4 years ago

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.

jedwards1211 commented 4 years ago

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

jedwards1211 commented 4 years ago

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

pfusik commented 3 years ago

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 commented 2 years ago

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

pfusik commented 2 years ago

Go introduces generics after 12 years.

pfusik commented 2 years ago

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