r/ProgrammingLanguages • u/yolosuperboss • 11d ago
Discussion Adding actual generics to c
I was watching tsoding's crust stream and I thought it would be so funny if I extended c to have generics.
The way I did this was by extending the syntax of c with the classic rust angle brackets to define generics and then during transpilation do some name mangling to monomorphize the generic code.
Here is the result of that abomination:
#include <stdio.h>
// you can define your own operator in fortran .op. style with a trait
trait<T> DotP {
// for parsing reasons any dot-op must have whitespaces around it in any context
T .dot. (Self self, Self other);
}
// using a very similar syntax to rust, you can add generics to structs
typedef<T> struct {
T x;
T y;
T z;
} Vector<T>;
// and by appling the trait to your struct (with generic parameters)
// you can define what your operator should do.
impl<T> DotP<T> for Vector<T> {
T .dot. (Self self, Self other){
return self.x * other.x + self.y * other.y + self.z * other.z;
}
}
int main(void) {
// by concretly defining which type you want to use as the generic
// a c struct with that/those type(s) is genenerated
Vector<float> fv = {1.0, 2.0, 3.0};
printf("Dot product of fv^2: %f\n", fv .dot. fv);
Vector<int> iv = {1, 2, 3};
printf("Dot product of iv^2: %d\n", iv .dot. iv);
}The generated c code looks something like this (I have actually implemented this transpiler, perhaps a true indicator of joblessness):
/* Generated by typedc. */
#ifndef TYPEDC_SIMPLE_GENERICS_H
#define TYPEDC_SIMPLE_GENERICS_H
#include <stdio.h>
typedef struct float_Vector float_Vector;
typedef struct int_Vector int_Vector;
struct float_Vector {
float x;
float y;
float z;
};
struct int_Vector {
int x;
int y;
int z;
};
int main(void);
static inline float float_dot(float_Vector self, float_Vector other);
static inline int int_dot(int_Vector self, int_Vector other);
int main(void) {
float_Vector fv = {1.0, 2.0, 3.0};
printf("Dot product of fv^2: %f\n", float_dot(fv, fv));
int_Vector iv = {1, 2, 3};
printf("Dot product of iv^2: %d\n", int_dot(iv, iv));
}
static inline float float_dot(float_Vector self, float_Vector other) {
return self.x * other.x + self.y * other.y + self.z * other.z;
}
static inline int int_dot(int_Vector self, int_Vector other) {
return self.x * other.x + self.y * other.y + self.z * other.z;
}
#endif /* TYPEDC_SIMPLE_GENERICS_H */38
u/Usual_Office_1740 11d ago
Classic rust angle brackets? So you've "invented" a worse version of c++ templates?
-9
u/yolosuperboss 11d ago
No, c++ templates are horrible to work with
4
u/Usual_Office_1740 11d ago
You should try them with C++23. Concepts are great and templates aren't difficult once you understand what the compiler errors are about.
1
u/yolosuperboss 11d ago
Yeah, I should really give c++23 a go, fair enough
3
u/Usual_Office_1740 11d ago
Requires really does turn a set of template parameters into something that feels like writing a compile time function. The errors are easier to manage. Inside requires clauses you can write expressions and do things like check that a T has a given member function or can be used with a specific operator. All of that is possible with SFINAE but it gets cumbersome and ugly quickly.
3
u/StaticCoder 11d ago
I'm not sure why adding generics also somehow implies adding traits
1
u/yolosuperboss 11d ago
I wanted to do a bit of operator overloading too (to demo things like dot products) and it seemed more natural to put those things in traits rather than some special new concept.
2
3
u/Akangka 11d ago
How do you call a generic function on a dynamically linked library? I know Rust doesn't actually link the function dynamically for that function. They instead just stored the AST.
The only way I can see is by restricting the generic value behind a pointer, to go the JIT route, or secretly link statically.
2
u/yolosuperboss 11d ago
I don't really have an ABI and the code which you compile is already in c (after transpilation) so there aren't really any calls on generic functions other than the name mangled functions.
74
u/Spyromaniac666 11d ago
classic rust angle brackets
43
u/phischu Effekt 11d ago
This goes onto my list. Another one I liked was "rust-style implicit capture" for closures.
33
u/WittyStick 11d ago
Don't forget the rust style Result an Option types.
1
u/binarycow 11d ago
Is there an inside joke I'm missing?
16
u/Inconstant_Moo 🧿 Pipefish 11d ago
Angle brackets for generics come from C++, which is older than Rust by some decades.
5
6
1
14
7
5
2
19
u/Rich-Engineer2670 11d ago
Ah.... rebirth of CFront! Most people forget C++ started as a transpiler to C. In some ways, if your idea was taken to its endpoint, we could put a lot of C++ into C and even some of the rust memory checking via the compiler.
20
4
2
0
u/kwan_e tonal-lang 11d ago
C actually has generic macros. I don't know if anyone uses them. They never made it into C++, for obvious reasons.
7
u/Snarwin 11d ago
C's
_Genericis ad-hoc polymorphism (overloading), not parametric polymorphism.1
u/kwan_e tonal-lang 11d ago
Knowing C, there is a good chance that someone somewhere has experimented with it and paired with with preprocessor metaprogramming to create "parametric" polymorphism.
1
u/Snarwin 11d ago
People have definitely created templates-at-home using the preprocessor (e.g., this generic "span" data structure by a member of the C committee), but as far as I know, nobody has ever found a reason to use
_Genericin these attempts.1
u/WittyStick 10d ago
I have seen a more crazy attempt to use
_Genericfor extensible generics.It's a nice trick, but still very limited.
_Genericis just really unsuitable for open generics.1
u/jevndev 10d ago
I have at least used `_Generic` once in an attempt to add ergonomic discriminatory sum types to c They were a great fit for dispatching to the types generated by the library.
1
u/WittyStick 10d ago
_Genericisn't a macro, it's a kind of expression - they're typically used in macros, but don't have to be.The obvious downside to them being expressions is they can't appear in type declarations - which really limits their use for implementing generic types.
18
u/xpusostomos 11d ago
I think someone might have done this already 🤔🤔🤔🤔🤔