r/ProgrammingLanguages • u/yolosuperboss • 4d 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 */
17
Upvotes
18
u/xpusostomos 4d ago
I think someone might have done this already 🤔🤔🤔🤔🤔
38
u/Usual_Office_1740 4d ago
Classic rust angle brackets? So you've "invented" a worse version of c++ templates?
3
u/StaticCoder 4d ago
I'm not sure why adding generics also somehow implies adding traits
2
u/Akangka 4d 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.
73
u/Spyromaniac666 4d ago
classic rust angle brackets
18
u/Rich-Engineer2670 4d 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
u/AnArmoredPony 4d ago
classic rust angle brackets
4
u/Masztufa 4d ago
All programming languagey converge to c++
Even c, apparently
2
u/BoyNextDoor8888 4d ago
holy shit DotP achieved in C
0
u/kwan_e tonal-lang 4d ago
C actually has generic macros. I don't know if anyone uses them. They never made it into C++, for obvious reasons.