📜 ⬆️ ⬇️

Better than C

Currently, system programmers have almost no choice what language to use. For good, all options are reduced to either pure C or Rust, although, as experience shows, not everyone likes its syntax.

Thanks to the efforts of the language development team, D now has a subset that is compatible with pure C, which possesses not only the syntax customary for any C programmer, but also significantly extends the functionality of the language. The new subset is called “betterC”. This subset allows you to take the C writing application to a new level.

Here is a brief list of what allows a betterC mode to a programmer:

1. Lack of preprocessor
2. Higher compilation speed
3. Full modularity
3.1 Support for various types of imports (static imports, partial imports, importing renames, etc.)
4. At the time of compilation it is possible: code generation, introspection, checking various conditions
4.1 Branching at compile time through static if and static foreach
4.2 possible operation of blocks of the form version(linux) { ... }
5. Templates
6. Analogous to borrow checking from Rust through scope pointers (scope T*) , scope slices (scope T[]) and scope references (scope ref T)
7. Support for const and immutable access modifiers
8. TLS default
9. Contract programming support
10. Convenient arrays with slice support
11. Acceleration of work with arrays due to SIMD
12. Convenient unit tests
13. Built-in profiler
14. User-defined attributes
15. Built-in and very convenient documentation generator
16. Syntax familiar to every C programmer
17. Unicode support
18. Better than C memory safety
19. Closures
20. Much more.
')
Let's now look at examples.

The following C code:

 #include <stdio.h> int main(int argc, char** argv) { printf("hello world\n"); return 0; } 

Will be compiled into:

 _main: push EAX mov [ESP],offset FLAT:_DATA call near ptr _printf xor EAX,EAX pop ECX ret 

Output size: 23,068 bytes.

Now the same thing on D in betterC mode:

 import core.stdc.stdio; extern (C) int main(int argc, char** argv) { printf("hello world\n"); return 0; } 

Output size: 23,068 bytes.

Not bad right? The same code without a betterC mode will weigh 194Kb.

More complicated example:

C version:

 #include <stdio.h> /* Eratosthenes Sieve prime number calculation. */ #define true 1 #define false 0 #define size 8190 #define sizepl 8191 char flags[sizepl]; int main() { int i, prime, k, count, iter; printf ("10 iterations\n"); for (iter = 1; iter <= 10; iter++) { count = 0; for (i = 0; i <= size; i++) flags[i] = true; for (i = 0; i <= size; i++) { if (flags[i]) { prime = i + i + 3; k = i + prime; while (k <= size) { flags[k] = false; k += prime; } count += 1; } } } printf ("\n%d primes", count); return 0; } 

BetterC version on D:

 import core.stdc.stdio; extern (C): //          C __gshared bool[8191] flags; int main() { int count; printf("10 iterations\n"); foreach (iter; 1 .. 11) { count = 0; flags[] = true; foreach (i; 0 .. flags.length) { if (flags[i]) { const prime = i + i + 3; // const -        auto k = i + prime; while (k < flags.length) { flags[k] = false; k += prime; } count += 1; } } } printf("%d primes\n", count); return 0; } 

It turned out much more readable and short.

To initialize an empty project, call: dub init

To enable this mode in dub.sdl you need to add the line:

dflags "-betterC"
For dub.json, the line will be:
"dflags" : ["betterC"],

Requires dmd compiler version 2.076 or later .

For novice programmers published the first version of the Russian textbook on the language D.

Also released a new extension with language support for Visual Code.

Source: https://habr.com/ru/post/336392/


All Articles