Contents
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
a local clone.
Legacy Code
To maintain compatibility with older D code, many legacy features remain supported. This page describes each legacy feature that is supported, with a suggestion of how to modernize the code.
Feature | Summary |
---|---|
body keyword | body after a contract statement - use do instead |
alias target first syntax | use alias name = target instead. |
Struct/union postblit | use a copy constructor instead. |
Aliasing an instance member | use typeof(instance).member instead. |
body keyword
body was a keyword used to specify a function/method's body after a contract statement:
class Foo { void bar(int i) in { assert(i >= 42); } body { /* Do something interesting */ } string method(string s) out(v) { assert(v.length == s.length); } body { /* Do something even more interesting */ } void noBody() { /* No contracts, no body */ } }
Corrective Action
Use the do keyword instead (introduced in v2.075.0):
void bar(int i) in { assert(i >= 42); } do { /* Look ma, no body! */ }
Rationale: The body keyword was only used for this single purpose.
Since D grammar aims to be context free, this common word was reserved,
which led to frequent trouble for people interfacing with other languages
(e.g. javascript) or auto-generating code.
Aliasing an instance member
E.g. alias a = instance.field;. Such an alias actually aliases a member of the instance's type, not the instance member itself. That could be confusing. Instead, alias a member of the type.
Note: Aliasing this.member and super.member are valid inside
methods.
struct Bar { Foo f; alias v = f.v; // Error, use `typeof(f).v` } struct Foo { int v; void test(Foo that) const { alias a = this.v; // OK alias b = that.v; // Error, use `typeof(that).v` instead assert(&a is &b); // passes assert(&b !is &that.v); } }
Copyright © 1999-2024 by the D Language Foundation | Page generated by
Ddoc on Wed Dec 18 09:00:37 2024