7 ms·
But if that's the case couldn't there just be a library that provides all the functionality without having to switch languages? Something like boost but for pla
by ziftface 5y ago
But if that's the case couldn't there just be a library that provides all the functionality without having to switch languages? Something like boost but for plain C?
- whaaswijk 5y agoIn theory there could be such a library but in practice I don’t think there is. Maybe because maintaining something like the STL is a non-trivial task.
- mastrsushi 5y agoThat’s pretty much what glib is. There are generic structures.
- lalaland1125 5y agoThe lack of generics for C makes it difficult to write those sorts of libraries in an efficient way.
- ncmncm 5y agoAnd, generally impossible to write C generics so they may confidently be used safely. There are reasons why C++ was necessary.
- klyrs 5y agoC has had generics since C11. Can't fault you for not knowing that, as they're a pain to use and I think folks would rather forget that the language feature exists.
- edflsafoiewq 5y ago_Generic is basically a switch on typeof(x). Despite the name, it's really a specialization/overloading mechanism.
- junon 5y agoThose aren't generics, they're more like overloading.
- rwmj 5y agoI wrote this (slightly crazy) generic vector for C: https://gitlab.com/nbdkit/nbdkit/-/blob/master/common/utils/vector.h https://gitlab.com/nbdkit/nbdkit/-/blob/master/common/utils/... If you search for "string" in this page you can see how it is used: https://gitlab.com/nbdkit/nbdkit/-/blob/master/plugins/data/format.c https://gitlab.com/nbdkit/nbdkit/-/blob/master/plugins/data/...
- tekknik 5y agoYou’d use a code generator for this, similar to how you’d do it in golang.
- LAC-Tech 5y agoI wonder if Dlangs "betterC" would be a good fit, since you get real metaprogramming then.
- elcritch 5y agoDlang's betterC would be a good option for just a better-than-C language but looks like it doesn't support D's standard library [1]. That'd mean being stuck with plain C container libraries. :/ Personally, I was impressed by an approach taken by the authors of Pixie (a fast 2d library like Cairo) [2]. They created a "library wrapper" generator for Nim code that supports creating libraries Python, Node, C, and Nim itself that they call Genny [3]. Haven't tried it but being able to use Nim and it's ref based gc but still export to nice API's in other languages is fantastic. Pixie's aim is to be a Cairo alternative so it makes sense they'd need this. I hope the approach takes off. Usually writing any cross language API's is a lossy operation. Here's a sample of the API definition: exportObject Matrix3: constructor: matrix3 procs: mul(Matrix3, Matrix3) 1: https://dlang.org/spec/betterc.html https://dlang.org/spec/betterc.html 2: https://github.com/treeform/pixie https://github.com/treeform/pixie 3: https://github.com/treeform/genny https://github.com/treeform/genny (edited formatting)
- david2ndaccount 5y agoThe consequences of betterC are listed out here. https://dlang.org/spec/betterc.html#consequences https://dlang.org/spec/betterc.html#consequences Notice that betterC does not exclude templates or destructors, so you could still use c++-style containers based around RAII, etc. The argument against it is a smaller ecosystem and you’d end up having to roll your own containers etc.
- ksec 5y ago>I wonder if Dlangs "betterC" would be a good fit, You mean "Das C" ? :)
- guerrilla 5y agoYes, Gtk+ and Glib do that with the catch that C has no type polymorphism, so everything is done with casts and void pointers. It's even fully object-oriented.
- qalmakka 5y agoThe Glib approach is an immense monolith of nonsensical zealotry towards C. Reimplementing an Object-Oriented system in C using hacks and void pointers when GCC already had not one but two fully object oriented languages built-in was a completely pointless endeavor. There are a million different ways to shoot youself in the foot with Glib, and it definitely doesn't feel like writing C at all. I understand the historical context that lead to the creation of Glib (no C++ standard in the mid-'90s, no decent open Objective-C libraries, Stallman and most of the FOSS movement at the time harbouring a deep disgust towards C++ for some reason, etc), but it can't be pointed at as a good example of how to code in C - on the contrary, it's IMHO exactly what your C project shouldn't do. You've picked C because it's simple and clear, don't butcher it up with macros and weird hacks just to get a worse Objective-C.
- guerrilla 5y ago> understand the historical context that lead to the creation of Glib Well, yeah, that was 23 years ago. Back then there was no GNUstep and no free as in freedom C++ or Objective-C compiler. Now GCC's main development language is C++ and GNOME is slowly becoming somewhat language agnostic and also has Vala which at one point was supposed to cover up that mess. I guess another point is that they seem happy with it[1], so why should we care anyway. 1. http://planet.gnome.org http://planet.gnome.org
- zucker42 5y agoC libraries can't be simultaneously as generic and as fast as equivalent C++ libraries without (or sometimes even with) the aforementioned "clunky macro hacks".
- lionkor 5y agoif "fast" was the only requirement we would be writing it in assembly
- rbanffy 5y agoIf fast were the only requirement, we’d have vastly different processor and memory architectures and we’d probably write in higher level languages with compilers that would also generate the optimal ISA to be compiled into architecture-specific microcode along with the binary that runs on it with enough information the OS can pick which part of the heterogeneous machine is best suited to run the program.
- tekknik 5y agoNo OS, it’s too slow. The binary needs to bootstrap the machine as well.
- tempodox 5y agoNope, folks didn't invent higher-level programming languages (the first of which was arguably Fortran, see case in point [1]) because they wanted their code to run slower. In fact, if “fast” is the most important requirement we'll probably write it in Fortran or C++. With C++ you just have to know which parts not to use. [1] Fortran Web Framework https://news.ycombinator.com/item?id=28509333 https://news.ycombinator.com/item?id=28509333
- zucker42 5y ago"Fast" is never the only requirement but often is a requirement. And the problem with a slow library is you don't know when rewriting the program to not use the library will be necessary to achieve the desired performance.
- ncmncm 5y ago"Something like boost but for plain C" is, exactly, Standard C++.