So what about better C?

Dennis dkorpel at gmail.com
Sun Jun 14 21:32:04 UTC 2020


On Sunday, 14 June 2020 at 19:50:47 UTC, Rel wrote:
> * Have you already used it? What for?

Yes.
- making my D libraries smaller in binary size
- running D code on a Nintendo 64 emulator
- WebAssembly (https://github.com/dkorpel/tictac)

> * Is there any known big projects in it?

The Digital Mars C/D compilers (dmd and dmc) where converted from 
C to D a few years ago with help of betterC. In terms of lines of 
code those are the biggest public projects that used betterC I 
think.

> * What D libraries can be used with it?

mir is a pretty notable one that has betterC compatibility, e.g.: 
https://github.com/libmir/mir-optim
bindbc bindings have betterC right in the name: 
https://github.com/BindBC
For more, see: https://code.dlang.org/search?q=betterC

> * What ways of coding is considered ideomatic in better C?

Writing new betterC code is not 'idiomatic'. The official use 
case is merely to convert C to D while sticking as close to the 
original source as possible, and maybe refactor it afterwards to 
idiomatic 'full D' if you like. In any case, you can do these 
things while keeping things betterC:
- change pointers + length to slices
- change memset, memcpy, strcmp to slice syntax
- replace for() with foreach()
- change pointer parameters to ref / out
- replace goto with scope guards or inner functions
- make code @safe and pure (you get nothrow and @nogc for free 
from C codebases)

> * Can better C be used without C's runtime (for example for OS 
> kernels or other bare metal projects)?

Yes, though the compiler might sometimes emit a call to certain C 
runtime functions such as `memcpy`, `memset`, `memcmp`, or 
`assert` for assertions with -betterC. If you get a link error, 
provide an implementation for it or remove the code that 
generates those calls.


More information about the Digitalmars-d mailing list