5 ms·
For C with generic types, I think one should pick up Zig. It's exactly that, it's a low level language with manual memory management, but it allows you to do ge
by lukaslalinsky 2y ago
For C with generic types, I think one should pick up Zig. It's exactly that, it's a low level language with manual memory management, but it allows you to do generic types like this without hacks.
- randomNumber7 2y agoYou could use C++ and ignore the rest of it.
- huhtenberg 2y agoNot sure why the parent is in gray. It's a perfectly valid and widely practiced approach.
- codr7 2y agoHow often it works out as intended is very much up for debate though.
- jimbob45 2y agoFor legacy codebases, switching the compiler is certainly out of the question. For everyone else, why do you think it would be an issue to use the C++ compiler?
- codr7 2y agoBecause the complexity seeps in through the cracks, and with the language comes a mindset; same reasons Linus is wary about C++ in the kernel. Some say it works; I've never succeeded, even on solo projects.
- krapp 2y agoI think this is the general style for the Godot framework. They use a limited subset of C++, avoiding STL and some modern features, and limited templates.
- bfrog 2y agoI tried it once, ran out of memory debugging a template error as it blew up my terminal into a sea of jibberish.
- unclad5968 2y agoC3 and Odin are some other alternatives.
- jdougan 2y agoAlong with D
- lukaslalinsky 2y agoD sits in a weird place and that's probably why it's not widely adopted. If I'm using C, it's probably because I'm doing something low level and garbage collector could be problematic there. If you are fine with complexity, C++ or Rust are better options. If you want a simple language, I'd suggest Zig. On the other hand, if I don't want to deal with memory management, and still want fairly fast compiled language, I'd just go with Go. It has a much better ecosystem, because low level GC language is really mostly targeted at server components and Go excels there with implicit async everywhere.
- pebal 2y agoThe creators of the D made a big mistake. Instead of focusing on improving the GC, they focused on making it possible to avoid GC in D. The Go has shown that this strategy was wrong.
- alcover 2y agoI often see such replies and wonder : the article isn't about choosing langs but is an exercise in C. That's the constraint.
- bsder 2y agoIf C is the constraint, then why are you doing generic types? If you're using C, use bloody C. Not C with weird extensions that nobody will understand. Not C attempting to gussy up into a language with a real type system. Not C with a garbage collector. etc. I don't get why so many people use C but don't want C. This isn't 1995 where you have to shoehorn everything into the C ecosystem because everything else sucked.
- spacedcowboy 2y agoBut ... all of those things are still C. Maybe with extra libraries / steps / whatever, but still C.
- bsder 2y agoNo ... this isn't C. Will it have IDE support for the abstraction? Will a syntax mistake explode with a non-sensical error (yes, in this instance, because you are splicing macros and a syntax mistake will splice weird)? etc. If you want C with Generic Types, use C++. Rather than splicing unknown garbage onto C, use a subset of your C++ features and you will get full IDE support, proper template errors (okay, maybe not an improvement), proper elision and inlining, etc.
- spacedcowboy 2y ago> No ... this isn't C. My test for whether something or not is "still C" is very simple... "Does it compile with a C compiler" These compile. They're C. End of debate. Whether something has IDE support is neither here nor there. When I was learning C, your IDE was "vi" and "csh"... It was still C. As for errors, completely normal C code can spew out errors up the wazoo if you miss an errant ;, " or } (to pluck examples out of the air) somewhere important, things like "function not defined" because you were using a function later on in the file and it can no longer be parsed. Error quality does not define the language either.
- sirwhinesalot 2y agoZig is nice. Comptime is a really cool feature.