Walter Bright is the "benevolent, lifelong dictator" of the D programming language and founder of Digital Mars . He has decades of experience in developing compilers and interpreters for several languages, including Zortech C ++, the first native C ++ compiler. He is also the creator of Empire , the main inspiration for Sid Meier's Civilization.
The D language was designed from the start to easily and directly access C and, to a lesser extent, C ++. Thanks to this, countless C libraries, the standard C library and of course the system APIs, which are usually built on the C API, are available in it.
But C isn't just about libraries. Many large and invaluable programs are written in C, such as the Linux operating system and most of the programs for it. While D programs can access C libraries, the opposite is not true. C programs cannot access D code. It is impossible (or at least very difficult) to compile multiple D files and link them into a C program. The problem is that compiled D files can access something that exists only in runtime D, and adding it to the link is usually impractical (the runtime is quite voluminous).
Also, D code cannot exist in a program if D does not control the function main()
, because this is how the D runtime starts. Therefore, D libraries are inaccessible to C programs, and chimera programs (a mixture of C and D) become impractical. You can't just βtryβ D by adding D modules to existing C program modules.
This was until Better C.
All this has already happened, the idea is not new. Bjarne Stroustrup in 1988 wrote an article called A the Better the C . His early C ++ compiler could compile C code almost unchanged, and he could start using C ++ features where and when it made sense - without sacrificing existing C ++ work. It was a brilliant strategy to ensure C ++'s early success.
β Kotlin, . Kotlin Java, Java-, Java Kotlin. Kotlin β Β« JavaΒ», .
D C
D C. C, , C ( , ..). D β D, , . -betterC
.
D D? , . . , C. , C D.
, , β , . , C: malloc
.
C++ COM , D β , .
, typeid
, , RAII . , , .
Better C RAII . (. .)
assert
, C D.
( , . Better C).
, Better C C?
C , . , : , , , , , , , (Compile Time Function Execution, CTFE), , (Design by Introspection, DbI).
:
#include <stdio.h>
int main(int argc, char** argv) {
printf("hello world\n");
return 0;
}
:
_main:
push EAX
mov [ESP],offset FLAT:_DATA
call near ptr _printf
xor EAX,EAX
pop ECX
ret
β 23β068 .
D:
import core.stdc.stdio;
extern (C) int main(int argc, char** argv) {
printf("hello world\n");
return 0;
}
: 23β068 . , C, D , . ( D 194 ). , D C .
Hello World β . - : :
#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;
}
Better C:
import core.stdc.stdio;
extern (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;
auto k = i + prime;
while (k < flags.length) {
flags[k] = false;
k += prime;
}
count += 1;
}
}
}
printf("%d primes\n", count);
return 0;
}
, - :
-
extern(C)
C. - D (thread-local storage, TLS). C .
__gshared
. -
foreach
β . -
const
,prime
. -
iter
,i
,prime
k
, . -
flags
flags.length
, - .
, : flags
. - ! .
Better C, , C. , D , , goto
.
On my own behalf, I can say that since the option appeared -betterC
, I began to translate many of my old, but still used programs into D - one function at a time. By working one function at a time and running a set of tests after each change, I keep the program running at all times. If something breaks, I only need to test one function to find the cause. I'm not very interested in continuing to maintain my C programs, and with the advent of Better C, there is no longer any reason for that.