in ,

Better C – A subset of D Programming Language, Hacker News

                      

It is straightforward to link C functions and libraries into D programs.     But linking D functions and libraries into C programs is not straightforward.     

    

D programs generally require:

    

  1. The D runtime library to be linked in, because many features of     the core language require runtime library support.
  2.     

  3. The main () function to be written in D, to ensure that the required     runtime library support is properly initialized.
  4.     

    

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 BetterC .     

    

Implementation Defined : BetterC is typically enabled by setting the – betterC     command line flag for the implementation.     

    

When BetterC is enabled, the predefined version D_BetterC     can be used for conditional compilation.     

    

An entire program can be written in BetterC by supplying a C main () function:

  extern  (C)  void  main ()  {       import  core.stdc.stdio: printf;      printf ( "Hello betterC  n" );  }  
    
> dmd -betterC hello.d && ./hello  Hello betterC  
        

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.     

    

BetterC makes embedding D libraries in existing larger projects easier by:     

    
  1. Simplifying the process of integration at the build-system level
  2.     
  3. 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.
  4.     
  5. Mixing memory management strategies (GC manual memory management) can     be tricky, hence removing D's GC from the equation may be worthwhile sometimes.
  6.     
 

Retained Features

    

Nearly the full language remains available. Highlights include:

    
  1. Unrestricted use of compile-time features
  2.     
  3. Full metaprogramming facilities
  4.     
  5. Nested functions, nested structs, delegates and lambdas
  6.     
  7. Member functions, constructors, destructors, operating overloading, etc.
  8.     
  9. The full module system
  10.     
  11. Array slicing, and array bounds checking
  12.     
  13. RAII (yes, it can work without exceptions)
  14.     
  15. scope (exit)
  16.     
  17. Memory safety protections
  18.     
  19. Interfacing with C
  20.     
  21. COM classes and C classes
  22.     
  23. assert failures are directed to the C runtime library
  24.     
  25. switch with strings
  26.     
  27. final switch
  28.     
  29. unittest
  30.     
  31. printf format validation
  32.     

Running unittests in - betterC

While, testing can be done without the - betterC flag, it is sometimes desirable to run the testsuite in - betterC too. unittest blocks can be listed with the

getUnitTests trait:
  unittest   {      assert  (0);  }     extern  (C)  void  main ()  {       static   foreach  (u;  __ traits  (getUnitTests,  __ traits  (parent, main)))          u ();  }  
    
> dmd -betterC -unittest -run test.d  dmd_runpezoXK: foo.d: 3: Assertion `0 'failed.  
  

However, in - betterC assert expressions don't use Druntime's assert and are directed to assert of the C runtime library instead.

Unavailable Features     

D features not available with BetterC :

  1. Garbage Collection
  2.     
  3. TypeInfo and ModuleInfo
  4.     
  5. Classes
  6.     
  7. Built-in threading (eg core.thread )
  8.     
  9. Dynamic arrays (though slices of static arrays work) and associative arrays
  10.     
  11. Exceptions
  12.     
  13. synchronized and core.sync
  14.     
  15. Static module constructors or destructors
        

Copyright © - by the D Language Foundation Page generated by Ddoc on Mon Apr : : Brave Browser

    

(Read More) (Full coverage and live updates on the Coronavirus (Covid -) )

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

2020 Guide to Generating Leads With Google Local Service Ads

Pixel Buds, Hacker News

Pixel Buds, Hacker News