Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Language Reference

table of 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 usinga local clone.

Classes

Contents
  1. Overview
  2. Class Declarations
    1. Access Control
    2. Super Class
    3. Fields
  3. Class Properties
    1. .tupleof
    2. Accessing Hidden Fields
    3. Field Properties
  4. Member Functions (a.k.a. Methods)
    1. Objective-C linkage
  5. Synchronized Method Calls
    1. Synchronized Classes
  6. Class Instantiation
    1. Constructors
    2. Base Class Construction
    3. Delegating Constructors
    4. Restrictions
    5. Implicit Base Class Construction
    6. Instantiation Process
    7. Constructor Attributes
    8. Field initialization inside a constructor
  7. Destructors
  8. Static Constructors and Destructors
    1. Static Constructors
    2. Static Destructors
    3. Shared Static Constructors
    4. Shared Static Destructors
  9. Class Invariants
  10. Scope Classes
  11. Abstract Classes
  12. Final Classes
  13. Nested Classes
    1. Static Nested Classes
    2. Context Pointer
    3. Explicit Instantiation
    4. outer Property
    5. Anonymous Nested Classes
  14. Const, Immutable and Shared Classes

Overview

The object-oriented features of D all come from classes. The class hierarchy has as its root the class Object. Object defines a minimum level of functionality that each derived class has, and a default implementation for that functionality.

Classes are programmer defined types. Support for classes are what make D an object oriented language, giving it encapsulation, inheritance, and polymorphism. D classes support the single inheritance paradigm, extended by adding support for interfaces. Class objects are instantiated by reference only.

A class can be exported, which means its name and all its non-private members are exposed externally to the DLL or EXE.

Class Declarations

A class declaration is defined:

ClassDeclaration:classIdentifier;classIdentifierBaseClassListoptAggregateBodyClassTemplateDeclaration
BaseClassList::SuperClassOrInterface:SuperClassOrInterface,Interfaces
SuperClassOrInterface:BasicType
Interfaces:InterfaceInterface,Interfaces
Interface:BasicType

A class consists of:

A class is defined:

class Foo{    ... members ...}
Note: Unlike C++, there is no trailing; after the closing} of the class definition. It is also not possible to declare a variablevar inline:
class Foo { } var;
Instead, use:
class Foo { }Foo var;

Access Control

Access to class members is controlled using visibility attributes. The default visibility attribute ispublic.

Super Class

All classes inherit from a super class. If one is not specified, a class inherits fromObject.Object forms the root of the D class inheritance hierarchy.

class A { }// A inherits from Objectclass B : A { }// B inherits from A

Multiple class inheritance is not supported, however a class can inherit from multipleinterfaces. If a super class is declared, it must come before any interfaces. Commas are used to separate inherited types.

Fields

Non-static member variables are called fields. Members of a class instance are accessed with the. operator.

Members of a base class can be accessed by prepending the name of the base class followed by a dot:

