| Attribute Name | Linkage | Description |
|---|---|---|
| gnuAbiTag | C++ | Declares an ABI tag on a C++ symbol. |
| mustuse | Ensures that values of a struct or union type are not discarded. | |
| optional | Objective-C | Makes an Objective-C interface method optional. |
| selector | Objective-C | Attaches an Objective-C selector to a method. |
| standalone | Marks a shared module constructor as not depending on any other module constructor being run first. | |
| weak | Specifies that a global symbol should be emitted with weak linkage. |
Sourcecore/attribute.d
weak;selector;extern(Objective-C).
extern (Objective-C)class NSObject{this() @selector("init");static NSObject alloc() @selector("alloc"); NSObject initWithUTF8String(inchar* str) @selector("initWithUTF8String:"); ObjcObject copyScriptingValue(ObjcObject value, NSString key, NSDictionary properties) @selector("copyScriptingValue:forKey:withProperties:");}
optional;import core.attribute :optional, selector;extern (Objective-C):struct objc_selector;alias SEL = objc_selector*;SEL sel_registerName(inchar* str);externclass NSObject{bool respondsToSelector(SEL sel) @selector("respondsToSelector:");}interface Foo{ @optionalvoid foo() @selector("foo"); @optionalvoid bar() @selector("bar");}class Bar : NSObject{static Bar alloc() @selector("alloc"); Bar init() @selector("init");void bar() @selector("bar") { }}extern (D)void main(){auto bar = Bar.alloc.init;if (bar.respondsToSelector(sel_registerName("bar"))) bar.bar();}
gnuAbiTag;gnuAbiTag("c", "b", "a") will appear as@gnuAbiTag("a", "b", "c").// ---- foo.cppstruct [[gnu::abi_tag ("tag1","tag2")]] Tagged1_2{struct [[gnu::abi_tag ("tag3")]] Tagged3 { [[gnu::abi_tag ("tag4")]]int Tagged4 () {return 42; } }}Tagged1_2 inst1;// ---- foo.d@gnuAbiTag("tag1","tag2")struct Tagged1_2{// Notice the repetition @gnuAbiTag("tag1","tag2","tag3")struct Tagged3 { @gnuAbiTag("tag1","tag2","tag3","tag4")int Tagged4 (); }}extern__gshared Tagged1_2 inst1;
mustuse;mustuse attribute, the compiler will emit an error any time a value of that type would be discarded. Currently,@mustuse is only recognized by the compiler when attached tostruct andunion declarations. To allow for future expansion, attaching@mustuse to aclass,interface,enum, or function declaration is currently forbidden, and will result in a compile-time error. All other uses of@mustuse are ignored.@mustusestruct ErrorCode {int value; }extern(C) ErrorCode doSomething();void main(){// error: would discard a value of type ErrorCode//doSomething(); ErrorCode result;// ok: value is assigned to a variable result = doSomething();// ok: can ignore the value explicitly with a castcast(void) doSomething();}
standalone;