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