Live data from Hacker News

Show HN: I implemented generics in my programming language

axe-docs.pages.dev

1–10 of 22 posts

Re: Show HN: I implemented generics in my programming language

#2
This are merely instanceof switches. Generics mean that you write

  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 vectors

Re: Show HN: I implemented generics in my programming language

#3
You implemented Specifics.

One 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

#6
If your function changes it's behaviour based on the type, then it's not generic.

Your 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

#7
I saw your programming language on reddit and now here too. To me first looking at it, I think its built straight for parallelism, Didn't know you were on hackernews too, interesting stuff and I will try to keep an eye out hopefully for this language but what are some stuff where you think it can be really useful for?

Re: Show HN: I implemented generics in my programming language

#8

You 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 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

#9
If I understand correctly, generics here are “type agnostic” functions in a strongly typed language?

Why 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

#10
post #8

You 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…

Right, but C-style generics really are not generics at all. As other comments point out, they effectively are conpile-time if statements. A generic function would automatically instantiate and generate required code for different call types, and perform the correct operations. The C-style "generics" are a conpile-time switch statement, switching on types.
Post reply on HN