Skip to content

Commit 233e39e

Browse files
authored
Merge pull request #2145 from WalterBright/betterc-spec
betterc.dd: expand and reword merged-on-behalf-of: MetaLang <MetaLang@users.noreply.github.com>
2 parents 39db9d6 + ae7bcfd commit 233e39e

File tree

1 file changed

+74
-24
lines changed

1 file changed

+74
-24
lines changed

spec/betterc.dd

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,88 @@ $(SPEC_S Better C,
44

55
$(HEADERNAV_TOC)
66

7-
`-betterC` is a command-line flag for `dmd`,
8-
which restricts the compiler's support of certain runtime features.
9-
Notably, D programs or libraries compiled with `betterC` aren't linked with Druntime.
10-
The use of compile-time features is not restricted in any way.
7+
$(P It is straightforward to link C functions and libraries into D programs.
8+
But linking D functions and libraries into C programs is not straightforward.
9+
)
1110

12-
Limiting a program or library to this subset of runtime features is useful
13-
when targeting constrained environments where the use of such features is not practical or possible.
14-
Additionally, this also makes embedding D libraries in larger projects easier by:
11+
$(P D programs generally require:)
1512

16-
$(OL
17-
$(LI Simplifying the process of integration at the build-system level)
18-
$(LI Removing the need to ensure that Druntime is properly initialized on calls to the library, when an initialization step is not performed before the library is used.)
19-
$(LI Mixing memory management strategies (GC + manual memory management) can sometimes be tricky, hence removing D's GC from the equation may be a good solution)
20-
)
13+
$(OL
14+
$(LI The D runtime library (Phobos) to be linked in, because many features of
15+
the core language require runtime library support.)
16+
$(LI The `main()` function to be written in D, to ensure that the required
17+
runtime library support is properly initialized.)
18+
)
19+
20+
$(P To link D functions and libraries into C programs, it's necessary to only
21+
require the C runtime library to be linked in. This is accomplished by defining
22+
a subset of D that fits this requirement, called $(B BetterC).
23+
)
24+
25+
$(IMPLEMENTATION_DEFINED $(B BetterC) is typically enabled by setting the $(TT -betterC)
26+
command line flag for the implementation.
27+
)
2128

22-
---
23-
extern(C) void main()
24-
{
25-
import core.stdc.stdio : printf;
26-
printf("Hello betterC\n");
27-
}
28-
---
29+
$(P When $(B BetterC) is enabled, the predefined $(LINK2 version.html, version) `D_BetterC`
30+
can be used for conditional compilation.
31+
)
32+
33+
34+
$(P An entire program can be written in $(B BetterC) by supplying a C `main()` function:)
35+
36+
---
37+
extern(C) void main()
38+
{
39+
import core.stdc.stdio : printf;
40+
printf("Hello betterC\n");
41+
}
42+
---
2943

3044
$(CONSOLE
3145
> dmd -betterC hello.d && ./hello
3246
Hello betterC
3347
)
3448

35-
$(H2 $(LNAME2 consequences, Consequences))
49+
$(P Limiting a program to this subset of runtime features is useful
50+
when targeting constrained environments where the use of such features
51+
is not practical or possible.
52+
)
3653

37-
As no Druntime is available, many D features won't work.
38-
For example:
54+
$(P $(B BetterC) makes embedding D libraries in existing larger projects easier by:
55+
)
56+
57+
$(OL
58+
$(LI Simplifying the process of integration at the build-system level)
59+
$(LI Removing the need to ensure that Druntime is properly initialized on
60+
calls to the library, when an initialization step is not performed before
61+
the library is used.)
62+
$(LI Mixing memory management strategies (GC + manual memory management) can
63+
be tricky, hence removing D's GC from the equation may be a good solution.)
64+
)
65+
66+
67+
$(H2 $(LNAME2 retained, Retained Features))
68+
69+
$(P Nearly the full language remains available. Highlights include:)
70+
71+
$(OL
72+
$(LI Unrestricted use of compile-time features)
73+
$(LI Full metaprogramming facilities)
74+
$(LI Nested functions, nested structs, delegates and lambdas)
75+
$(LI Member functions, constructors, destructors, operating overloading, etc.)
76+
$(LI The full module system)
77+
$(LI Dynamic arrays, array slicing, and array bounds checking)
78+
$(LI RAII (yes, it can work without exceptions))
79+
$(LI `scope(exit)`)
80+
$(LI Memory safety protections)
81+
$(LI Interfacing with C++)
82+
$(LI COM classes and C++ classes)
83+
$(LI `assert` failures are directed to the C runtime library)
84+
)
85+
86+
$(H2 $(LNAME2 consequences, Not Available))
87+
88+
$(P D features not available with $(B BetterC):)
3989

4090
$(OL
4191
$(LI Garbage Collection)
@@ -50,10 +100,10 @@ $(OL
50100
$(LI `synchronized` and $(MREF core, sync))
51101
$(LI Static module constructors or deconstructors)
52102
$(LI Struct deconstructors)
53-
$(LI `unittest` (testing can be done without the `-betterC` flag))
103+
$(LI `unittest` (testing can be done without the $(TT -betterC) flag))
54104
)
55105

56-
The predefined $(LINK2 version.html, version) `D_BetterC` can be used for conditional compilation.
106+
57107

58108
$(SPEC_SUBNAV_PREV simd, Vector Extensions)
59109

0 commit comments

Comments
 (0)