Show HN: I implemented generics in my programming language
axe-docs.pages.dev
Show HN: I implemented generics in my programming language
1–10 of 22 posts
Re: Show HN: I implemented generics in my programming language
#2 fun(a,b): return a+b
and if the a + b is doable, it will be done. You don't need to specify the list of types that accepts this syntax. The difference between this and duck typing is that you can also specify interfaces (or traits in c++) that will say that this type is quackable so fun(a ): a.quack()
is reusable. What is the difference between this and simple interface implementation? It took me some time to find this example in the narrowest version possible. class Complex(a T, b T):
Complex operator+(Complex other): return new Complex(this.a + other.a, this.b+other.b)
Complex operator-(Complex other): return new Complex(this.a - other.a, this.b-other.b)
Complex operator*(Complex other):
return Complex(this.a * other.a - this.b * other.b, this.a * other.b + this.b * other.a)
The generic renders this code reusable, so if only you can create a new type, let's say vector, that supports +,-, and multiply you can have complex algebra on those vectorsRe: Show HN: I implemented generics in my programming language
#3One of my pet-hates is fellow developers who call an implementation 'generic', but when you peek inside, there's just if-statements that (at best) cover the already-known input types.
Usually I point to Generics as an example of what "generic" actually means: You peek inside List, it doesn't know about your type, it does the right thing anyway.
Re: Show HN: I implemented generics in my programming language
#4This is different from ‘parametric polymorphism’, which is what people call generics.
Re: Show HN: I implemented generics in my programming language
#5Re: Show HN: I implemented generics in my programming language
#6Your list_contains function should be able to just do a == comparison regardless of whether it's an int or a string.
This is effectively no different than adding a parameter to one of your non-"generic" functions and just swapping behaviour based on that?
Re: Show HN: I implemented generics in my programming language
#7Re: Show HN: I implemented generics in my programming language
#8You have implemented a form of ‘ad-hoc polymorphism’. This is different from ‘parametric polymorphism’, which is what people call generics.
Given that, this isn’t that different from C generics (https://en.cppreference.com/w/c/language/generic.html), and people call that generics, too.
Having said that, even ignoring that this requires all implementations to be in a single source file (yes, you probably could use m4 or #include or whatever it’s called in this language) I do not find this syntax elegant.
Also, one thing that it doesn’t seem to support is generating compiler errors when calling a function with a type that isn’t supported.
Re: Show HN: I implemented generics in my programming language
#9Why not just use a weakly typed language and add type checking were needed?
It seems strange to put in so much effort for type checking then only to throw it overboard by implementing something that ignores type.
Re: Show HN: I implemented generics in my programming language
#10You have implemented a form of ‘ad-hoc polymorphism’. This is different from ‘parametric polymorphism’, which is what people call generics.
I somewhat disagree. FTA: “If the callsite is some_function(2);, the compiler resolves T as i32 and selects the corresponding branch, returning the value incremented by one. […] The important point is that the decision is driven entirely by type information, not by runtime inspection” Given that, this isn’t that different from C generics ( https://en.cppreference.com/w/c/language/generic.html ), and people call that…