$(DDOC $(DDOC_BLANKLINE ) $(DDOC_BLANKLINE ) $(SPEC_S Better C, $(DDOC_BLANKLINE ) $(HEADERNAV_TOC $(HEADERNAV_ITEM linking, Linking) $(HEADERNAV_ITEM better-c, Better C) $(HEADERNAV_SUBITEMS retained, Retained Features, $(HEADERNAV_ITEM unittests, Running unittests in -betterC) ) $(HEADERNAV_ITEM consequences, Unavailable Features) ) $(DDOC_BLANKLINE )

$(LNAME2 linking, Linking)

$(DDOC_BLANKLINE ) $(P It is straightforward to link C functions and libraries into D programs. But linking D functions and libraries into C programs is not straightforward. ) $(DDOC_BLANKLINE ) $(P D programs generally require:) $(DDOC_BLANKLINE ) $(OL $(LI The D runtime library to be linked in, because many features of the core language require runtime library support.) $(LI The main() function to be written in D, to ensure that the required runtime library support is properly initialized.) ) $(DDOC_BLANKLINE ) $(P To link D functions and libraries into C programs, it's necessary to only require the C runtime library to be linked in. This is accomplished by defining a subset of D that fits this requirement, called $(B BetterC). ) $(DDOC_BLANKLINE )

$(LNAME2 better-c, Better C)

$(DDOC_BLANKLINE ) $(IMPLEMENTATION_DEFINED $(B BetterC) is typically enabled by setting the $(TT -betterC) command line flag for the implementation. ) $(DDOC_BLANKLINE ) $(P When $(B BetterC) is enabled, the predefined $(DDLINK spec/version, Conditional Compilation, version) D_BetterC can be used for conditional compilation. ) $(DDOC_BLANKLINE ) $(DDOC_BLANKLINE ) $(P An entire program can be written in $(B BetterC) by supplying a C main() function:) $(DDOC_BLANKLINE ) $(D_CODE $(D_KEYWORD extern)(C) $(D_KEYWORD void) main() { $(D_KEYWORD import) core.stdc.stdio : printf; printf($(D_STRING "Hello betterC\n")); } ) $(DDOC_BLANKLINE ) $(CONSOLE $(GT ) dmd -betterC hello.d && ./hello Hello betterC ) $(DDOC_BLANKLINE ) $(P Limiting a program to this subset of runtime features is useful when targeting constrained environments where the use of such features is not practical or possible. ) $(DDOC_BLANKLINE ) $(P $(B BetterC) makes embedding D libraries in existing larger projects easier by: ) $(DDOC_BLANKLINE ) $(OL $(LI Simplifying the process of integration at the build-system level) $(LI Removing the need to ensure that Druntime is properly initialized on calls to the library, for situations when an initialization step is not performed or would be difficult to insert before the library is used.) $(LI Mixing memory management strategies (GC + manual memory management) can be tricky, hence removing D's GC from the equation may be worthwhile sometimes.) ) $(DDOC_BLANKLINE ) $(NOTE BetterC and $(DDLINK spec/importc, ImportC, ImportC) are very different. ImportC is an actual C compiler. BetterC is a subset of D that relies only on the existence of the C Standard library.) $(DDOC_BLANKLINE ) $(DDOC_BLANKLINE )

$(LNAME2 retained, Retained Features)

$(DDOC_BLANKLINE ) $(P Nearly the full language remains available. Highlights include:) $(DDOC_BLANKLINE ) $(OL $(LI Unrestricted use of compile-time features) $(LI Full metaprogramming facilities) $(LI Nested functions, nested structs, delegates and $(DDSUBLINK spec/expression, function_literals, lambdas)) $(LI Member functions, constructors, destructors, operating overloading, etc.) $(LI The full module system) $(LI Array slicing, and array bounds checking) $(LI RAII (yes, it can work without exceptions)) $(LI scope(exit)) $(LI Memory safety protections) $(LI $(DDLINK spec/cpp_interface, Interfacing to C++, Interfacing to C++)) $(LI COM classes and C++ classes) $(LI assert failures are directed to the C runtime library) $(LI switch with strings) $(LI final switch) $(LI unittest) $(LI $(DDSUBLINK spec/interfaceToC, calling_printf, printf format validation)) ) $(DDOC_BLANKLINE )

$(LNAME2 unittests, Running unittests in -betterC)

$(DDOC_BLANKLINE ) While testing can be done without the $(TT -betterC) flag, it is sometimes desirable to run the testsuite in -betterC too. unittest blocks can be listed with the $(DDSUBLINK spec/traits, getUnitTests, getUnitTests) trait: $(DDOC_BLANKLINE ) $(D_CODE $(D_KEYWORD unittest) { $(D_KEYWORD assert)(0); } $(D_KEYWORD extern)(C) $(D_KEYWORD void) main() { $(D_KEYWORD static) $(D_KEYWORD foreach)(u; $(D_KEYWORD __traits)(getUnitTests, $(D_KEYWORD __traits)(parent, main))) u(); } ) $(DDOC_BLANKLINE ) $(CONSOLE $(GT ) dmd -betterC -unittest -run test.d dmd_runpezoXK: foo.d:3: Assertion `0' failed. ) $(DDOC_BLANKLINE ) However, in -betterC, assert expressions don't use Druntime's assert and are directed to assert from the C runtime library instead. $(DDOC_BLANKLINE )

$(LNAME2 consequences, Unavailable Features)

$(DDOC_BLANKLINE ) $(P D features not available with $(B BetterC):) $(DDOC_BLANKLINE ) $(OL $(LI Garbage Collection) $(LI TypeInfo and $(DDSUBLINK spec/abi, ModuleInfo, $(TT ModuleInfo))) $(LI Classes) $(LI Built-in threading (e.g. $(MREF core, thread))) $(LI Dynamic arrays (though slices of static arrays and pointers work)) $(LI Associative arrays) $(LI Exceptions) $(LI synchronized and $(MREF core, sync)) $(LI Static module constructors or destructors) ) $(DDOC_BLANKLINE ) $(DDOC_BLANKLINE ) $(SPEC_SUBNAV_PREV_NEXT simd, Vector Extensions, importc, ImportC) $(DDOC_BLANKLINE ) ) )