| 1 |
Ddoc |
|---|
| 2 |
|
|---|
| 3 |
$(SPEC_S Functions, |
|---|
| 4 |
|
|---|
| 5 |
$(GRAMMAR |
|---|
| 6 |
$(GNAME FunctionBody): |
|---|
| 7 |
$(LINK2 statement.html#BlockStatement, $(I BlockStatement)) |
|---|
| 8 |
$(I BodyStatement) |
|---|
| 9 |
$(I InStatement) $(I BodyStatement) |
|---|
| 10 |
$(I OutStatement) $(I BodyStatement) |
|---|
| 11 |
$(I InStatement) $(I OutStatement) $(I BodyStatement) |
|---|
| 12 |
$(I OutStatement) $(I InStatement) $(I BodyStatement) |
|---|
| 13 |
|
|---|
| 14 |
$(GNAME InStatement): |
|---|
| 15 |
$(B in) $(LINK2 statement.html#BlockStatement, $(I BlockStatement)) |
|---|
| 16 |
|
|---|
| 17 |
$(GNAME OutStatement): |
|---|
| 18 |
$(B out) $(LINK2 statement.html#BlockStatement, $(I BlockStatement)) |
|---|
| 19 |
$(B out) $(B $(LPAREN)) $(I Identifier) $(B $(RPAREN)) $(LINK2 statement.html#BlockStatement, $(I BlockStatement)) |
|---|
| 20 |
|
|---|
| 21 |
$(GNAME BodyStatement): |
|---|
| 22 |
$(B body) $(LINK2 statement.html#BlockStatement, $(I BlockStatement)) |
|---|
| 23 |
) |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
<h3>Function Return Values</h3> |
|---|
| 27 |
|
|---|
| 28 |
$(P Function return values are considered to be rvalues. |
|---|
| 29 |
This means they cannot be passed by reference to other functions. |
|---|
| 30 |
) |
|---|
| 31 |
|
|---|
| 32 |
$(V2 |
|---|
| 33 |
<h3>$(LNAME2 pure-functions, Pure Functions)</h3> |
|---|
| 34 |
|
|---|
| 35 |
$(P Pure functions are functions that produce the same |
|---|
| 36 |
result for the same arguments. |
|---|
| 37 |
To that end, a pure function: |
|---|
| 38 |
) |
|---|
| 39 |
|
|---|
| 40 |
$(UL |
|---|
| 41 |
$(LI does not read or write any global mutable state) |
|---|
| 42 |
$(LI cannot call functions that are not pure) |
|---|
| 43 |
$(LI can override an impure function, but an impure function |
|---|
| 44 |
cannot override a pure one) |
|---|
| 45 |
$(LI is covariant with an impure function) |
|---|
| 46 |
$(LI cannot perform I/O) |
|---|
| 47 |
) |
|---|
| 48 |
|
|---|
| 49 |
$(P As a concession to practicality, a pure function can:) |
|---|
| 50 |
|
|---|
| 51 |
$(UL |
|---|
| 52 |
$(LI allocate memory via a $(LINK2 expression.html#NewExpression, $(I NewExpression))) |
|---|
| 53 |
$(LI terminate the program) |
|---|
| 54 |
$(LI read and write the floating point exception flags) |
|---|
| 55 |
$(LI read and write the floating point mode flags, as long as those flags |
|---|
| 56 |
are restored to their initial state upon function entry) |
|---|
| 57 |
) |
|---|
| 58 |
|
|---|
| 59 |
$(P A pure function can throw exceptions.) |
|---|
| 60 |
|
|---|
| 61 |
--- |
|---|
| 62 |
int x; |
|---|
| 63 |
immutable int y; |
|---|
| 64 |
const int* pz; |
|---|
| 65 |
|
|---|
| 66 |
pure int foo(int i, |
|---|
| 67 |
char* p, |
|---|
| 68 |
const char* q, |
|---|
| 69 |
immutable int* s) |
|---|
| 70 |
{ |
|---|
| 71 |
x = i; // error, modifying global state |
|---|
| 72 |
i = x; // error, reading mutable global state |
|---|
| 73 |
i = y; // ok, reading immutable global state |
|---|
| 74 |
i = *pz; // error, reading const global state |
|---|
| 75 |
return i; |
|---|
| 76 |
} |
|---|
| 77 |
--- |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
<h3>$(LNAME2 nothrow-functions, Nothrow Functions)</h3> |
|---|
| 81 |
|
|---|
| 82 |
$(P Nothrow functions do not throw any exceptions derived |
|---|
| 83 |
from class $(I Exception). |
|---|
| 84 |
) |
|---|
| 85 |
|
|---|
| 86 |
$(P Nothrow functions are covariant with throwing ones.) |
|---|
| 87 |
|
|---|
| 88 |
<h3>$(LNAME2 ref-functions, Ref Functions)</h3> |
|---|
| 89 |
|
|---|
| 90 |
$(P Ref functions allow functions to return by reference. |
|---|
| 91 |
This is analogous to ref function parameters. |
|---|
| 92 |
) |
|---|
| 93 |
|
|---|
| 94 |
--- |
|---|
| 95 |
ref int foo() |
|---|
| 96 |
{ auto p = new int; |
|---|
| 97 |
return *p; |
|---|
| 98 |
} |
|---|
| 99 |
... |
|---|
| 100 |
foo() = 3; // reference returns can be lvalues |
|---|
| 101 |
--- |
|---|
| 102 |
|
|---|
| 103 |
<h3>$(LNAME2 auto-functions, Auto Functions)</h3> |
|---|
| 104 |
|
|---|
| 105 |
$(P Auto functions have their return type inferred from any |
|---|
| 106 |
$(LINK2 statement.html#ReturnStatement, $(I ReturnStatement))s |
|---|
| 107 |
in the function body. |
|---|
| 108 |
) |
|---|
| 109 |
|
|---|
| 110 |
$(P An auto function is declared without a return type. |
|---|
| 111 |
If it does not already have a storage class, use the |
|---|
| 112 |
$(D_KEYWORD auto) storage class. |
|---|
| 113 |
) |
|---|
| 114 |
|
|---|
| 115 |
$(P If there are multiple $(I ReturnStatement)s, the types |
|---|
| 116 |
of them must match exactly. If there are no $(I ReturnStatement)s, |
|---|
| 117 |
the return type is inferred to be $(D_KEYWORD void). |
|---|
| 118 |
) |
|---|
| 119 |
|
|---|
| 120 |
--- |
|---|
| 121 |
auto foo(int i) |
|---|
| 122 |
{ |
|---|
| 123 |
return i + 3; // return type is inferred to be int |
|---|
| 124 |
} |
|---|
| 125 |
--- |
|---|
| 126 |
|
|---|
| 127 |
$(V2 |
|---|
| 128 |
<h3>$(LNAME2 auto-ref-functions, Auto Ref Functions)</h3> |
|---|
| 129 |
|
|---|
| 130 |
$(P Auto ref functions infer their return type just as |
|---|
| 131 |
$(LINK2 #auto-functions, auto functions) do. |
|---|
| 132 |
In addition, they become $(LINK2 #ref-functions, ref functions) |
|---|
| 133 |
if the return expression is an lvalue, |
|---|
| 134 |
and it would not be a reference to a local or a parameter. |
|---|
| 135 |
) |
|---|
| 136 |
|
|---|
| 137 |
--- |
|---|
| 138 |
auto ref foo(int x) { return x; } // value return |
|---|
| 139 |
auto ref foo() { return 3; } // value return |
|---|
| 140 |
auto ref foo(ref int x) { return x; } // ref return |
|---|
| 141 |
auto ref foo(out int x) { return x; } // ref return |
|---|
| 142 |
auto ref foo() { static int x; return x; } // ref return |
|---|
| 143 |
--- |
|---|
| 144 |
|
|---|
| 145 |
$(P The lexically first $(LINK2 statement.html#ReturnStatement, $(I ReturnStatement)) |
|---|
| 146 |
determines the ref-ness of a function: |
|---|
| 147 |
) |
|---|
| 148 |
|
|---|
| 149 |
--- |
|---|
| 150 |
auto ref foo(ref int x) { return 3; return x; } // ok, value return |
|---|
| 151 |
auto ref foo(ref int x) { return x; return 3; } // error, ref return, 3 is not an lvalue |
|---|
| 152 |
--- |
|---|
| 153 |
) |
|---|
| 154 |
|
|---|
| 155 |
$(V2 |
|---|
| 156 |
<h3>$(LNAME2 inout-functions, Inout Functions)</h3> |
|---|
| 157 |
|
|---|
| 158 |
$(P Functions that deal with mutable, const, or immutable types with |
|---|
| 159 |
equanimity often need to transmit their type to the return value: |
|---|
| 160 |
) |
|---|
| 161 |
|
|---|
| 162 |
--- |
|---|
| 163 |
int[] foo(int[] a, int x, int y) { return a[x .. y]; } |
|---|
| 164 |
const(int)[] foo(const(int)[] a, int x, int y) { return a[x .. y]; } |
|---|
| 165 |
immutable(int)[] foo(immutable(int)[] a, int x, int y) { return a[x .. y]; } |
|---|
| 166 |
--- |
|---|
| 167 |
|
|---|
| 168 |
$(P The code generated by these three functions is identical. |
|---|
| 169 |
To indicate that these can be one function, the $(D_KEYWORD inout) |
|---|
| 170 |
type constructor is employed: |
|---|
| 171 |
) |
|---|
| 172 |
|
|---|
| 173 |
--- |
|---|
| 174 |
inout(int)[] foo(inout(int)[] a, int x, int y) { return a[x .. y]; } |
|---|
| 175 |
--- |
|---|
| 176 |
|
|---|
| 177 |
$(P The $(D_KEYWORD inout) forms a wildcard that stands in for |
|---|
| 178 |
any of mutable, const or immutable. When the function is called, |
|---|
| 179 |
the inout of the return type is changed to whatever the mutable, |
|---|
| 180 |
const, or immutable status of the argument type to the parameter |
|---|
| 181 |
inout was. |
|---|
| 182 |
) |
|---|
| 183 |
|
|---|
| 184 |
$(P Inout types can be implicitly converted to const, but to nothing |
|---|
| 185 |
else. Other types cannot be implicitly converted to inout. |
|---|
| 186 |
Casting to or from inout is not allowed in @safe functions. |
|---|
| 187 |
) |
|---|
| 188 |
|
|---|
| 189 |
$(P If an inout appears in a function parameter list, it must also appear |
|---|
| 190 |
in the return type. |
|---|
| 191 |
) |
|---|
| 192 |
|
|---|
| 193 |
$(P A set of arguments to a function with inout parameters is considered |
|---|
| 194 |
a match if any inout argument types match exactly, or:) |
|---|
| 195 |
|
|---|
| 196 |
$(OL |
|---|
| 197 |
$(LI No argument types are composed of inout types.) |
|---|
| 198 |
$(LI A mutable, const or immutable argument type can be matched against each |
|---|
| 199 |
corresponding parameter inout type.) |
|---|
| 200 |
) |
|---|
| 201 |
|
|---|
| 202 |
$(P If such a match occurs, if every match is mutable, then the inout is |
|---|
| 203 |
considered matched with mutable. If every match is immutable, then the |
|---|
| 204 |
inout is considered matched with immutable. Otherwise, the inout is |
|---|
| 205 |
considered matched with const. The inout in the return type is then rewritten |
|---|
| 206 |
to be the inout matched attribute. |
|---|
| 207 |
) |
|---|
| 208 |
|
|---|
| 209 |
$(P Global and static variable types cannot have any inout components. |
|---|
| 210 |
) |
|---|
| 211 |
|
|---|
| 212 |
$(P $(B Note:) Shared types are not overlooked. Shared types cannot |
|---|
| 213 |
be matched with inout. |
|---|
| 214 |
) |
|---|
| 215 |
) |
|---|
| 216 |
|
|---|
| 217 |
<h3>$(LNAME2 property-functions, Property Functions)</h3> |
|---|
| 218 |
|
|---|
| 219 |
$(P Property functions are tagged with the $(CODE @property) |
|---|
| 220 |
attribute. They can be called without parentheses (hence |
|---|
| 221 |
acting like properties). |
|---|
| 222 |
) |
|---|
| 223 |
|
|---|
| 224 |
--- |
|---|
| 225 |
struct S |
|---|
| 226 |
{ |
|---|
| 227 |
int m_x; |
|---|
| 228 |
@property |
|---|
| 229 |
{ int x() { return m_x; } |
|---|
| 230 |
int x(int newx) { return m_x = newx; } |
|---|
| 231 |
} |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
void foo() |
|---|
| 235 |
{ |
|---|
| 236 |
S s; |
|---|
| 237 |
s.x = 3; // calls s.x(int) |
|---|
| 238 |
bar(s.x); // calls bar(s.x()) |
|---|
| 239 |
} |
|---|
| 240 |
--- |
|---|
| 241 |
) |
|---|
| 242 |
|
|---|
| 243 |
<h3>$(LNAME2 virtual-functions, Virtual Functions)</h3> |
|---|
| 244 |
|
|---|
| 245 |
$(P Virtual functions are functions that are called indirectly |
|---|
| 246 |
through a function |
|---|
| 247 |
pointer table, called a vtbl[], rather than directly. |
|---|
| 248 |
All non-static non-private non-template member functions are virtual. |
|---|
| 249 |
This may sound |
|---|
| 250 |
inefficient, but since the D compiler knows all of the class |
|---|
| 251 |
hierarchy when generating code, all |
|---|
| 252 |
functions that are not overridden can be optimized to be non-virtual. |
|---|
| 253 |
In fact, since |
|---|
| 254 |
C++ programmers tend to "when in doubt, make it virtual", the D way of |
|---|
| 255 |
"make it |
|---|
| 256 |
virtual unless we can prove it can be made non-virtual" results, on |
|---|
| 257 |
average, in many |
|---|
| 258 |
more direct function calls. It also results in fewer bugs caused by |
|---|
| 259 |
not declaring |
|---|
| 260 |
a function virtual that gets overridden. |
|---|
| 261 |
) |
|---|
| 262 |
|
|---|
| 263 |
$(P Functions with non-D linkage cannot be virtual, and hence cannot be |
|---|
| 264 |
overridden. |
|---|
| 265 |
) |
|---|
| 266 |
|
|---|
| 267 |
$(P Member template functions cannot be virtual, and hence cannot be |
|---|
| 268 |
overridden. |
|---|
| 269 |
) |
|---|
| 270 |
|
|---|
| 271 |
$(P Functions marked as $(TT final) may not be overridden in a |
|---|
| 272 |
derived class, unless they are also $(TT private). |
|---|
| 273 |
For example: |
|---|
| 274 |
) |
|---|
| 275 |
|
|---|
| 276 |
------ |
|---|
| 277 |
class A |
|---|
| 278 |
{ |
|---|
| 279 |
int def() { ... } |
|---|
| 280 |
final int foo() { ... } |
|---|
| 281 |
final private int bar() { ... } |
|---|
| 282 |
private int abc() { ... } |
|---|
| 283 |
} |
|---|
| 284 |
|
|---|
| 285 |
class B : A |
|---|
| 286 |
{ |
|---|
| 287 |
int def() { ... } // ok, overrides A.def |
|---|
| 288 |
int foo() { ... } // error, A.foo is final |
|---|
| 289 |
int bar() { ... } // ok, A.bar is final private, but not virtual |
|---|
| 290 |
int abc() { ... } // ok, A.abc is not virtual, B.abc is virtual |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
void test(A a) |
|---|
| 294 |
{ |
|---|
| 295 |
a.def(); // calls B.def |
|---|
| 296 |
a.foo(); // calls A.foo |
|---|
| 297 |
a.bar(); // calls A.bar |
|---|
| 298 |
a.abc(); // calls A.abc |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
void func() |
|---|
| 302 |
{ B b = new B(); |
|---|
| 303 |
test(b); |
|---|
| 304 |
} |
|---|
| 305 |
------ |
|---|
| 306 |
|
|---|
| 307 |
$(P Covariant return types |
|---|
| 308 |
are supported, which means that the |
|---|
| 309 |
overriding function in a derived class can return a type |
|---|
| 310 |
that is derived from the type returned by the overridden function: |
|---|
| 311 |
) |
|---|
| 312 |
|
|---|
| 313 |
------ |
|---|
| 314 |
class A { } |
|---|
| 315 |
class B : A { } |
|---|
| 316 |
|
|---|
| 317 |
class Foo |
|---|
| 318 |
{ |
|---|
| 319 |
A test() { return null; } |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|
| 322 |
class Bar : Foo |
|---|
| 323 |
{ |
|---|
| 324 |
B test() { return null; } // overrides and is covariant with Foo.test() |
|---|
| 325 |
} |
|---|
| 326 |
------ |
|---|
| 327 |
|
|---|
| 328 |
$(P Virtual functions all have a hidden parameter called the |
|---|
| 329 |
$(I this) reference, which refers to the class object for which |
|---|
| 330 |
the function is called. |
|---|
| 331 |
) |
|---|
| 332 |
|
|---|
| 333 |
<h3>$(LNAME2 function-inheritance, Function Inheritance and Overriding)</h3> |
|---|
| 334 |
|
|---|
| 335 |
A functions in a derived class with the same name and parameter |
|---|
| 336 |
types as a function in a base class overrides that function: |
|---|
| 337 |
|
|---|
| 338 |
------ |
|---|
| 339 |
class A |
|---|
| 340 |
{ |
|---|
| 341 |
int foo(int x) { ... } |
|---|
| 342 |
} |
|---|
| 343 |
|
|---|
| 344 |
class B : A |
|---|
| 345 |
{ |
|---|
| 346 |
override int foo(int x) { ... } |
|---|
| 347 |
} |
|---|
| 348 |
|
|---|
| 349 |
void test() |
|---|
| 350 |
{ |
|---|
| 351 |
B b = new B(); |
|---|
| 352 |
bar(b); |
|---|
| 353 |
} |
|---|
| 354 |
|
|---|
| 355 |
void bar(A a) |
|---|
| 356 |
{ |
|---|
| 357 |
a.foo(1); // calls B.foo(int) |
|---|
| 358 |
} |
|---|
| 359 |
------ |
|---|
| 360 |
|
|---|
| 361 |
$(P However, when doing overload resolution, the functions in the base |
|---|
| 362 |
class are not considered: |
|---|
| 363 |
) |
|---|
| 364 |
|
|---|
| 365 |
------ |
|---|
| 366 |
class A |
|---|
| 367 |
{ |
|---|
| 368 |
int foo(int x) { ... } |
|---|
| 369 |
int foo(long y) { ... } |
|---|
| 370 |
} |
|---|
| 371 |
|
|---|
| 372 |
class B : A |
|---|
| 373 |
{ |
|---|
| 374 |
override int foo(long x) { ... } |
|---|
| 375 |
} |
|---|
| 376 |
|
|---|
| 377 |
void test() |
|---|
| 378 |
{ |
|---|
| 379 |
B b = new B(); |
|---|
| 380 |
b.foo(1); // calls B.foo(long), since A.foo(int) not considered |
|---|
| 381 |
A a = b; |
|---|
| 382 |
$(V1 a.foo(1); // calls A.foo(int)) |
|---|
| 383 |
$(V2 a.foo(1); // issues runtime error (instead of calling A.foo(int))) |
|---|
| 384 |
} |
|---|
| 385 |
------ |
|---|
| 386 |
|
|---|
| 387 |
$(P To consider the base class's functions in the overload resolution |
|---|
| 388 |
process, use an $(I AliasDeclaration): |
|---|
| 389 |
) |
|---|
| 390 |
|
|---|
| 391 |
------ |
|---|
| 392 |
class A |
|---|
| 393 |
{ |
|---|
| 394 |
int foo(int x) { ... } |
|---|
| 395 |
int foo(long y) { ... } |
|---|
| 396 |
} |
|---|
| 397 |
|
|---|
| 398 |
class B : A |
|---|
| 399 |
{ |
|---|
| 400 |
$(B alias A.foo foo;) |
|---|
| 401 |
override int foo(long x) { ... } |
|---|
| 402 |
} |
|---|
| 403 |
|
|---|
| 404 |
void test() |
|---|
| 405 |
{ |
|---|
| 406 |
B b = new B(); |
|---|
| 407 |
bar(b); |
|---|
| 408 |
} |
|---|
| 409 |
|
|---|
| 410 |
void bar(A a) |
|---|
| 411 |
{ |
|---|
| 412 |
a.foo(1); // calls A.foo(int) |
|---|
| 413 |
B b = new B(); |
|---|
| 414 |
b.foo(1); // calls A.foo(int) |
|---|
| 415 |
} |
|---|
| 416 |
------ |
|---|
| 417 |
|
|---|
| 418 |
$(V2 |
|---|
| 419 |
$(P If such an $(I AliasDeclaration) is not used, the derived |
|---|
| 420 |
class's functions completely override all the functions of the |
|---|
| 421 |
same name in the base class, even if the types of the parameters |
|---|
| 422 |
in the base class functions are different. If, through |
|---|
| 423 |
implicit conversions to the base class, those other functions do |
|---|
| 424 |
get called, an $(CODE std.HiddenFuncError) exception is raised: |
|---|
| 425 |
) |
|---|
| 426 |
--- |
|---|
| 427 |
import std.hiddenfunc; |
|---|
| 428 |
|
|---|
| 429 |
class A |
|---|
| 430 |
{ |
|---|
| 431 |
void set(long i) { } |
|---|
| 432 |
void $(B set)(int i) { } |
|---|
| 433 |
} |
|---|
| 434 |
class B : A |
|---|
| 435 |
{ |
|---|
| 436 |
void set(long i) { } |
|---|
| 437 |
} |
|---|
| 438 |
|
|---|
| 439 |
void foo(A a) |
|---|
| 440 |
{ int i; |
|---|
| 441 |
try |
|---|
| 442 |
{ |
|---|
| 443 |
a.$(B set)(3); // error, throws runtime exception since |
|---|
| 444 |
// A.set(int) should not be available from B |
|---|
| 445 |
} |
|---|
| 446 |
catch ($(B HiddenFuncError) o) |
|---|
| 447 |
{ |
|---|
| 448 |
i = 1; |
|---|
| 449 |
} |
|---|
| 450 |
assert(i == 1); |
|---|
| 451 |
} |
|---|
| 452 |
|
|---|
| 453 |
void main() |
|---|
| 454 |
{ |
|---|
| 455 |
foo(new B); |
|---|
| 456 |
} |
|---|
| 457 |
--- |
|---|
| 458 |
$(P If an $(CODE HiddenFuncError) exception is thrown in your program, |
|---|
| 459 |
the use of overloads and overrides needs to be reexamined in the |
|---|
| 460 |
relevant classes.) |
|---|
| 461 |
|
|---|
| 462 |
$(P The $(CODE HiddenFuncError) exception is not thrown if the |
|---|
| 463 |
hidden function is disjoint, as far as overloading is concerned, |
|---|
| 464 |
from all the other virtual functions is the inheritance hierarchy.) |
|---|
| 465 |
) |
|---|
| 466 |
|
|---|
| 467 |
$(P A function parameter's default value is not inherited:) |
|---|
| 468 |
|
|---|
| 469 |
------ |
|---|
| 470 |
class A |
|---|
| 471 |
{ |
|---|
| 472 |
void foo(int $(B x = 5)) { ... } |
|---|
| 473 |
} |
|---|
| 474 |
|
|---|
| 475 |
class B : A |
|---|
| 476 |
{ |
|---|
| 477 |
void foo(int $(B x = 7)) { ... } |
|---|
| 478 |
} |
|---|
| 479 |
|
|---|
| 480 |
class C : B |
|---|
| 481 |
{ |
|---|
| 482 |
void foo(int $(B x)) { ... } |
|---|
| 483 |
} |
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
void test() |
|---|
| 487 |
{ |
|---|
| 488 |
A a = new A(); |
|---|
| 489 |
a.foo(); // calls A.foo(5) |
|---|
| 490 |
|
|---|
| 491 |
B b = new B(); |
|---|
| 492 |
b.foo(); // calls B.foo(7) |
|---|
| 493 |
|
|---|
| 494 |
C c = new C(); |
|---|
| 495 |
c.foo(); // error, need an argument for C.foo |
|---|
| 496 |
} |
|---|
| 497 |
------ |
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
<h3>Inline Functions</h3> |
|---|
| 501 |
|
|---|
| 502 |
There is no inline keyword. The compiler makes the decision whether to |
|---|
| 503 |
inline a function or not, analogously to the register keyword no |
|---|
| 504 |
longer being relevant to a |
|---|
| 505 |
compiler's decisions on enregistering variables. |
|---|
| 506 |
(There is no register keyword either.) |
|---|
| 507 |
|
|---|
| 508 |
|
|---|
| 509 |
<h2>$(LNAME2 function-overloading, Function Overloading)</h2> |
|---|
| 510 |
|
|---|
| 511 |
$(P Functions are overloaded based on how well the arguments |
|---|
| 512 |
to a function can match up with the parameters. |
|---|
| 513 |
The function with the $(I best) match is selected. |
|---|
| 514 |
The levels of matching are: |
|---|
| 515 |
) |
|---|
| 516 |
|
|---|
| 517 |
$(OL |
|---|
| 518 |
$(LI no match) |
|---|
| 519 |
$(LI match with implicit conversions) |
|---|
| 520 |
$(V2 $(LI match with conversion to const)) |
|---|
| 521 |
$(LI exact match) |
|---|
| 522 |
) |
|---|
| 523 |
|
|---|
| 524 |
$(P Each argument (including any $(CODE this) pointer) is |
|---|
| 525 |
compared against the function's corresponding parameter, to |
|---|
| 526 |
determine the match level for that argument. The match level |
|---|
| 527 |
for a function is the $(I worst) match level of each of its |
|---|
| 528 |
arguments.) |
|---|
| 529 |
|
|---|
| 530 |
$(V2 |
|---|
| 531 |
$(P Literals do not match $(CODE ref) or $(CODE out) parameters.) |
|---|
| 532 |
) |
|---|
| 533 |
$(V1 |
|---|
| 534 |
$(P If two or more functions have the same match level, |
|---|
| 535 |
it is an ambiguity error. |
|---|
| 536 |
) |
|---|
| 537 |
) |
|---|
| 538 |
$(V2 |
|---|
| 539 |
$(P If two or more functions have the same match level, |
|---|
| 540 |
then $(LNAME2 partial-ordering, $(I partial ordering)) |
|---|
| 541 |
is used to try to find the best match. |
|---|
| 542 |
Partial ordering finds the most specialized function. |
|---|
| 543 |
If neither function is more specialized than the other, |
|---|
| 544 |
then it is an ambiguity error. |
|---|
| 545 |
Partial ordering is determined for functions $(CODE f()) |
|---|
| 546 |
and $(CODE g()) by taking the parameter types of $(CODE f()), |
|---|
| 547 |
constructing a list of arguments by taking the default values |
|---|
| 548 |
of those types, and attempting to match them against $(CODE g()). |
|---|
| 549 |
If it succeeds, then $(CODE g()) is at least as specialized |
|---|
| 550 |
as $(CODE f()). |
|---|
| 551 |
For example: |
|---|
| 552 |
) |
|---|
| 553 |
--- |
|---|
| 554 |
class A { } |
|---|
| 555 |
class B : A { } |
|---|
| 556 |
class C : B { } |
|---|
| 557 |
void foo(A); |
|---|
| 558 |
void foo(B); |
|---|
| 559 |
|
|---|
| 560 |
void test() |
|---|
| 561 |
{ |
|---|
| 562 |
C c; |
|---|
| 563 |
/* Both foo(A) and foo(B) match with implicit conversion rules. |
|---|
| 564 |
* Applying partial ordering rules, |
|---|
| 565 |
* foo(B) cannot be called with an A, and foo(A) can be called |
|---|
| 566 |
* with a B. Therefore, foo(B) is more specialized, and is selected. |
|---|
| 567 |
*/ |
|---|
| 568 |
foo(c); // calls foo(B) |
|---|
| 569 |
} |
|---|
| 570 |
--- |
|---|
| 571 |
$(P A function with a variadic argument is considered less |
|---|
| 572 |
specialized than a function without. |
|---|
| 573 |
) |
|---|
| 574 |
) |
|---|
| 575 |
|
|---|
| 576 |
$(P Functions defined with non-D linkage cannot be overloaded. |
|---|
| 577 |
because the name mangling does not take the parameter types |
|---|
| 578 |
into account. |
|---|
| 579 |
) |
|---|
| 580 |
|
|---|
| 581 |
$(V2 |
|---|
| 582 |
<h2><a name="overload-sets">Overload Sets</a></h2> |
|---|
| 583 |
|
|---|
| 584 |
$(P Functions declared at the same scope overload against each |
|---|
| 585 |
other, and are called an $(I Overload Set). |
|---|
| 586 |
A typical example of an overload set are functions defined |
|---|
| 587 |
at module level: |
|---|
| 588 |
) |
|---|
| 589 |
|
|---|
| 590 |
--- |
|---|
| 591 |
module A; |
|---|
| 592 |
void foo() { } |
|---|
| 593 |
void foo(long i) { } |
|---|
| 594 |
--- |
|---|
| 595 |
|
|---|
| 596 |
$(P $(CODE A.foo()) and $(CODE A.foo(long)) form an overload set. |
|---|
| 597 |
A different module can also define functions with the same name: |
|---|
| 598 |
) |
|---|
| 599 |
|
|---|
| 600 |
--- |
|---|
| 601 |
module B; |
|---|
| 602 |
class C { } |
|---|
| 603 |
void foo(C) { } |
|---|
| 604 |
void foo(int i) { } |
|---|
| 605 |
--- |
|---|
| 606 |
|
|---|
| 607 |
$(P and A and B can be imported by a third module, C. |
|---|
| 608 |
Both overload sets, the $(CODE A.foo) overload set and the $(CODE B.foo) |
|---|
| 609 |
overload set, are found. An instance of $(CODE foo) is selected |
|---|
| 610 |
based on it matching in exactly one overload set: |
|---|
| 611 |
) |
|---|
| 612 |
|
|---|
| 613 |
--- |
|---|
| 614 |
import A; |
|---|
| 615 |
import B; |
|---|
| 616 |
|
|---|
| 617 |
void bar(C c) |
|---|
| 618 |
{ |
|---|
| 619 |
foo(); // calls A.foo() |
|---|
| 620 |
foo(1L); // calls A.foo(long) |
|---|
| 621 |
foo(c); // calls B.foo(C) |
|---|
| 622 |
foo(1,2); // error, does not match any foo |
|---|
| 623 |
foo(1); // error, matches A.foo(long) and B.foo(int) |
|---|
| 624 |
A.foo(1); // calls A.foo(long) |
|---|
| 625 |
} |
|---|
| 626 |
--- |
|---|
| 627 |
|
|---|
| 628 |
$(P Even though $(CODE B.foo(int)) is a better match than $(CODE |
|---|
| 629 |
A.foo(long)) for $(CODE foo(1)), |
|---|
| 630 |
it is an error because the two matches are in |
|---|
| 631 |
different overload sets. |
|---|
| 632 |
) |
|---|
| 633 |
|
|---|
| 634 |
$(P Overload sets can be merged with an alias declaration:) |
|---|
| 635 |
|
|---|
| 636 |
--- |
|---|
| 637 |
import A; |
|---|
| 638 |
import B; |
|---|
| 639 |
|
|---|
| 640 |
alias A.foo foo; |
|---|
| 641 |
alias B.foo foo; |
|---|
| 642 |
|
|---|
| 643 |
void bar(C c) |
|---|
| 644 |
{ |
|---|
| 645 |
foo(); // calls A.foo() |
|---|
| 646 |
foo(1L); // calls A.foo(long) |
|---|
| 647 |
foo(c); // calls B.foo(C) |
|---|
| 648 |
foo(1,2); // error, does not match any foo |
|---|
| 649 |
foo(1); // calls B.foo(int) |
|---|
| 650 |
A.foo(1); // calls A.foo(long) |
|---|
| 651 |
} |
|---|
| 652 |
--- |
|---|
| 653 |
|
|---|
| 654 |
) |
|---|
| 655 |
|
|---|
| 656 |
|
|---|
| 657 |
<h3><a name="parameters">Function Parameters</a></h3> |
|---|
| 658 |
|
|---|
| 659 |
$(V1 |
|---|
| 660 |
Parameters are $(B in), $(B out), $(B ref) or $(B lazy). |
|---|
| 661 |
$(B in) is the default; the others work like |
|---|
| 662 |
storage classes. For example: |
|---|
| 663 |
|
|---|
| 664 |
------ |
|---|
| 665 |
int foo(int x, out int y, ref int z, int q); |
|---|
| 666 |
------ |
|---|
| 667 |
|
|---|
| 668 |
x is $(B in), y is $(B out), z is $(B ref), and q is $(B in). |
|---|
| 669 |
<p> |
|---|
| 670 |
|
|---|
| 671 |
$(B out) is rare enough, and $(B ref) even rarer, to |
|---|
| 672 |
attach the keywords to |
|---|
| 673 |
them and leave $(B in) as |
|---|
| 674 |
the default. |
|---|
| 675 |
) |
|---|
| 676 |
$(V2 |
|---|
| 677 |
Parameter storage classes are $(B in), $(B out), |
|---|
| 678 |
$(B ref), $(B lazy), $(B final), $(B const), $(B immutable), or |
|---|
| 679 |
$(B scope). |
|---|
| 680 |
For example: |
|---|
| 681 |
|
|---|
| 682 |
------ |
|---|
| 683 |
int foo(in int x, out int y, ref int z, int q); |
|---|
| 684 |
------ |
|---|
| 685 |
|
|---|
| 686 |
$(P |
|---|
| 687 |
x is $(B in), y is $(B out), z is $(B ref), and q is none. |
|---|
| 688 |
) |
|---|
| 689 |
|
|---|
| 690 |
$(P |
|---|
| 691 |
The $(B in) storage class is equivalent to $(B const scope). |
|---|
| 692 |
) |
|---|
| 693 |
|
|---|
| 694 |
$(P |
|---|
| 695 |
If no storage class is specified, the parameter becomes a mutable |
|---|
| 696 |
copy of its argument. |
|---|
| 697 |
) |
|---|
| 698 |
) |
|---|
| 699 |
|
|---|
| 700 |
$(UL |
|---|
| 701 |
$(LI The function declaration makes it clear what the inputs and |
|---|
| 702 |
outputs to the function are.) |
|---|
| 703 |
$(LI It eliminates the need for IDL as a separate language.) |
|---|
| 704 |
$(LI It provides more information to the compiler, enabling more |
|---|
| 705 |
error checking and |
|---|
| 706 |
possibly better code generation.) |
|---|
| 707 |
) |
|---|
| 708 |
|
|---|
| 709 |
$(P |
|---|
| 710 |
$(B out) parameters are set to the default initializer for the |
|---|
| 711 |
type of it. For example: |
|---|
| 712 |
) |
|---|
| 713 |
------ |
|---|
| 714 |
void foo(out int x) |
|---|
| 715 |
{ |
|---|
| 716 |
// x is set to 0 at start of foo() |
|---|
| 717 |
} |
|---|
| 718 |
|
|---|
| 719 |
int a = 3; |
|---|
| 720 |
foo(a); |
|---|
| 721 |
// a is now 0 |
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 |
void abc(out int x) |
|---|
| 725 |
{ |
|---|
| 726 |
x = 2; |
|---|
| 727 |
} |
|---|
| 728 |
|
|---|
| 729 |
int y = 3; |
|---|
| 730 |
abc(y); |
|---|
| 731 |
// y is now 2 |
|---|
| 732 |
|
|---|
| 733 |
|
|---|
| 734 |
void def(ref int x) |
|---|
| 735 |
{ |
|---|
| 736 |
x += 1; |
|---|
| 737 |
} |
|---|
| 738 |
|
|---|
| 739 |
int z = 3; |
|---|
| 740 |
def(z); |
|---|
| 741 |
// z is now 4 |
|---|
| 742 |
------------ |
|---|
| 743 |
|
|---|
| 744 |
$(P For dynamic array and object parameters, which are passed |
|---|
| 745 |
by reference, in/out/ref |
|---|
| 746 |
apply only to the reference and not the contents. |
|---|
| 747 |
) |
|---|
| 748 |
|
|---|
| 749 |
$(P Lazy arguments are evaluated not when the function is called, |
|---|
| 750 |
but when the parameter is evaluated within the function. Hence, |
|---|
| 751 |
a lazy argument can be executed 0 or more times. A lazy parameter |
|---|
| 752 |
cannot be an lvalue.) |
|---|
| 753 |
|
|---|
| 754 |
--- |
|---|
| 755 |
void dotimes(int n, lazy void exp) |
|---|
| 756 |
{ |
|---|
| 757 |
while (n--) |
|---|
| 758 |
exp(); |
|---|
| 759 |
} |
|---|
| 760 |
|
|---|
| 761 |
void test() |
|---|
| 762 |
{ int x; |
|---|
| 763 |
dotimes(3, writefln(x++)); |
|---|
| 764 |
} |
|---|
| 765 |
--- |
|---|
| 766 |
|
|---|
| 767 |
$(P prints to the console:) |
|---|
| 768 |
|
|---|
| 769 |
$(CONSOLE |
|---|
| 770 |
0 |
|---|
| 771 |
1 |
|---|
| 772 |
2 |
|---|
| 773 |
) |
|---|
| 774 |
|
|---|
| 775 |
$(P A lazy parameter of type $(TT void) can accept an argument |
|---|
| 776 |
of any type.) |
|---|
| 777 |
|
|---|
| 778 |
<h3>Function Default Arguments</h3> |
|---|
| 779 |
|
|---|
| 780 |
$(P Function parameter declarations can have default values:) |
|---|
| 781 |
|
|---|
| 782 |
--- |
|---|
| 783 |
void foo(int x, int y = 3) |
|---|
| 784 |
{ |
|---|
| 785 |
... |
|---|
| 786 |
} |
|---|
| 787 |
... |
|---|
| 788 |
foo(4); // same as foo(4, 3); |
|---|
| 789 |
--- |
|---|
| 790 |
|
|---|
| 791 |
$(P Default parameters are evaluated in the context of the |
|---|
| 792 |
function declaration. |
|---|
| 793 |
If the default value for a parameter is given, all following |
|---|
| 794 |
parameters must also have default values. |
|---|
| 795 |
) |
|---|
| 796 |
|
|---|
| 797 |
<a name="variadic"><h2>Variadic Functions</h2></a> |
|---|
| 798 |
|
|---|
| 799 |
Functions taking a variable number of arguments are called |
|---|
| 800 |
variadic functions. A variadic function can take one of |
|---|
| 801 |
three forms: |
|---|
| 802 |
|
|---|
| 803 |
$(OL |
|---|
| 804 |
$(LI C-style variadic functions) |
|---|
| 805 |
$(LI Variadic functions with type info) |
|---|
| 806 |
$(LI Typesafe variadic functions) |
|---|
| 807 |
) |
|---|
| 808 |
|
|---|
| 809 |
|
|---|
| 810 |
<h3>C-style Variadic Functions</h3> |
|---|
| 811 |
|
|---|
| 812 |
A C-style variadic function is declared as taking |
|---|
| 813 |
a parameter of ... after the required function parameters. |
|---|
| 814 |
It has non-D linkage, such as $(TT extern (C)): |
|---|
| 815 |
|
|---|
| 816 |
------ |
|---|
| 817 |
extern (C) int foo(int x, int y, ...); |
|---|
| 818 |
|
|---|
| 819 |
foo(3, 4); // ok |
|---|
| 820 |
foo(3, 4, 6.8); // ok, one variadic argument |
|---|
| 821 |
foo(2); // error, y is a required argument |
|---|
| 822 |
------ |
|---|
| 823 |
|
|---|
| 824 |
There must be at least one non-variadic parameter declared. |
|---|
| 825 |
|
|---|
| 826 |
------ |
|---|
| 827 |
extern (C) int def(...); // error, must have at least one parameter |
|---|
| 828 |
------ |
|---|
| 829 |
|
|---|
| 830 |
C-style variadic functions match the C calling convention for |
|---|
| 831 |
variadic functions, and is most useful for calling C library |
|---|
| 832 |
functions like $(TT printf). |
|---|
| 833 |
The implementiations of these variadic functions have a special |
|---|
| 834 |
local variable declared for them, |
|---|
| 835 |
$(B _argptr), which is a $(TT void*) pointer to the first of the |
|---|
| 836 |
variadic |
|---|
| 837 |
arguments. To access the arguments, $(B _argptr) must be cast |
|---|
| 838 |
to a pointer to the expected argument type: |
|---|
| 839 |
|
|---|
| 840 |
------ |
|---|
| 841 |
foo(3, 4, 5); // first variadic argument is 5 |
|---|
| 842 |
|
|---|
| 843 |
int foo(int x, int y, ...) |
|---|
| 844 |
{ int z; |
|---|
| 845 |
|
|---|
| 846 |
z = *cast(int*)$(B _argptr); // z is set to 5 |
|---|
| 847 |
} |
|---|
| 848 |
------ |
|---|
| 849 |
|
|---|
| 850 |
To protect against the vagaries of stack layouts on different |
|---|
| 851 |
CPU architectures, use $(B std.c.stdarg) to access the variadic |
|---|
| 852 |
arguments: |
|---|
| 853 |
|
|---|
| 854 |
------ |
|---|
| 855 |
import $(B std.c.stdarg); |
|---|
| 856 |
------ |
|---|
| 857 |
|
|---|
| 858 |
<h3>D-style Variadic Functions</h3> |
|---|
| 859 |
|
|---|
| 860 |
Variadic functions with argument and type info are declared as taking |
|---|
| 861 |
a parameter of ... after the required function parameters. |
|---|
| 862 |
It has D linkage, and need not have any non-variadic parameters |
|---|
| 863 |
declared: |
|---|
| 864 |
|
|---|
| 865 |
------ |
|---|
| 866 |
int abc(char c, ...); // one required parameter: c |
|---|
| 867 |
int def(...); // ok |
|---|
| 868 |
------ |
|---|
| 869 |
|
|---|
| 870 |
These variadic functions have a special local variable declared for |
|---|
| 871 |
them, |
|---|
| 872 |
$(B _argptr), which is a $(TT void*) pointer to the first of the |
|---|
| 873 |
variadic |
|---|
| 874 |
arguments. To access the arguments, $(B _argptr) must be cast |
|---|
| 875 |
to a pointer to the expected argument type: |
|---|
| 876 |
|
|---|
| 877 |
------ |
|---|
| 878 |
foo(3, 4, 5); // first variadic argument is 5 |
|---|
| 879 |
|
|---|
| 880 |
int foo(int x, int y, ...) |
|---|
| 881 |
{ int z; |
|---|
| 882 |
|
|---|
| 883 |
z = *cast(int*)$(B _argptr); // z is set to 5 |
|---|
| 884 |
} |
|---|
| 885 |
------ |
|---|
| 886 |
|
|---|
| 887 |
An additional hidden argument |
|---|
| 888 |
with the name $(B _arguments) and type $(TT TypeInfo[]) |
|---|
| 889 |
is passed to the function. |
|---|
| 890 |
$(B _arguments) gives the number of arguments and the type |
|---|
| 891 |
of each, enabling the creation of typesafe variadic functions. |
|---|
| 892 |
|
|---|
| 893 |
------ |
|---|
| 894 |
import std.stdio; |
|---|
| 895 |
|
|---|
| 896 |
class Foo { int x = 3; } |
|---|
| 897 |
class Bar { long y = 4; } |
|---|
| 898 |
|
|---|
| 899 |
void printargs(int x, ...) |
|---|
| 900 |
{ |
|---|
| 901 |
writefln("%d arguments", $(B _arguments).length); |
|---|
| 902 |
for (int i = 0; i < $(B _arguments).length; i++) |
|---|
| 903 |
{ $(B _arguments)[i].print(); |
|---|
| 904 |
|
|---|
| 905 |
if ($(B _arguments)[i] == typeid(int)) |
|---|
| 906 |
{ |
|---|
| 907 |
int j = *cast(int *)_argptr; |
|---|
| 908 |
_argptr += int.sizeof; |
|---|
| 909 |
writefln("\t%d", j); |
|---|
| 910 |
} |
|---|
| 911 |
else if ($(B _arguments)[i] == typeid(long)) |
|---|
| 912 |
{ |
|---|
| 913 |
long j = *cast(long *)_argptr; |
|---|
| 914 |
_argptr += long.sizeof; |
|---|
| 915 |
writefln("\t%d", j); |
|---|
| 916 |
} |
|---|
| 917 |
else if ($(B _arguments)[i] == typeid(double)) |
|---|
| 918 |
{ |
|---|
| 919 |
double d = *cast(double *)_argptr; |
|---|
| 920 |
_argptr += double.sizeof; |
|---|
| 921 |
writefln("\t%g", d); |
|---|
| 922 |
} |
|---|
| 923 |
else if ($(B _arguments)[i] == typeid(Foo)) |
|---|
| 924 |
{ |
|---|
| 925 |
Foo f = *cast(Foo*)_argptr; |
|---|
| 926 |
_argptr += Foo.sizeof; |
|---|
| 927 |
writefln("\t%X", f); |
|---|
| 928 |
} |
|---|
| 929 |
else if ($(B _arguments)[i] == typeid(Bar)) |
|---|
| 930 |
{ |
|---|
| 931 |
Bar b = *cast(Bar*)_argptr; |
|---|
| 932 |
_argptr += Bar.sizeof; |
|---|
| 933 |
writefln("\t%X", b); |
|---|
| 934 |
} |
|---|
| 935 |
else |
|---|
| 936 |
assert(0); |
|---|
| 937 |
} |
|---|
| 938 |
} |
|---|
| 939 |
|
|---|
| 940 |
void main() |
|---|
| 941 |
{ |
|---|
| 942 |
Foo f = new Foo(); |
|---|
| 943 |
Bar b = new Bar(); |
|---|
| 944 |
|
|---|
| 945 |
writefln("%X", f); |
|---|
| 946 |
printargs(1, 2, 3L, 4.5, f, b); |
|---|
| 947 |
} |
|---|
| 948 |
------ |
|---|
| 949 |
|
|---|
| 950 |
which prints: |
|---|
| 951 |
|
|---|
| 952 |
------ |
|---|
| 953 |
00870FE0 |
|---|
| 954 |
5 arguments |
|---|
| 955 |
int |
|---|
| 956 |
2 |
|---|
| 957 |
long |
|---|
| 958 |
3 |
|---|
| 959 |
double |
|---|
| 960 |
4.5 |
|---|
| 961 |
Foo |
|---|
| 962 |
00870FE0 |
|---|
| 963 |
Bar |
|---|
| 964 |
00870FD0 |
|---|
| 965 |
------ |
|---|
| 966 |
|
|---|
| 967 |
To protect against the vagaries of stack layouts on different |
|---|
| 968 |
CPU architectures, use $(B std.stdarg) to access the variadic |
|---|
| 969 |
arguments: |
|---|
| 970 |
|
|---|
| 971 |
------ |
|---|
| 972 |
import std.stdio; |
|---|
| 973 |
import $(B std.stdarg); |
|---|
| 974 |
|
|---|
| 975 |
void foo(int x, ...) |
|---|
| 976 |
{ |
|---|
| 977 |
writefln("%d arguments", _arguments.length); |
|---|
| 978 |
for (int i = 0; i < _arguments.length; i++) |
|---|
| 979 |
{ _arguments[i].print(); |
|---|
| 980 |
|
|---|
| 981 |
if (_arguments[i] == typeid(int)) |
|---|
| 982 |
{ |
|---|
| 983 |
int j = $(B va_arg)!(int)(_argptr); |
|---|
| 984 |
writefln("\t%d", j); |
|---|
| 985 |
} |
|---|
| 986 |
else if (_arguments[i] == typeid(long)) |
|---|
| 987 |
{ |
|---|
| 988 |
long j = $(B va_arg)!(long)(_argptr); |
|---|
| 989 |
writefln("\t%d", j); |
|---|
| 990 |
} |
|---|
| 991 |
else if (_arguments[i] == typeid(double)) |
|---|
| 992 |
{ |
|---|
| 993 |
double d = $(B va_arg)!(double)(_argptr); |
|---|
| 994 |
writefln("\t%g", d); |
|---|
| 995 |
} |
|---|
| 996 |
else if (_arguments[i] == typeid(FOO)) |
|---|
| 997 |
{ |
|---|
| 998 |
FOO f = $(B va_arg)!(FOO)(_argptr); |
|---|
| 999 |
writefln("\t%X", f); |
|---|
| 1000 |
} |
|---|
| 1001 |
else |
|---|
| 1002 |
assert(0); |
|---|
| 1003 |
} |
|---|
| 1004 |
} |
|---|
| 1005 |
------ |
|---|
| 1006 |
|
|---|
| 1007 |
<h3>Typesafe Variadic Functions</h3> |
|---|
| 1008 |
|
|---|
| 1009 |
Typesafe variadic functions are used when the variable argument |
|---|
| 1010 |
portion of the arguments are used to construct an array or |
|---|
| 1011 |
class object. |
|---|
| 1012 |
<p> |
|---|
| 1013 |
|
|---|
| 1014 |
For arrays: |
|---|
| 1015 |
|
|---|
| 1016 |
------ |
|---|
| 1017 |
int test() |
|---|
| 1018 |
{ |
|---|
| 1019 |
return sum(1, 2, 3) + sum(); // returns 6+0 |
|---|
| 1020 |
} |
|---|
| 1021 |
|
|---|
| 1022 |
int func() |
|---|
| 1023 |
{ |
|---|
| 1024 |
int[3] ii = [4, 5, 6]; |
|---|
| 1025 |
return sum(ii); // returns 15 |
|---|
| 1026 |
} |
|---|
| 1027 |
|
|---|
| 1028 |
int sum(int[] ar ...) |
|---|
| 1029 |
{ |
|---|
| 1030 |
int s; |
|---|
| 1031 |
foreach (int x; ar) |
|---|
| 1032 |
s += x; |
|---|
| 1033 |
return s; |
|---|
| 1034 |
} |
|---|
| 1035 |
------ |
|---|
| 1036 |
|
|---|
| 1037 |
For static arrays: |
|---|
| 1038 |
|
|---|
| 1039 |
------ |
|---|
| 1040 |
int test() |
|---|
| 1041 |
{ |
|---|
| 1042 |
return sum(2, 3); // error, need 3 values for array |
|---|
| 1043 |
return sum(1, 2, 3); // returns 6 |
|---|
| 1044 |
} |
|---|
| 1045 |
|
|---|
| 1046 |
int func() |
|---|
| 1047 |
{ |
|---|
| 1048 |
int[3] ii = [4, 5, 6]; |
|---|
| 1049 |
int[] jj = ii; |
|---|
| 1050 |
return sum(ii); // returns 15 |
|---|
| 1051 |
return sum(jj); // error, type mismatch |
|---|
| 1052 |
} |
|---|
| 1053 |
|
|---|
| 1054 |
int sum(int[3] ar ...) |
|---|
| 1055 |
{ |
|---|
| 1056 |
int s; |
|---|
| 1057 |
foreach (int x; ar) |
|---|
| 1058 |
s += x; |
|---|
| 1059 |
return s; |
|---|
| 1060 |
} |
|---|
| 1061 |
------ |
|---|
| 1062 |
|
|---|
| 1063 |
For class objects: |
|---|
| 1064 |
|
|---|
| 1065 |
------ |
|---|
| 1066 |
class Foo |
|---|
| 1067 |
{ |
|---|
| 1068 |
int x; |
|---|
| 1069 |
char[] s; |
|---|
| 1070 |
|
|---|
| 1071 |
this(int x, char[] s) |
|---|
| 1072 |
{ |
|---|
| 1073 |
this.x = x; |
|---|
| 1074 |
this.s = s; |
|---|
| 1075 |
} |
|---|
| 1076 |
} |
|---|
| 1077 |
|
|---|
| 1078 |
void test(int x, Foo f ...); |
|---|
| 1079 |
|
|---|
| 1080 |
... |
|---|
| 1081 |
|
|---|
| 1082 |
Foo g = new Foo(3, "abc"); |
|---|
| 1083 |
test(1, g); // ok, since g is an instance of Foo |
|---|
| 1084 |
test(1, 4, "def"); // ok |
|---|
| 1085 |
test(1, 5); // error, no matching constructor for Foo |
|---|
| 1086 |
------ |
|---|
| 1087 |
|
|---|
| 1088 |
An implementation may construct the object or array instance |
|---|
| 1089 |
on the stack. Therefore, it is an error to refer to that |
|---|
| 1090 |
instance after the variadic function has returned: |
|---|
| 1091 |
|
|---|
| 1092 |
------ |
|---|
| 1093 |
Foo test(Foo f ...) |
|---|
| 1094 |
{ |
|---|
| 1095 |
return f; // error, f instance contents invalid after return |
|---|
| 1096 |
} |
|---|
| 1097 |
|
|---|
| 1098 |
int[] test(int[] a ...) |
|---|
| 1099 |
{ |
|---|
| 1100 |
return a; // error, array contents invalid after return |
|---|
| 1101 |
return a[0..1]; // error, array contents invalid after return |
|---|
| 1102 |
return a.dup; // ok, since copy is made |
|---|
| 1103 |
} |
|---|
| 1104 |
------ |
|---|
| 1105 |
|
|---|
| 1106 |
For other types, the argument is built with itself, as in: |
|---|
| 1107 |
|
|---|
| 1108 |
------ |
|---|
| 1109 |
int test(int i ...) |
|---|
| 1110 |
{ |
|---|
| 1111 |
return i; |
|---|
| 1112 |
} |
|---|
| 1113 |
|
|---|
| 1114 |
... |
|---|
| 1115 |
test(3); // returns 3 |
|---|
| 1116 |
test(3, 4); // error, too many arguments |
|---|
| 1117 |
int[] x; |
|---|
| 1118 |
test(x); // error, type mismatch |
|---|
| 1119 |
------ |
|---|
| 1120 |
|
|---|
| 1121 |
<h3>Lazy Variadic Functions</h3> |
|---|
| 1122 |
|
|---|
| 1123 |
$(P If the variadic parameter is an array of delegates |
|---|
| 1124 |
with no parameters: |
|---|
| 1125 |
) |
|---|
| 1126 |
|
|---|
| 1127 |
--- |
|---|
| 1128 |
void foo(int delegate()[] dgs ...); |
|---|
| 1129 |
--- |
|---|
| 1130 |
|
|---|
| 1131 |
$(P Then each of the arguments whose type does not match that |
|---|
| 1132 |
of the delegate is converted to a delegate. |
|---|
| 1133 |
) |
|---|
| 1134 |
|
|---|
| 1135 |
--- |
|---|
| 1136 |
int delegate() dg; |
|---|
| 1137 |
foo(1, 3+x, dg, cast(int delegate())null); |
|---|
| 1138 |
--- |
|---|
| 1139 |
|
|---|
| 1140 |
$(P is the same as:) |
|---|
| 1141 |
|
|---|
| 1142 |
--- |
|---|
| 1143 |
foo( { return 1; }, { return 3+x; }, dg, null ); |
|---|
| 1144 |
--- |
|---|
| 1145 |
|
|---|
| 1146 |
<h2>Local Variables</h2> |
|---|
| 1147 |
|
|---|
| 1148 |
$(P It is an error to use a local variable without first assigning it a |
|---|
| 1149 |
value. The implementation may not always be able to detect these |
|---|
| 1150 |
cases. Other language compilers sometimes issue a warning for this, |
|---|
| 1151 |
but since it is always a bug, it should be an error. |
|---|
| 1152 |
) |
|---|
| 1153 |
|
|---|
| 1154 |
$(P It is an error to declare a local variable that is never referred to. |
|---|
| 1155 |
Dead variables, like anachronistic dead code, are just a source of |
|---|
| 1156 |
confusion for maintenance programmers. |
|---|
| 1157 |
) |
|---|
| 1158 |
|
|---|
| 1159 |
$(P It is an error to declare a local variable that hides another local |
|---|
| 1160 |
variable in the same function: |
|---|
| 1161 |
) |
|---|
| 1162 |
|
|---|
| 1163 |
------ |
|---|
| 1164 |
void func(int x) |
|---|
| 1165 |
{ int x; // error, hides previous definition of x |
|---|
| 1166 |
double y; |
|---|
| 1167 |
... |
|---|
| 1168 |
{ char y; // error, hides previous definition of y |
|---|
| 1169 |
int z; |
|---|
| 1170 |
} |
|---|
| 1171 |
{ wchar z; // legal, previous z is out of scope |
|---|
| 1172 |
} |
|---|
| 1173 |
} |
|---|
| 1174 |
------ |
|---|
| 1175 |
|
|---|
| 1176 |
$(P While this might look unreasonable, in practice whenever |
|---|
| 1177 |
this is done it either is a |
|---|
| 1178 |
bug or at least looks like a bug. |
|---|
| 1179 |
) |
|---|
| 1180 |
|
|---|
| 1181 |
$(P It is an error to return the address of or a reference to a |
|---|
| 1182 |
local variable. |
|---|
| 1183 |
) |
|---|
| 1184 |
|
|---|
| 1185 |
$(P It is an error to have a local variable and a label with the same |
|---|
| 1186 |
name. |
|---|
| 1187 |
) |
|---|
| 1188 |
|
|---|
| 1189 |
<h2><a name="nested">Nested Functions</a></h2> |
|---|
| 1190 |
|
|---|
| 1191 |
$(P Functions may be nested within other functions:) |
|---|
| 1192 |
|
|---|
| 1193 |
------ |
|---|
| 1194 |
int bar(int a) |
|---|
| 1195 |
{ |
|---|
| 1196 |
int foo(int b) |
|---|
| 1197 |
{ |
|---|
| 1198 |
int abc() { return 1; } |
|---|
| 1199 |
|
|---|
| 1200 |
return b + abc(); |
|---|
| 1201 |
} |
|---|
| 1202 |
return foo(a); |
|---|
| 1203 |
} |
|---|
| 1204 |
|
|---|
| 1205 |
void test() |
|---|
| 1206 |
{ |
|---|
| 1207 |
int i = bar(3); // i is assigned 4 |
|---|
| 1208 |
} |
|---|
| 1209 |
------ |
|---|
| 1210 |
|
|---|
| 1211 |
$(P Nested functions can be accessed only if the name is in scope.) |
|---|
| 1212 |
|
|---|
| 1213 |
------ |
|---|
| 1214 |
void foo() |
|---|
| 1215 |
{ |
|---|
| 1216 |
void A() |
|---|
| 1217 |
{ |
|---|
| 1218 |
B(); // error, B() is forward referenced |
|---|
| 1219 |
C(); // error, C undefined |
|---|
| 1220 |
} |
|---|
| 1221 |
void B() |
|---|
| 1222 |
{ |
|---|
| 1223 |
A(); // ok, in scope |
|---|
| 1224 |
void C() |
|---|
| 1225 |
{ |
|---|
| 1226 |
void D() |
|---|
| 1227 |
{ |
|---|
| 1228 |
A(); // ok |
|---|
| 1229 |
B(); // ok |
|---|
| 1230 |
C(); // ok |
|---|
| 1231 |
D(); // ok |
|---|
| 1232 |
} |
|---|
| 1233 |
} |
|---|
| 1234 |
} |
|---|
| 1235 |
A(); // ok |
|---|
| 1236 |
B(); // ok |
|---|
| 1237 |
C(); // error, C undefined |
|---|
| 1238 |
} |
|---|
| 1239 |
------ |
|---|
| 1240 |
|
|---|
| 1241 |
$(P and:) |
|---|
| 1242 |
|
|---|
| 1243 |
------ |
|---|
| 1244 |
int bar(int a) |
|---|
| 1245 |
{ |
|---|
| 1246 |
int foo(int b) { return b + 1; } |
|---|
| 1247 |
int abc(int b) { return foo(b); } // ok |
|---|
| 1248 |
return foo(a); |
|---|
| 1249 |
} |
|---|
| 1250 |
|
|---|
| 1251 |
void test() |
|---|
| 1252 |
{ |
|---|
| 1253 |
int i = bar(3); // ok |
|---|
| 1254 |
int j = bar.foo(3); // error, bar.foo not visible |
|---|
| 1255 |
} |
|---|
| 1256 |
------ |
|---|
| 1257 |
|
|---|
| 1258 |
$(P Nested functions have access to the variables and other symbols |
|---|
| 1259 |
defined by the lexically enclosing function. |
|---|
| 1260 |
This access includes both the ability to read and write them. |
|---|
| 1261 |
) |
|---|
| 1262 |
|
|---|
| 1263 |
------ |
|---|
| 1264 |
int bar(int a) |
|---|
| 1265 |
{ int c = 3; |
|---|
| 1266 |
|
|---|
| 1267 |
int foo(int b) |
|---|
| 1268 |
{ |
|---|
| 1269 |
b += c; // 4 is added to b |
|---|
| 1270 |
c++; // bar.c is now 5 |
|---|
| 1271 |
return b + c; // 12 is returned |
|---|
| 1272 |
} |
|---|
| 1273 |
c = 4; |
|---|
| 1274 |
int i = foo(a); // i is set to 12 |
|---|
| 1275 |
return i + c; // returns 17 |
|---|
| 1276 |
} |
|---|
| 1277 |
|
|---|
| 1278 |
void test() |
|---|
| 1279 |
{ |
|---|
| 1280 |
int i = bar(3); // i is assigned 17 |
|---|
| 1281 |
} |
|---|
| 1282 |
------ |
|---|
| 1283 |
|
|---|
| 1284 |
$(P This access can span multiple nesting levels:) |
|---|
| 1285 |
|
|---|
| 1286 |
------ |
|---|
| 1287 |
int bar(int a) |
|---|
| 1288 |
{ int c = 3; |
|---|
| 1289 |
|
|---|
| 1290 |
int foo(int b) |
|---|
| 1291 |
{ |
|---|
| 1292 |
int abc() |
|---|
| 1293 |
{ |
|---|
| 1294 |
return c; // access bar.c |
|---|
| 1295 |
} |
|---|
| 1296 |
return b + c + abc(); |
|---|
| 1297 |
} |
|---|
| 1298 |
return foo(3); |
|---|
| 1299 |
} |
|---|
| 1300 |
------ |
|---|
| 1301 |
|
|---|
| 1302 |
$(P Static nested functions cannot access any stack variables of |
|---|
| 1303 |
any lexically enclosing function, but can access static variables. |
|---|
| 1304 |
This is analogous to how static member functions behave. |
|---|
| 1305 |
) |
|---|
| 1306 |
|
|---|
| 1307 |
------ |
|---|
| 1308 |
int bar(int a) |
|---|
| 1309 |
{ int c; |
|---|
| 1310 |
static int d; |
|---|
| 1311 |
|
|---|
| 1312 |
static int foo(int b) |
|---|
| 1313 |
{ |
|---|
| 1314 |
b = d; // ok |
|---|
| 1315 |
b = c; // error, foo() cannot access frame of bar() |
|---|
| 1316 |
return b + 1; |
|---|
| 1317 |
} |
|---|
| 1318 |
return foo(a); |
|---|
| 1319 |
} |
|---|
| 1320 |
------ |
|---|
| 1321 |
|
|---|
| 1322 |
$(P Functions can be nested within member functions:) |
|---|
| 1323 |
|
|---|
| 1324 |
------ |
|---|
| 1325 |
struct Foo |
|---|
| 1326 |
{ int a; |
|---|
| 1327 |
|
|---|
| 1328 |
int bar() |
|---|
| 1329 |
{ int c; |
|---|
| 1330 |
|
|---|
| 1331 |
int foo() |
|---|
| 1332 |
{ |
|---|
| 1333 |
return c + a; |
|---|
| 1334 |
} |
|---|
| 1335 |
return 0; |
|---|
| 1336 |
} |
|---|
| 1337 |
} |
|---|
| 1338 |
------ |
|---|
| 1339 |
|
|---|
| 1340 |
$(P Member functions of nested classes and structs do not have |
|---|
| 1341 |
access to the stack variables of the enclosing function, but |
|---|
| 1342 |
do have access to the other symbols: |
|---|
| 1343 |
) |
|---|
| 1344 |
|
|---|
| 1345 |
------ |
|---|
| 1346 |
void test() |
|---|
| 1347 |
{ int j; |
|---|
| 1348 |
static int s; |
|---|
| 1349 |
|
|---|
| 1350 |
struct Foo |
|---|
| 1351 |
{ int a; |
|---|
| 1352 |
|
|---|
| 1353 |
int bar() |
|---|
| 1354 |
{ int c = s; // ok, s is static |
|---|
| 1355 |
int d = j; // error, no access to frame of test() |
|---|
| 1356 |
|
|---|
| 1357 |
int foo() |
|---|
| 1358 |
{ |
|---|
| 1359 |
int e = s; // ok, s is static |
|---|
| 1360 |
int f = j; // error, no access to frame of test() |
|---|
| 1361 |
return c + a; // ok, frame of bar() is accessible, |
|---|
| 1362 |
// so are members of Foo accessible via |
|---|
| 1363 |
// the 'this' pointer to Foo.bar() |
|---|
| 1364 |
} |
|---|
| 1365 |
|
|---|
| 1366 |
return 0; |
|---|
| 1367 |
} |
|---|
| 1368 |
} |
|---|
| 1369 |
} |
|---|
| 1370 |
------ |
|---|
| 1371 |
|
|---|
| 1372 |
$(P Nested functions always have the D function linkage type. |
|---|
| 1373 |
) |
|---|
| 1374 |
|
|---|
| 1375 |
$(P Unlike module level declarations, declarations within function |
|---|
| 1376 |
scope are processed in order. This means that two nested functions |
|---|
| 1377 |
cannot mutually call each other: |
|---|
| 1378 |
) |
|---|
| 1379 |
|
|---|
| 1380 |
------ |
|---|
| 1381 |
void test() |
|---|
| 1382 |
{ |
|---|
| 1383 |
void foo() { bar(); } // error, bar not defined |
|---|
| 1384 |
void bar() { foo(); } // ok |
|---|
| 1385 |
} |
|---|
| 1386 |
------ |
|---|
| 1387 |
|
|---|
| 1388 |
$(P The solution is to use a delegate:) |
|---|
| 1389 |
|
|---|
| 1390 |
------ |
|---|
| 1391 |
void test() |
|---|
| 1392 |
{ |
|---|
| 1393 |
void delegate() fp; |
|---|
| 1394 |
void foo() { fp(); } |
|---|
| 1395 |
void bar() { foo(); } |
|---|
| 1396 |
fp = &bar; |
|---|
| 1397 |
} |
|---|
| 1398 |
------ |
|---|
| 1399 |
|
|---|
| 1400 |
$(P $(B Future directions:) This restriction may be removed.) |
|---|
| 1401 |
|
|---|
| 1402 |
|
|---|
| 1403 |
<h3><a name="closures">Delegates, Function Pointers, and $(V1 Dynamic) Closures</a></h3> |
|---|
| 1404 |
|
|---|
| 1405 |
$(P A function pointer can point to a static nested function:) |
|---|
| 1406 |
|
|---|
| 1407 |
------ |
|---|
| 1408 |
int function() fp; |
|---|
| 1409 |
|
|---|
| 1410 |
void test() |
|---|
| 1411 |
{ static int a = 7; |
|---|
| 1412 |
static int foo() { return a + 3; } |
|---|
| 1413 |
|
|---|
| 1414 |
fp = &foo; |
|---|
| 1415 |
} |
|---|
| 1416 |
|
|---|
| 1417 |
void bar() |
|---|
| 1418 |
{ |
|---|
| 1419 |
test(); |
|---|
| 1420 |
int i = fp(); // i is set to 10 |
|---|
| 1421 |
} |
|---|
| 1422 |
------ |
|---|
| 1423 |
|
|---|
| 1424 |
$(P A delegate can be set to a non-static nested function:) |
|---|
| 1425 |
|
|---|
| 1426 |
------ |
|---|
| 1427 |
int delegate() dg; |
|---|
| 1428 |
|
|---|
| 1429 |
void test() |
|---|
| 1430 |
{ int a = 7; |
|---|
| 1431 |
int foo() { return a + 3; } |
|---|
| 1432 |
|
|---|
| 1433 |
dg = &foo; |
|---|
| 1434 |
int i = dg(); // i is set to 10 |
|---|
| 1435 |
} |
|---|
| 1436 |
------ |
|---|
| 1437 |
|
|---|
| 1438 |
$(V1 |
|---|
| 1439 |
$(P The stack variables, however, are not valid once the function |
|---|
| 1440 |
declaring them has exited, in the same manner that pointers to |
|---|
| 1441 |
stack variables are not valid upon exit from a function: |
|---|
| 1442 |
) |
|---|
| 1443 |
|
|---|
| 1444 |
------ |
|---|
| 1445 |
int* bar() |
|---|
| 1446 |
{ int b; |
|---|
| 1447 |
test(); |
|---|
| 1448 |
int i = dg(); // error, test.a no longer exists |
|---|
| 1449 |
return &b; // error, bar.b not valid after bar() exits |
|---|
| 1450 |
} |
|---|
| 1451 |
------ |
|---|
| 1452 |
) |
|---|
| 1453 |
$(V2 |
|---|
| 1454 |
$(P The stack variables referenced by a nested function are |
|---|
| 1455 |
still valid even after the function exits (this is different |
|---|
| 1456 |
from D 1.0). This is called a $(I closure). |
|---|
| 1457 |
Returning addresses of stack variables, however, is not |
|---|
| 1458 |
a closure and is an error. |
|---|
| 1459 |
) |
|---|
| 1460 |
|
|---|
| 1461 |
------ |
|---|
| 1462 |
int* bar() |
|---|
| 1463 |
{ int b; |
|---|
| 1464 |
test(); |
|---|
| 1465 |
int i = dg(); // ok, test.a is in a closure and still exists |
|---|
| 1466 |
return &b; // error, bar.b not valid after bar() exits |
|---|
| 1467 |
} |
|---|
| 1468 |
------ |
|---|
| 1469 |
) |
|---|
| 1470 |
|
|---|
| 1471 |
$(P Delegates to non-static nested functions contain two pieces of |
|---|
| 1472 |
data: the pointer to the stack frame of the lexically enclosing |
|---|
| 1473 |
function (called the $(I frame pointer)) and the address of the |
|---|
| 1474 |
function. This is analogous to struct/class non-static member |
|---|
| 1475 |
function delegates consisting of a $(I this) pointer and |
|---|
| 1476 |
the address of the member function. |
|---|
| 1477 |
Both forms of delegates are interchangeable, and are actually |
|---|
| 1478 |
the same type: |
|---|
| 1479 |
) |
|---|
| 1480 |
|
|---|
| 1481 |
------ |
|---|
| 1482 |
struct Foo |
|---|
| 1483 |
{ int a = 7; |
|---|
| 1484 |
int bar() { return a; } |
|---|
| 1485 |
} |
|---|
| 1486 |
|
|---|
| 1487 |
int foo(int delegate() dg) |
|---|
| 1488 |
{ |
|---|
| 1489 |
return dg() + 1; |
|---|
| 1490 |
} |
|---|
| 1491 |
|
|---|
| 1492 |
void test() |
|---|
| 1493 |
{ |
|---|
| 1494 |
int x = 27; |
|---|
| 1495 |
int abc() { return x; } |
|---|
| 1496 |
Foo f; |
|---|
| 1497 |
int i; |
|---|
| 1498 |
|
|---|
| 1499 |
i = foo(&abc); // i is set to 28 |
|---|
| 1500 |
i = foo(&f.bar); // i is set to 8 |
|---|
| 1501 |
} |
|---|
| 1502 |
------ |
|---|
| 1503 |
|
|---|
| 1504 |
$(P This combining of the environment and the function is called |
|---|
| 1505 |
a $(I dynamic closure). |
|---|
| 1506 |
) |
|---|
| 1507 |
|
|---|
| 1508 |
$(P The $(B .ptr) property of a delegate will return the |
|---|
| 1509 |
$(I frame pointer) value as a $(TT void*). |
|---|
| 1510 |
) |
|---|
| 1511 |
|
|---|
| 1512 |
$(P The $(B .funcptr) property of a delegate will return the |
|---|
| 1513 |
$(I function pointer) value as a function type. |
|---|
| 1514 |
) |
|---|
| 1515 |
|
|---|
| 1516 |
$(P $(B Future directions:) Function pointers and delegates may merge |
|---|
| 1517 |
into a common syntax and be interchangeable with each other. |
|---|
| 1518 |
) |
|---|
| 1519 |
|
|---|
| 1520 |
<h3>Anonymous Functions and Anonymous Delegates</h3> |
|---|
| 1521 |
|
|---|
| 1522 |
$(P See $(LINK2 expression.html#FunctionLiteral, Function Literals). |
|---|
| 1523 |
) |
|---|
| 1524 |
|
|---|
| 1525 |
<h2>main() Function</h2> |
|---|
| 1526 |
|
|---|
| 1527 |
$(P For console programs, $(TT main()) serves as the entry point. |
|---|
| 1528 |
It gets called after all the module initializers are run, and |
|---|
| 1529 |
after any unittests are run. |
|---|
| 1530 |
After it returns, all the module destructors are run. |
|---|
| 1531 |
$(TT main()) must be declared using one of the following forms: |
|---|
| 1532 |
) |
|---|
| 1533 |
|
|---|
| 1534 |
---- |
|---|
| 1535 |
void main() { ... } |
|---|
| 1536 |
void main(char[][] args) { ... } |
|---|
| 1537 |
int main() { ... } |
|---|
| 1538 |
int main(char[][] args) { ... } |
|---|
| 1539 |
---- |
|---|
| 1540 |
|
|---|
| 1541 |
<h2>$(LNAME2 interpretation, Compile Time Function Execution (CTFE))</h2> |
|---|
| 1542 |
|
|---|
| 1543 |
$(P A subset of functions can be executed at compile time. |
|---|
| 1544 |
This is useful when constant folding algorithms need to |
|---|
| 1545 |
include recursion and looping. |
|---|
| 1546 |
In order to be executed at compile time, a function must |
|---|
| 1547 |
meet the following criteria: |
|---|
| 1548 |
) |
|---|
| 1549 |
|
|---|
| 1550 |
$(OL |
|---|
| 1551 |
|
|---|
| 1552 |
$(LI function arguments must all be: |
|---|
| 1553 |
$(UL |
|---|
| 1554 |
$(LI integer literals) |
|---|
| 1555 |
$(LI floating point literals) |
|---|
| 1556 |
$(LI character literals) |
|---|
| 1557 |
$(LI string literals) |
|---|
| 1558 |
$(LI array literals where the members are all items |
|---|
| 1559 |
in this list) |
|---|
| 1560 |
$(LI associative array literals where the members are all items |
|---|
| 1561 |
in this list) |
|---|
| 1562 |
$(LI struct literals where the members are all items |
|---|
| 1563 |
in this list) |
|---|
| 1564 |
$(LI const variables initialized with a member of |
|---|
| 1565 |
this list) |
|---|
| 1566 |
$(LI delegates) |
|---|
| 1567 |
$(LI pointers to functions) |
|---|
| 1568 |
$(LI delegate literals) |
|---|
| 1569 |
$(LI function literals) |
|---|
| 1570 |
) |
|---|
| 1571 |
) |
|---|
| 1572 |
|
|---|
| 1573 |
$(LI function parameters may not be C-style variadic) |
|---|
| 1574 |
|
|---|
| 1575 |
$(LI the function may not be synchronized) |
|---|
| 1576 |
|
|---|
| 1577 |
$(LI expressions in the function may not: |
|---|
| 1578 |
$(UL |
|---|
| 1579 |
$(LI throw exceptions) |
|---|
| 1580 |
$(LI use pointers or classes) |
|---|
| 1581 |
$(LI reference any global state or variables) |
|---|
| 1582 |
$(LI reference any local static variables) |
|---|
| 1583 |
$(LI delete) |
|---|
| 1584 |
$(LI call any function that is not |
|---|
| 1585 |
executable at compile time) |
|---|
| 1586 |
) |
|---|
| 1587 |
) |
|---|
| 1588 |
|
|---|
| 1589 |
$(LI the following statement types are not allowed: |
|---|
| 1590 |
$(UL |
|---|
| 1591 |
$(LI synchronized statements) |
|---|
| 1592 |
$(LI throw statements) |
|---|
| 1593 |
$(LI with statements) |
|---|
| 1594 |
$(LI scope statements) |
|---|
| 1595 |
$(LI try-catch-finally statements) |
|---|
| 1596 |
$(LI labeled break and continue statements) |
|---|
| 1597 |
) |
|---|
| 1598 |
) |
|---|
| 1599 |
|
|---|
| 1600 |
$(LI as a special case, the following properties |
|---|
| 1601 |
can be executed at compile time: |
|---|
| 1602 |
$(TABLE1 |
|---|
| 1603 |
$(TR $(TD $(CODE .dup))) |
|---|
| 1604 |
$(TR $(TD $(CODE .length))) |
|---|
| 1605 |
$(TR $(TD $(CODE .keys))) |
|---|
| 1606 |
$(TR $(TD $(CODE .values))) |
|---|
| 1607 |
) |
|---|
| 1608 |
) |
|---|
| 1609 |
|
|---|
| 1610 |
) |
|---|
| 1611 |
|
|---|
| 1612 |
$(P In order to be executed at compile time, the function |
|---|
| 1613 |
must appear in a context where it must be so executed, for |
|---|
| 1614 |
example:) |
|---|
| 1615 |
|
|---|
| 1616 |
$(UL |
|---|
| 1617 |
$(LI initialization of a static variable) |
|---|
| 1618 |
$(LI dimension of a static array) |
|---|
| 1619 |
$(LI argument for a template value parameter) |
|---|
| 1620 |
) |
|---|
| 1621 |
|
|---|
| 1622 |
--- |
|---|
| 1623 |
template eval( A... ) |
|---|
| 1624 |
{ |
|---|
| 1625 |
const typeof(A[0]) eval = A[0]; |
|---|
| 1626 |
} |
|---|
| 1627 |
|
|---|
| 1628 |
int square(int i) { return i * i; } |
|---|
| 1629 |
|
|---|
| 1630 |
void foo() |
|---|
| 1631 |
{ |
|---|
| 1632 |
static j = square(3); // compile time |
|---|
| 1633 |
writefln(j); |
|---|
| 1634 |
writefln(square(4)); // run time |
|---|
| 1635 |
writefln(eval!(square(5))); // compile time |
|---|
| 1636 |
} |
|---|
| 1637 |
--- |
|---|
| 1638 |
|
|---|
| 1639 |
$(P Executing functions at compile time can take considerably |
|---|
| 1640 |
longer than executing it at run time. |
|---|
| 1641 |
If the function goes into an infinite loop, it will hang at |
|---|
| 1642 |
compile time (rather than hanging at run time). |
|---|
| 1643 |
) |
|---|
| 1644 |
|
|---|
| 1645 |
$(P Functions executed at compile time can give different results |
|---|
| 1646 |
from run time in the following scenarios: |
|---|
| 1647 |
) |
|---|
| 1648 |
|
|---|
| 1649 |
$(UL |
|---|
| 1650 |
|
|---|
| 1651 |
$(LI floating point computations may be done at a higher |
|---|
| 1652 |
precision than run time) |
|---|
| 1653 |
$(LI dependency on implementation defined order of evaluation) |
|---|
| 1654 |
$(LI use of uninitialized variables) |
|---|
| 1655 |
|
|---|
| 1656 |
) |
|---|
| 1657 |
|
|---|
| 1658 |
$(P These are the same kinds of scenarios where different |
|---|
| 1659 |
optimization settings affect the results.) |
|---|
| 1660 |
|
|---|
| 1661 |
<h3>String Mixins and Compile Time Function Execution</h3> |
|---|
| 1662 |
|
|---|
| 1663 |
$(P Any functions that execute at compile time must also |
|---|
| 1664 |
be executable at run time. The compile time evaluation of |
|---|
| 1665 |
a function does the equivalent of running the function at |
|---|
| 1666 |
run time. This means that the semantics of a function cannot |
|---|
| 1667 |
depend on compile time values of the function. For example:) |
|---|
| 1668 |
|
|---|
| 1669 |
--- |
|---|
| 1670 |
int foo(char[] s) |
|---|
| 1671 |
{ |
|---|
| 1672 |
return mixin(s); |
|---|
| 1673 |
} |
|---|
| 1674 |
|
|---|
| 1675 |
const int x = foo("1"); |
|---|
| 1676 |
--- |
|---|
| 1677 |
|
|---|
| 1678 |
$(P is illegal, because the runtime code for foo() cannot be |
|---|
| 1679 |
generated. A function template would be the appropriate |
|---|
| 1680 |
method to implement this sort of thing.) |
|---|
| 1681 |
|
|---|
| 1682 |
$(V2 |
|---|
| 1683 |
<h2>$(LNAME2 function-safety, Function Safety)</h2> |
|---|
| 1684 |
|
|---|
| 1685 |
$(P $(I Safe functions) are functions that are statically checked |
|---|
| 1686 |
to exhibit no possibility of |
|---|
| 1687 |
$(LINK2 glossary.html#undefined_behavior, $(I undefined behavior)). |
|---|
| 1688 |
Undefined behavior is often used as a vector for malicious |
|---|
| 1689 |
attacks. |
|---|
| 1690 |
) |
|---|
| 1691 |
|
|---|
| 1692 |
<h3>$(LNAME2 safe-functions, Safe Functions)</h3> |
|---|
| 1693 |
|
|---|
| 1694 |
$(P Safe functions are marked with the $(CODE @safe) attribute.) |
|---|
| 1695 |
|
|---|
| 1696 |
$(P The following operations are not allowed in safe |
|---|
| 1697 |
functions:) |
|---|
| 1698 |
|
|---|
| 1699 |
$(UL |
|---|
| 1700 |
$(LI No casting from a pointer type to any type other than $(CODE void*).) |
|---|
| 1701 |
$(LI No casting from any non-pointer type to a pointer type.) |
|---|
| 1702 |
$(LI No modification of pointer values.) |
|---|
| 1703 |
$(LI Cannot access unions that have pointers or references overlapping |
|---|
| 1704 |
with other types.) |
|---|
| 1705 |
$(LI Calling any system functions.) |
|---|
| 1706 |
$(LI No catching of exceptions that are not derived from $(CODE class Exception).) |
|---|
| 1707 |
$(LI No inline assembler.) |
|---|
| 1708 |
$(LI No explicit casting of mutable objects to immutable.) |
|---|
| 1709 |
$(LI No explicit casting of immutable objects to mutable.) |
|---|
| 1710 |
$(LI No explicit casting of thread local objects to shared.) |
|---|
| 1711 |
$(LI No explicit casting of shared objects to thread local.) |
|---|
| 1712 |
$(LI No taking the address of a local variable or function parameter.) |
|---|
| 1713 |
$(LI Cannot access $(D_KEYWORD __gshared) variables.) |
|---|
| 1714 |
) |
|---|
| 1715 |
|
|---|
| 1716 |
$(P Functions nested inside safe functions default to being |
|---|
| 1717 |
safe functions. |
|---|
| 1718 |
) |
|---|
| 1719 |
|
|---|
| 1720 |
$(P Safe functions are covariant with trusted or system functions.) |
|---|
| 1721 |
|
|---|
| 1722 |
$(P $(B Note:) The verifiable safety of functions may be compromised by |
|---|
| 1723 |
bugs in the compiler and specification. Please report all such errors |
|---|
| 1724 |
so they can be corrected. |
|---|
| 1725 |
) |
|---|
| 1726 |
|
|---|
| 1727 |
<h3>$(LNAME2 trusted-functions, Trusted Functions)</h3> |
|---|
| 1728 |
|
|---|
| 1729 |
$(P Trusted functions are marked with the $(CODE @trusted) attribute.) |
|---|
| 1730 |
|
|---|
| 1731 |
$(P Trusted functions are guaranteed by the programmer to not exhibit |
|---|
| 1732 |
any undefined behavior if called by a safe function. |
|---|
| 1733 |
Generally, trusted functions should be kept small so that they are |
|---|
| 1734 |
easier to manually verify. |
|---|
| 1735 |
) |
|---|
| 1736 |
|
|---|
| 1737 |
$(P Trusted functions may call safe, trusted, or system functions. |
|---|
| 1738 |
) |
|---|
| 1739 |
|
|---|
| 1740 |
$(P Trusted functions are covariant with safe or system functions.) |
|---|
| 1741 |
|
|---|
| 1742 |
<h3>$(LNAME2 system-functions, System Functions)</h3> |
|---|
| 1743 |
|
|---|
| 1744 |
$(P System functions are functions not marked with $(CODE @safe) or |
|---|
| 1745 |
$(CODE @trusted) |
|---|
| 1746 |
and are not nested inside $(CODE @safe) functions. |
|---|
| 1747 |
System functions may be marked with the $(CODE @system) attribute. |
|---|
| 1748 |
A function being system does not mean it actually is unsafe, it just |
|---|
| 1749 |
means that the compiler is unable to verify that it cannot exhibit |
|---|
| 1750 |
undefined behavior. |
|---|
| 1751 |
) |
|---|
| 1752 |
|
|---|
| 1753 |
$(P System functions are $(B not) covariant with trusted or safe functions. |
|---|
| 1754 |
) |
|---|
| 1755 |
) |
|---|
| 1756 |
|
|---|
| 1757 |
) |
|---|
| 1758 |
|
|---|
| 1759 |
Macros: |
|---|
| 1760 |
TITLE=Functions |
|---|
| 1761 |
WIKI=Function |
|---|