root/trunk/docsrc/builtin.dd

Revision 659, 5.2 kB (checked in by andrei, 4 years ago)

Changed ddoc extensions to dd and changed linux.mak accordingly

  • Property svn:eol-style set to native
Line 
1 Ddoc
2
3 $(COMMUNITY Core Language Features vs Library Implementation,
4
5
6     $(P D offers several capabilities built in to the core language
7     that are implemented as libraries in other languages such
8     as C++:
9     )
10
11     $(OL
12     $(LI Dynamic Arrays)
13     $(LI Strings)
14     $(LI Associative Arrays)
15     $(LI Complex numbers)
16     )
17
18     $(P Some consider this as evidence of language bloat, rather than
19     a useful feature.
20     So why not implement each of these as standardized library types?
21     )
22
23     $(P Some general initial observations:
24     )
25
26     $(OL
27
28     $(LI Each of them is heavily used. This means that even small
29     improvements in usability are worth reaching for.
30     )
31
32     $(LI Being a core language feature means that the compiler can
33     issue better and more to the point error messages when a type
34     is used incorrectly.
35     Library implementations tend to give notoriously obtuse messages
36     based on the internal details of those implementations.
37     )
38
39     $(LI Library features cannot invent new syntax, new operators,
40     or new tokens.
41     )
42
43     $(LI Library implementations tend to require a lot of compile
44     time processing of the implementation, over and over for each compile,
45     that slows down compilation.
46     )
47
48     $(LI Library implementations are supposed to provide flexibility
49     to the end user. But if they are standardized, standardized to the
50     point of the compiler being allowed to recognized them as special
51     (the C++ Standard allows this), then they become just as inflexible
52     as builtin core features.
53     )
54
55     $(LI The ability to define new library types, while having greatly
56     advanced in the last few years, still leaves a lot to be desired
57     in smoothly integrating it into the existing language.
58     Rough edges, clumsy syntax, and odd corner cases abound.
59     )
60
61     )
62
63     $(P More specific comments:
64     )
65
66 $(SECTION2 Dynamic Arrays,
67
68     $(P C++ has builtin core arrays. It's just that they don't work very
69     well. Rather than fix them, several different array types were
70     created as part of the C++ Standard Template Library, each covering
71     a different deficiency in the builtin arrays. These
72     include:
73     )
74
75     $(UL
76     $(LI $(TT basic_string))
77     $(LI $(TT vector))
78     $(LI $(TT valarray))
79     $(LI $(TT deque))
80     $(LI $(TT slice_array))
81     $(LI $(TT gslice_array))
82     $(LI $(TT mask_array))
83     $(LI $(TT indirect_array))
84     )
85
86     $(P Fixing the builtin array support means the need for each of these
87     variations just evaporates. There's one array type that covers
88     it all, only one thing to learn, and no problems getting one array
89     type to work with another array type.
90     )
91
92     $(P As usual, a builtin type lets us create syntactic sugar for it.
93     This starts with having an array literal, and follows with some
94     new operators specific to arrays. A library array implementation
95     has to make due with overloading existing operators.
96     The indexing operator, $(TT a[i]), it shares with C++.
97     Added are the array concatenation operator $(TT ~), array append operator
98     $(TT ~=), array slice operator $(TT a[i..j]),
99     and the array vector operator
100     $(TT a[]).
101     )
102
103     $(P The ~ and ~= concatenation operators resolve a problem that comes
104     up when only existing operators can be overloaded. Usually, + is
105     pressed into service as concatenation for library array
106     implementations. But that winds up precluding having + mean
107     array vector addition. Furthermore, concatenation has nothing in
108     common with addition, and using the same operator for both is
109     confusing.
110     )
111 )
112
113
114 $(SECTION2 Strings,
115
116     $(P A <a href="cppstrings.html">detailed comparison with C++'s std::string</a>.
117     )
118
119     $(P C++ has, of course, builtin string support in the form of string
120     literals and char arrays. It's just that they suffer from all
121     the weaknesses of C++ builtin arrays.
122     )
123
124     $(P But after all, what is a string if not an array of characters?
125     If the builtin array problems are fixed, doesn't that resolve
126     the string problems as well? It does. It seems odd at first that
127     D doesn't have a string class, but since manipulating strings
128     is nothing more than manipulating arrays of characters, if arrays
129     work, there's nothing a class adds to it.
130     )
131
132     $(P Furthermore, the oddities resulting from builtin string literals
133     not being of the same type as the library string class type go
134     away.
135     )
136 )
137
138
139 $(SECTION2 Associative Arrays,
140
141     $(P The main benefit for this is, once again, syntactic sugar.
142     An associative array keying off of a type $(TT T) and storing an
143     $(TT int) value is naturally written
144     as:
145     )
146
147 ---------------
148 int[T] foo;
149 ---------------
150
151     $(P rather than:
152     )
153
154 ---------------
155 import std.associativeArray;
156 ...
157 std.associativeArray.AA!(T, int) foo;
158 ---------------
159
160     $(P Builtin associative arrays also offer the possibility of having
161     associative array literals, which are an often requested additional
162     feature.
163     )
164 )
165
166
167 $(SECTION2 Complex Numbers,
168
169     $(P A $(LINK2 cppcomplex.html, detailed comparison with C++'s std::complex).
170     )
171
172     $(P The most compelling reason is compatibility with C's imaginary
173     and complex floating point types.
174     Next, is the ability to have imaginary floating point literals.
175     Isn't:
176     )
177
178 ---------------
179 c = (6 + 2i - 1 + 3i) / 3i;
180 ---------------
181
182     $(P far preferable than writing:
183     )
184
185 ---------------
186 c = (complex!(double)(6,2) + complex!(double)(-1,3)) / complex!(double)(0,3);
187 ---------------
188
189     $(P ? It's no contest.
190     )
191 )
192
193 )
194
195 Macros:
196     TITLE=D Builtin Rationale
197     WIKI=builtins
Note: See TracBrowser for help on using the browser.