3 ms·
Are there any features you would like in C that would make things easier? I'm writing a c compiler and adding low hanging stuff such as removing forward declara
by throwaway88j 5y ago
Are there any features you would like in C that would make things easier? I'm writing a c compiler and adding low hanging stuff such as removing forward declarations and supporting multiple returns. One thing I'm flip flopping on is function overloading for example. I'd appreciate your opinion.
- TravHatesMe 5y agoNot OP but based on his comment about checking return values of stuff.. would be nice to have lightweight exceptions. could improve readability and debuggability, less if statements, generally easier to propagate errors up the call stack. my 2cents.
- visualradio 5y agoBefore substantially modifying the language, make sure your parser can still load vanilla C header files with no new features. This way if you accidentally create a new language, programmers will still be able to load existing C header files without having to manually write and maintain FFI bindings.
- tomcam 5y agoAdding multiple return values creates a new language
- visualradio 5y agoThe more relevant issue is whether or not it preserves backwards compatibility. It is possible for their compiler to continue to consume existing ANSI C and C99 header files and source code, without requiring the programmer to manually write FFI bindings and wrappers to call regular C functions as is the case in many other high-level languages, and also without adding as many features as C++.
- epermyakov 5y agoI guess the "shtick" of C is that it has a small and obvious feature set. The readability and style of programming follows from that. As you start adding more and more language features and constructs, you start getting all the other languages that were derived from C and you no longer have C. During the development of the project, I had a thought that it would be nice to have a RAII/defer mechanism to get rid of repetitive code for freeing resources at the end of a function. But I'm not sure if that's really necessary since you can just put the 'free' calls at the end of the function and insert some labels between them in a kind of 'stack'. This perhaps is more in the spirit of the language - a bit more wordy, but having less voodoo done by the compiler.
- throwaway88j 5y agoInteresting, I think defer as a statement might not be too much magic but it would be too complicated for my mostly single-pass approach. Thank you for your response.
- jstimpfle 5y agoSome people like to make a defer macro like this: #define itervar(i) i##__LINE__ #define FOR_DEFER(pre, post) for (int itervar(i) = ((pre), 0); !itervar(i)++; (post)) Foo *foos; FOR_DEFER(foos = alloc(Foo, 1024), free(foos)) { // This "loop" only runs once. } Those can be easily stacked FOR_DEFER(foos = alloc(Foo, 1024), free(foos)) FOR_DEFER(bars = alloc(Bar, 1024), free(bars)) { /// } Here is a memory arena macro. #define FOR_MEMORY_ARENA(name) for DEFER(Arena name = make_arena(), free_arena(&arena)) FOR_MEMORY_ARENA(arena) { Foo *foos = arena_alloc(&arena, Foo, 1024); Bar *bars = arena_alloc(&arena, Bar, 1024); // all allocations automatically freed at the end }
- jnwatson 5y agogcc and llvm have a cleanup attribute you can use for this. Systemd uses it. https://fdiv.net/2015/10/08/emulating-defer-c-clang-or-gccblocks https://fdiv.net/2015/10/08/emulating-defer-c-clang-or-gccbl...
- deleted 5y ago[deleted]