class A {int a;int a2;}class B : A {int a; }void foo(B b){    b.a = 3;// accesses field B.a    b.a2 = 4;// accesses field A.a2    b.A.a = 5;// accesses field A.a}
Implementation Defined: The D compiler is free to rearrange the order of fields in a class to optimally pack them. Consider the fields much like the local variables in a function - the compiler assigns some to registers and shuffles others around all to get the optimal stack frame layout. This frees the code designer to organize the fields in a manner that makes the code more readable rather than being forced to organize it according to machine optimization rules. Explicit control of field layout is provided by struct/union types, not classes.

Fields ofextern(Objective-C) classes have a dynamic offset. That means that the base class can change (add or remove instance variables) without the subclasses needing to recompile or relink.

Class Properties

Class Properties
PropertyDescription
.tupleof Symbol sequence of fields in the class.
Class Instance Properties
PropertyDescription
.classinfo Information about the dynamic type of the class.
.outerFor a nested class instance, provides either the parent class instance, or the parent function's context pointer when there is no parent class.

.tupleof

The.tupleof property provides a symbol sequence of all the non-static fields in the class, excluding the initialhidden fields and the fields in the base class. When used on an instance,.tupleof gives anlvalue sequence.

The order of the fields in the tuple matches the order in which the fields are declared.

Note:.tupleof is not available forextern(Objective-C) classes due to their fields having a dynamic offset.
class Foo {int x;long y; }staticassert(__traits(identifier, Foo.tupleof[0]) =="x");staticassert(is(typeof(Foo.tupleof)[1] ==long));void main(){import std.stdio;auto foo =new Foo;    foo.tupleof[0] = 1;// set foo.x to 1    foo.tupleof[1] = 2;// set foo.y to 2foreach (ref x; foo.tupleof)        x++;assert(foo.x == 2);assert(foo.y == 3);auto bar =new Foo;    bar.tupleof = foo.tupleof;// copy fieldsassert(bar.x == 2);assert(bar.y == 3);}

Accessing Hidden Fields

The properties.__vptr and.__monitor give access to the class object's vtbl[] and monitor, respectively, but should not be used in user code. Seethe ABI for details.

See also:Context Pointer.

Field Properties

The.offsetof property gives the offset in bytes of the field from the beginning of the class instantiation. Note that the compiler canrearrange class field offsets. Seethealign attribute for a struct-based example.

Note:.offsetof is not available for fields ofextern(Objective-C) classes due to their fields having a dynamic offset.

Member Functions (a.k.a. Methods)

Non-static member functions must be called on an instance of their class. They have an extra hidden parameter calledthis through which the class object's other members can be accessed. Inside the function body, the class instance members are in scope.

class C{int a;void foo()    {        a = 3;// assign to `this.a`    }}auto c =new C;c.foo();assert(c.a == 3);

Non-static member functions can have, in addition to the usualFunctionAttributes, the attributesconst,immutable,shared,inout,scope orreturn scope. These attributes apply to the hiddenthis parameter.

class C{int a;void foo()const    {        a = 3;// error, 'this' is const    }void foo()immutable    {        a = 3;// error, 'this' is immutable    }    C bar() @safescope    {returnthis;// error, 'this' is scope    }}

Objective-C linkage

Static member functions withObjective-C linkage also have an extra hidden parameter calledthis through which the class object's other members can be accessed.

Member functions with Objective-C linkage have an additional hidden, anonymous, parameter which is the selector the function was called with.

Static member functions with Objective-C linkage are placed in a hidden nested metaclass as non-static member functions.

Synchronized Method Calls

Member functions of a (non-synchronized) class can be individually marked assynchronized. The class instance's monitor object will be locked when the method is called and unlocked when the call terminates.

A synchronized method can only be called on ashared class instance.

class C{void foo();synchronizedint bar();}void test(C c){    c.foo;// OK//c.bar; // Error, `c` is not `shared`shared C sc =newshared C;//sc.foo; // Error, `foo` not callable using a `shared` object    sc.bar;// OK}

See alsoSynchronizedStatement.

Synchronized Classes

Each member function of asynchronized class is implicitlysynchronized. A static member function is synchronized on theclassinfo object for the class, which means that one monitor is used for all static member functions for that synchronized class. For non-static functions of a synchronized class, the monitor used is part of the class object. For example:

synchronizedclass Foo{void bar() { ...statements... }}

is equivalent to (as far as the monitors go):

synchronizedclass Foo{void bar()    {synchronized (this) { ...statements... }    }}
Note:bar uses aSynchronizedStatement.

Member fields of a synchronized class cannot be public:

synchronizedclass Foo{int foo;// Error: public field}synchronizedclass Bar{privateint bar;// ok}
Note: struct types cannot be markedsynchronized.

Class Instantiation

Fields are by default initialized to thedefault initializer for their type (usually 0 for integer types and NaN for floating point types). If the field declaration has an optionalInitializer that will be used instead of the default.

class Abc{int a;// default initializer for a is 0long b = 7;// default initializer for b is 7float f;// default initializer for f is NAN}void main(){    Abc obj =new Abc;assert(obj.b == 7);}

TheInitializer is evaluated at compile time.

This initialization is done before anyconstructors are called.

Instances of class objects are created with aNewExpression.

By default, a class instance is allocated on the garbage-collected heap. Ascope class instance is allocated on the stack.

Constructors

A constructor is a specialmember function which is normallyinvoked by the compiler when the class instance is created.

Constructor:thisParametersMemberFunctionAttributesoptFunctionBodythisParametersMemberFunctionAttributesoptMissingFunctionBodyConstructorTemplate

A constructor is declared with a function name ofthis and no return type:

class A{int i;this()// constructor taking no arguments    {        i = 2;// initialize `this.i`    }this(int i)// constructor taking an int argument    {this.i = i;// initialize field `i` from parameter `i`    }}void main(){    A a =new A;// instantiate A and call `A.this()`assert(a.i == 2);    a =new A(3);// instantiate A and call `A.this(3)`assert(a.i == 3);}

Explicitly returning a value in a constructor is not allowed, howeverreturn; may be used to exit the function early.

Base Class Construction

Base class construction is done by calling the base class constructor by the namesuper:

class A {this(int y) { } }class B : A{int j;this()    {        ...super(3);// call base constructor A.this(3)        ...    }}

Delegating Constructors

A constructor can call another constructor for the same class in order to share common initializations. This is called adelegating constructor:

class C{int j;this()    {        ...    }this(int i)    {this();// delegating constructor call        j = i;    }}

Restrictions

The following restrictions apply:

  1. It is illegal for constructors to mutually call each other.
    this() {this(1); }this(int i) {this(); }// illegal, cyclic constructor calls
    Implementation Defined: The compiler is not required to detect cyclic constructor calls.
    Undefined Behavior: If the program executes with cyclic constructor calls.
  2. If a constructor's code contains a delegating/base constructor call, all possible execution paths through the constructor must make exactly one of those calls:
    this() { a ||super(); }// illegal, 0 or 1 callthis() { (a) ?this(1) :super(); }// OKthis() {super();this(1); }// illegal, 2 callsthis(){for (...)    {super();// illegal, inside loop    }}
  3. It is illegal to refer tothis implicitly or explicitly prior to making a delegating/base constructor call.
  4. Delegating/base constructor calls cannot appear after labels.

See also:field initialization.

Implicit Base Class Construction

If there is no constructor for a class, but there is a constructor for the base class, a default constructor is implicitly generated with the form:

this() { }

If no calls to a delegating constructor orsuper appear in a constructor, and the base class has a nullary constructor, a call tosuper() is inserted at the beginning of the constructor. If that base class has a constructor that requires arguments and no nullary constructor, a matching call tosuper is required.

Instantiation Process

The following steps happen:

  1. Storage is allocated for the object. If this fails, rather than returnnull, anOutOfMemoryError is thrown. Thus, tedious checks for null references are unnecessary.
  2. The raw data is statically initialized using the values provided in the class definition. The pointer to the vtbl[] (the array of pointers to virtual functions) is assigned. Constructors are passed fully formed objects for which virtual functions can be called. This operation is equivalent to doing a memory copy of a static version of the object onto the newly allocated one.
  3. If there is a constructor defined for the class, the constructor matching the argument list is called.
  4. If a delegating constructor is not called, a call to the base class's default constructor is issued.
  5. The body of the constructor is executed.
  6. If class invariant checking is turned on, theclass invariant is called at the end of the constructor.

Constructor Attributes

Constructors can have one of these member function attributes:const,immutable, andshared. Construction of qualified objects will then be restricted to the implemented qualified constructors.

class C{this();// non-shared mutable constructor}// create mutable objectC m =new C();// create const object using mutable constructorconst C c2 =newconst C();// a mutable constructor cannot create an immutable object// immutable C i = new immutable C();// a mutable constructor cannot create a shared object// shared C s = new shared C();

Constructors can be overloaded with different attributes.

class C{this();// non-shared mutable constructorthis()shared;// shared mutable constructorthis()immutable;// immutable constructor}C m =new C();shared s =newshared C();immutable i =newimmutable C();

Pure Constructors

If the constructor can create a unique object (e.g. if it ispure), the object can be implicitly convertible to any qualifiers.

class C{this()pure;// Based on the definition, this creates a mutable object. But the// created object cannot contain any mutable global data.// Therefore the created object is unique.this(int[] arr)immutablepure;// Based on the definition, this creates an immutable object. But// the argument int[] never appears in the created object so it// isn't implicitly convertible to immutable. Also, it cannot store// any immutable global data.// Therefore the created object is unique.}immutable i =newimmutable C();// this() pure is calledshared s =newshared C();// this() pure is calledC m =new C([1,2,3]);// this(int[]) immutable pure is called

Field initialization inside a constructor

In a constructor body, the first instance of field assignment is its initialization.

class C{int num;this()    {        num = 1;// initialization        num = 2;// assignment    }}

If the field type has anopAssign method, it will not be used for initialization.

struct A{this(int n) {}voidopAssign(A rhs) {}}class C{    A val;this()    {        val = A(1);// val is initialized to the value of A(1)        val = A(2);// rewritten to val.opAssign(A(2))    }}

If the field type is not mutable, multiple initialization will be rejected.

class C{immutableint num;this()    {        num = 1;// OK        num = 2;// Error: multiple field initialization    }}

If the field is initialized on one path, it must be initialized on all paths.

class C{immutableint num;immutableint ber;this(int i)    {if (i)            num = 3;// initializationelse            num = 4;// initialization    }this(long j)    {        j ? (num = 3) : (num = 4);// ok        j || (ber = 3);// error, intialized on only one path        j && (ber = 3);// error, intialized on only one path    }}

A field initialization may not appear in a loop or after a label.

class C{immutableint num;immutable string str;this()    {foreach (i; 0..2)        {            num = 1;// Error: field initialization not allowed in loops        }        size_t i = 0;    Label:        str ="hello";// Error: field initialization not allowed after labelsif (i++ < 2)goto Label;    }}

If a field's type has disabled default construction, then it must be initialized in the constructor.

struct S {int y; @disablethis(); }class C{    S s;this(S t) { s = t; }// okthis(int i) {this(); }// okthis() { }// error, s not initialized}

Destructors

Destructor:~ this ( )MemberFunctionAttributesoptFunctionBody~ this ( )MemberFunctionAttributesoptMissingFunctionBody

The destructor function is called when:

Example:

import std.stdio;class Foo{    ~this()// destructor for Foo    {        writeln("dtor");    }}void main(){auto foo =new Foo;    destroy(foo);    writeln("end");}

The destructor is expected to release any non-GC resources held by the object.

The program can explicitly call the destructor of a live object immediately withdestroy. The runtime marks the object so the destructor is never called twice.

The destructor for thesuper class automatically gets called when the destructor ends. There is no way to call the super class destructor explicitly.

Implementation Defined: The garbage collector is not guaranteed to run the destructor for all unreferenced objects.
Important: The order in which the garbage collector calls destructors for unreferenced objects is not specified. This means that when the garbage collector calls a destructor for an object of a class that has members which are references to garbage collected objects, those references may no longer be valid. This means that destructors cannot reference sub objects.
Note: This rule does not apply to ascope class instance or an object destructed withdestroy, as the destructor is not being run during a garbage collection cycle, meaning all references are valid.

Objects referenced from the static data segment never get collected by the GC.

Static Constructors and Destructors

Static Constructors

StaticConstructor:static this ( )MemberFunctionAttributesoptFunctionBodystatic this ( )MemberFunctionAttributesoptMissingFunctionBody

A static constructor is a function that performs initializations of thread local data before themain() function gets control for the main thread, and upon thread startup.

Static constructors are used to initialize static class members with values that cannot be computed at compile time.

Static constructors in other languages are built implicitly by using member initializers that can't be computed at compile time. The trouble with this stems from not having good control over exactly when the code is executed, for example:
class Foo{staticint a = b + 1;staticint b = a * 2;}
What values doa andb end up with, what order are the initializations executed in, what are the values ofa andb before the initializations are run, is this a compile error, or is this a runtime error? Additional confusion comes from it not being obvious if an initializer is static or dynamic.

D makes this simple. All member initializations must be determinable by the compiler at compile time, hence there is no order-of-evaluation dependency for member initializations, and it is not possible to read a value that has not been initialized. Dynamic initialization is performed by a static constructor, defined with a special syntaxstatic this().

class Foo{staticint a;// default initialized to 0staticint b = 1;staticint c = b + a;// error, not a constant initializerstaticthis()// static constructor    {        a = b + 1;// a is set to 2        b = a * 2;// b is set to 4    }}

Ifmain() or the thread returns normally, (does not throw anexception), the static destructor is added to the list of functions to be calledon thread termination.

Static constructors have empty parameter lists.

Static constructors within a module are executed in the lexical order in which they appear. All the static constructors for modules that are directly or indirectly imported are executed before the static constructors for the importer.

Thestatic in the static constructor declaration is not an attribute, it must appear immediately before thethis:

class Foo{staticthis() { ... }// a static constructorstaticprivatethis() { ... }// Error: not a static constructorstatic    {this() { ... }// not a static constructor    }static:this() { ... }// not a static constructor}

Static Destructors

StaticDestructor:static ~ this ( )MemberFunctionAttributesoptFunctionBody
A static destructor is defined as a special static function with the syntaxstatic ~this().
class Foo{static ~this()// static destructor    {    }}

A static destructor gets called on thread termination, but only if the static constructor completed successfully. Static destructors have empty parameter lists. Static destructors get called in the reverse order that the static constructors were called in.

Thestatic in the static destructor declaration is not an attribute, it must appear immediately before the~this:

class Foo{static ~this() { ... }// a static destructorstaticprivate ~this() { ... }// Error: not a static destructorstatic    {        ~this() { ... }// not a static destructor    }static:        ~this() { ... }// not a static destructor}

Shared Static Constructors

SharedStaticConstructor:shared static this ( )MemberFunctionAttributesoptFunctionBodyshared static this ( )MemberFunctionAttributesoptMissingFunctionBody

Shared static constructors are executed before anyStaticConstructors, and are intended for initializing any shared global data.

Shared Static Destructors

SharedStaticDestructor:shared static ~ this ( )MemberFunctionAttributesoptFunctionBodyshared static ~ this ( )MemberFunctionAttributesoptMissingFunctionBody

Shared static destructors are executed at program termination in the reverse order thatSharedStaticConstructors were executed.

Class Invariants

Invariant:invariant ( )BlockStatementinvariantBlockStatementinvariant (AssertArguments) ;

ClassInvariants specify the relationships among the members of a class instance. Those relationships must hold for any interactions with the instance from its public interface.

The invariant is in the form of aconst member function. The invariant is defined tohold if all theAssertExpressions within the invariant that are executed succeed.

class Date{this(int d,int h)    {        day = d;// days are 1..31        hour = h;// hours are 0..23    }invariant    {assert(1 <= day && day <= 31);assert(0 <= hour && hour < 24);    }private:int day;int hour;}

Any class invariants for base classes are applied before the class invariant for the derived class.

There may be multiple invariants in a class. They are applied in lexical order.

ClassInvariants must hold at the exit of the class constructor (if any), and at the entry of the class destructor (if any).

ClassInvariants must hold at the entry and exit of all public or exported non-static member functions. The order of application of invariants is:

  1. preconditions
  2. invariant
  3. function body
  4. invariant
  5. postconditions

If the invariant does not hold, then the program enters an invalid state.

Implementation Defined:
  1. Whether the classInvariant is executed at runtime or not. This is typically controlled with a compiler switch.
  2. The behavior when the invariant does not hold is typically the same as for whenAssertExpressions fail.
Undefined Behavior: happens if the invariant does not hold and execution continues.

Public or exported non-static member functions cannot be called from within an invariant.

class Foo{publicvoid f() { }privatevoid g() { }invariant    {        f();// error, cannot call public member function from invariant        g();// ok, g() is not public    }}
Best Practices:
  1. Do not indirectly call exported or public member functions within a class invariant, as this can result in infinite recursion.
  2. Avoid reliance on side effects in the invariant. as the invariant may or may not be executed.
  3. Avoid having mutable public fields of classes with invariants, as then the invariant cannot verify the public interface.

Scope Classes

Note: Scope classes have been deprecated. See alsoscope class instances.

A scope class is a class with thescope attribute, as in:

scopeclass Foo { ... }

The scope characteristic is inherited, so any classes derived from a scopeclass are also scope.

A scope class reference can only appear as a function local variable. It must be declared as beingscope:

scopeclass Foo { ... }void func(){    Foo f;// error, reference to scope class must be scopescope Foo g =new Foo();// correct}

When a scope class reference goes out of scope, the destructor (if any) forit is automatically called. This holds true even if the scope was exited via athrown exception.

Abstract Classes

An abstract member function must be overridden by a derived class. Only virtual member functions may be declared abstract; non-virtual member functions and free-standing functions cannot be declared abstract.

A class is abstract if any of its virtual member functions are declared abstract or if they are defined within an abstract attribute. Note that an abstract class may also contain non-virtual member functions. Abstract classes cannot be instantiated directly. They can only be instantiated as a base class of another, non-abstract, class.

class C{abstractvoid f();}auto c =new C;// error, C is abstractclass D : C {}auto d =new D;// error, D is abstractclass E : C{overridevoid f() {}}auto e =new E;// OK

Member functions declared as abstract can still have function bodies. This is so that even though they must be overridden, they can still provide ‘base class functionality’, e.g. throughsuper.foo() in a derived class. Note that the class is still abstract and cannot be instantiated directly.

A class can be declared abstract:

abstractclass A{// ...}auto a =new A;// error, A is abstractclass B : A {}auto b =new B;// OK

Final Classes

Final classes cannot be subclassed:

finalclass A { }class B : A { }// error, class A is final

Methods of a final class are alwaysfinal.

Nested Classes

Anested class is a class that is declared inside the scope of afunction or another class. A nested class has access to the variables and othersymbols of the classes and functions it is nested inside:

class Outer{int m;class Inner    {int foo()        {return m;// Ok to access member of Outer        }    }}
void func(){int m;class Inner    {int foo()        {return m;// Ok to access local variable m of func()        }    }}

Static Nested Classes

If a nested class has thestatic attribute, then it can not accessvariables of the enclosing scope that are local to the stack or need athis reference:

class Outer{int m;staticint n;staticclass Inner    {int foo()        {return m;// Error, Inner is static and m needs a thisreturn n;// Ok, n is static        }    }}
void func(){int m;staticint n;staticclass Inner    {int foo()        {return m;// Error, Inner is static and m is local to the stackreturn n;// Ok, n is static        }    }}

Context Pointer

Non-static nested classes work by containing an extra hidden member (calledthe context pointer) that is the frame pointer of the enclosing function if itis nested inside a function, or thethis reference of the enclosing class's instanceif it is nested inside a class.

When a non-static nested class is instantiated, the context pointer is assigned before the class's constructor is called, therefore the constructor has full access to the enclosing variables. A non-static nested class can only be instantiated when the necessary context pointer information is available:

class Outer{class Inner { }staticclass SInner { }}void main(){    Outer o =new Outer;// Ok//Outer.Inner oi = new Outer.Inner; // Error, no 'this' for Outer    Outer.SInner os =new Outer.SInner;// Ok}
void main(){class Nested { }    Nested n =new Nested;// Okstatic f()    {//Nested sn = new Nested; // Error, no 'this' for Nested    }}

Explicit Instantiation

Athis reference can be supplied to the creation of an inner class instance by prefixing it to theNewExpression:

class Outer{int a;class Inner    {int foo()        {return a;        }    }}void main(){    Outer o =new Outer;    o.a = 3;    Outer.Inner oi = o.new Inner;assert(oi.foo() == 3);}

Hereo supplies thethis reference to the inner class instance ofOuter.

outer Property

For a nested class instance, the.outer property provides thethis reference of the enclosing class's instance. If there is no accessible parent class instance, the property provides avoid* to the enclosing function frame.

class Outer{class Inner1    {        Outer getOuter()        {returnthis.outer;        }    }void foo()    {        Inner1 i =new Inner1;assert(i.getOuter()isthis);    }}
class Outer{void bar()    {// x is referenced from nested scope, so// bar makes a closure environment.int x = 1;class Inner2        {            Outer getOuter()            {                x = 2;// The Inner2 instance has access to the function frame// of bar as a static frame pointer, but .outer returns// the enclosing Outer class instance property.returnthis.outer;            }        }        Inner2 i =new Inner2;assert(i.getOuter()isthis);    }}
class Outer{// baz cannot access an instance of Outerstaticvoid baz()    {// make a closure environmentint x = 1;class Inner3        {void* getOuter()            {                x = 2;// There's no accessible enclosing class instance, so the// .outer property returns the function frame of baz.returnthis.outer;            }        }        Inner3 i =new Inner3;assert(i.getOuter() !isnull);    }}

Anonymous Nested Classes

An anonymous nested class is both defined and instantiated with aNewAnonClassExpression:

NewAnonClassExpression:newPlacementExpressionoptclassConstructorArgsoptAnonBaseClassListoptAggregateBody
ConstructorArgs:(NamedArgumentListopt)
AnonBaseClassList:SuperClassOrInterfaceSuperClassOrInterface,Interfaces
which is equivalent to:
classIdentifier:AnonBaseClassListAggregateBody// ...newIdentifierConstructorArgs
whereIdentifier is the name generated for the anonymous nested class.
interface I{void foo();}auto obj =newclass I{void foo()    {        writeln("foo");    }};obj.foo();

Const, Immutable and Shared Classes

If aClassDeclaration has aconst,immutable orshared storage class, then it is as if each member of the class was declared with that storage class. If a base class is const, immutable or shared, then all classes derived from it are also const, immutable or shared.

Structs and Unions
Interfaces
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Thu Feb 19 20:55:41 2026

[8]ページ先頭

©2009-2026 Movatter.jp