www.digitalmars.com [Home] [Search] [D]
Last modified Feb 2, 2004.

Attributes

	AttributeSpecifier:
	    Attribute :
	    Attribute DeclDefBlock
	    Pragma ;

	AttributeElseSpecifier:
	    AttributeElse :
	    AttributeElse DeclDefBlock
	    AttributeElse DeclDefBlock else DeclDefBlock

	Attribute:
	    LinkageAttribute
	    AlignAttribute
	    Pragma
	    deprecated
	    private
	    protected
	    public
	    export
	    static
	    final
	    override
	    abstract
	    const
	    auto

	AttributeElse:
	    DebugAttribute
	    VersionAttribute

	DeclDefBlock
	    DeclDef
	    { }
	    { DeclDefs }
	
Attributes are a way to modify one or more declarations. The general forms are:
	attribute declaration;		affects the declaration

	attribute:			affects all declarations until the next }
	    declaration;
	    declaration;
	    ...

	attribute			affects all declarations in the block
	{
	    declaration;
	    declaration;
	    ...
	}
	
For attributes with an optional else clause:
	attribute
	    declaration;
	else
	    declaration;

	attribute			affects all declarations in the block
	{
	    declaration;
	    declaration;
	    ...
	}
	else
	{
	    declaration;
	    declaration;
	    ...
	}
	

Linkage Attribute

	LinkageAttribute:
		extern
		extern ( LinkageType )

	LinkageType:
		C
		D
		Windows
		Pascal
	
D provides an easy way to call C functions and operating system API functions, as compatibility with both is essential. The LinkageType is case sensitive, and is meant to be extensible by the implementation (they are not keywords). C and D must be supplied, the others are what makes sense for the implementation. Implementation Note: for Win32 platforms, Windows and Pascal should exist.

C function calling conventions are specified by:

	extern (C):
		int foo();	call foo() with C conventions
	
D conventions are:
	extern (D):
	
or:
	extern:
	
Windows API conventions are:
	extern (Windows):
	    void *VirtualAlloc(
	    void *lpAddress,
	    uint dwSize,
	    uint flAllocationType,
	    uint flProtect
	    );
	

Align Attribute

	AlignAttribute:
		align
		align ( Integer )
	
Specifies the alignment of struct members. align by itself sets it to the default, which matches the default member alignment of the companion C compiler. Integer specifies the alignment which matches the behavior of the companion C compiler when non-default alignments are used. A value of 1 means that no alignment is done; members are packed together.

Deprecated Attribute

It is often necessary to deprecate a feature in a library, yet retain it for backwards compatibility. Such declarations can be marked as deprecated, which means that the compiler can be set to produce an error if any code refers to deprecated declarations:
	deprecated
	{
		void oldFoo();
	}
	
Implementation Note: The compiler should have a switch specifying if deprecated declarations should be compiled with out complaint or not.

Protection Attribute

Protection is an attribute that is one of private, protected, public or export.

Private means that only members of the enclosing class can access the member, or members and functions in the same module as the enclosing class. Private members cannot be overridden. Private module members are equivalent to static declarations in C programs.

Protected means that only members of the enclosing class or any classes derived from that class can access the member. Protected module members are illegal.

Public means that any code within the executable can access the member.

Export means that any code outside the executable can access the member. Export is analogous to exporting definitions from a DLL.

Const Attribute

	const
	
The const attribute declares constants that can be evaluated at compile time. For example:
	const int foo = 7;

	const
	{
	    double bar = foo + 6;
	}
	

Override Attribute

	override
	
The override attribute applies to virtual functions. It means that the function must override a function with the same name and parameters in a base class. The override attribute is useful for catching errors when a base class's member function gets its parameters changed, and all derived classes need to have their overriding functions updated.
	class Foo
	{
	    int bar();
	    int abc(int x);
	}

	class Foo2 : Foo
	{
	    override
	    {
		int bar(char c);	// error, no bar(char) in Foo
		int abc(int x);		// ok
	    }
	}
	

Static Attribute

	static
	
The static attribute applies to functions and data. It means that the declaration does not apply to a particular instance of an object, but to the type of the object. In other words, it means there is no this reference.
	class Foo
	{
	    static int bar() { return 6; }
	    int foobar() { return 7; }
	}

	...

	Foo f = new Foo;
	Foo.bar();	// produces 6
	Foo.foobar();	// error, no instance of Foo
	f.bar();	// produces 6;
	f.foobar();	// produces 7;
	
Static functions are never virtual.

Static data has only one instance for the entire program, not once per object.

Static does not have the additional C meaning of being local to a file. Use the private attribute in D to achieve that. For example:

	module foo;
	int x = 3;		// x is global
	private int y = 4;	// y is local to module foo
	
Static can be applied to constructors and destructors, producing static constructors and static destructors.

Auto Attribute

	auto
	
The auto attribute is used for local variables and for class declarations. For class declarations, the auto attribute creates an auto class. For local declarations, auto implements the RAII (Resource Acquisition Is Initialization) protocol. This means that the destructor for an object is automatically called when the auto reference to it goes out of scope. The destructor is called even if the scope is exited via a thrown exception, thus auto is used to guarantee cleanup.

Auto cannot be applied to globals, statics, data members, inout or out parameters. Arrays of autos are not allowed, and auto function return values are not allowed. Assignment to an auto, other than initialization, is not allowed. Rationale: These restrictions may get relaxed in the future if a compelling reason to appears.


Copyright (c) 1999-2004 by Digital Mars, All Rights Reserved