Skip to content

Commit 8a0296f

Browse files
authored
Merge pull request #2540 from jacob-carlborg/objc-class
Add documentation for Objective-C classes merged-on-behalf-of: unknown
2 parents 1f4b169 + 53b4d93 commit 8a0296f

File tree

3 files changed

+81
-20
lines changed

3 files changed

+81
-20
lines changed

spec/class.dd

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ $(P Classes consist of:)
5252
$(UL
5353
$(LI a super class)
5454
$(LI interfaces)
55+
$(LI a nested anonymous metaclass for classes declared with `extern (Objective-C)`)
5556
$(LI dynamic fields)
5657
$(LI static fields)
5758
$(LI types)
@@ -205,16 +206,25 @@ $(H2 $(LNAME2 super_class, Super Class))
205206

206207
$(H2 $(LNAME2 member-functions, Member Functions))
207208

208-
$(P Non-static member functions have an extra hidden parameter
209-
called $(I this) through which the class object's other members
210-
can be accessed.
209+
$(P Non-static member functions or static member functions with
210+
`Objective-C` linkage have an extra hidden parameter called $(I this)
211+
through which the class object's other members can be accessed.
212+
)
213+
214+
$(P Member functions with the Objective-C linkage has an additional
215+
hidden, anonymous, parameter which is the selector the function was
216+
called with.
211217
)
212218

213219
$(P Non-static member functions can have, in addition to the usual
214220
$(GLINK2 function, FunctionAttribute)s, the attributes
215221
$(D const), $(D immutable), $(D shared), or $(D inout).
216222
These attributes apply to the hidden $(I this) parameter.
217223
)
224+
225+
$(P Static member functions with the Objective-C linkage are placed in
226+
the hidden nested metaclass as non-static member functions.
227+
)
218228
---
219229
class C
220230
{

spec/function.dd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,8 @@ $(H2 $(LNAME2 virtual-functions, Virtual Functions))
717717
are not templatized are virtual unless the compiler can determine that
718718
they will never be overridden (e.g. they are marked with $(D final) and
719719
do not override any functions in a base class), in which case, it will
720-
make them non-virtual. This results in fewer bugs caused by not
720+
make them non-virtual. Static or `final` functions with `Objective-C`
721+
linkage are virtual as well. This results in fewer bugs caused by not
721722
declaring a function virtual and then overriding it anyway.
722723
)
723724

@@ -799,6 +800,11 @@ class Bar : Foo
799800
the function is called.
800801
)
801802

803+
$(P
804+
Functions with `Objective-C` linkage has an additional hidden,
805+
unnamed, parameter which is the selector it was called with.
806+
)
807+
802808
$(P To avoid dynamic binding on member function call, insert
803809
base class name before the member function name. For example:
804810
)
@@ -1043,6 +1049,8 @@ void main()
10431049
------
10441050
)
10451051

1052+
$(P Static functions with `Objective-C` linkage are overridable.)
1053+
10461054
$(H2 $(LNAME2 inline-functions, Inline Functions))
10471055

10481056
$(P The compiler makes the decision whether to inline a function or not.

spec/objc_interface.dd

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,79 @@ $(SPEC_S Interfacing to Objective-C,
55
$(HEADERNAV_TOC)
66

77
$(P
8-
D has limited support for interfacing with Objective-C. It supports
9-
external classes and calling instance and class methods. It is only
10-
available on macOS, compiling for 64bit.
8+
D has some support for interfacing with Objective-C. It supports
9+
classes, instance and class methods. It is only available on macOS,
10+
compiling for 64bit.
1111
)
1212

1313
$(P
1414
Fully working example is available at
1515
$(LINK2 #usage-example, the bottom).
1616
)
1717

18-
$(SECTION2 $(LNAME2 external-class, Declaring an External Class))
18+
$(SECTION2 $(LNAME2 classes, Classes))
19+
20+
$(SECTION3 $(LNAME2 external-class, Declaring an External Class))
1921

2022
---
2123
extern (Objective-C)
22-
interface NSString
24+
class NSString
2325
{
2426
const(char)* UTF8String() @selector("UTF8String");
2527
}
2628
---
2729

2830
$(P
29-
Currently all Objective-C classes need to be declared as interfaces in
30-
D. All Objective-C classes that should be accessible from within D
31-
need to be declared with the $(LINK2 #objc-linkage, Objective-C linkage).
31+
All Objective-C classes that should be accessible from within D need to
32+
be declared with the $(LINK2 #objc-linkage, Objective-C linkage). If all
33+
methods inside an Objective-C class are without a body the class is
34+
expected to be defined externally.
3235
)
3336

3437
$(P
3538
The $(LINK2 #selector-attribute, `@selector`) attribute indicates which
36-
Objective-C selector should be used when calling this method from D.
37-
This attribute needs to be attached to all methods.
39+
Objective-C selector should be used when calling this method.
40+
This attribute needs to be attached to all methods with the
41+
`Objective-C` linkage.
42+
)
43+
44+
$(SECTION3 $(LNAME2 defining-class, Defining a Class))
45+
46+
---
47+
// externally defined
48+
extern (Objective-C)
49+
class NSObject
50+
{
51+
static NSObject alloc() @selector("alloc");
52+
NSObject init() @selector("init");
53+
}
54+
55+
extern (Objective-C)
56+
class Foo : NSObject
57+
{
58+
override static Foo alloc() @selector("alloc");
59+
override Foo init() @selector("init");
60+
61+
final int bar(int a) @selector("bar:")
62+
{
63+
return a;
64+
}
65+
}
66+
67+
void main()
68+
{
69+
assert(Foo.alloc.init.bar(3) == 3);
70+
}
71+
---
72+
73+
$(P
74+
Defining an Objective-C class is exactly the same as declaring an
75+
external class but it needs to have at least one method with a body.
76+
)
77+
78+
$(P
79+
To match the Objective-C semantics, `static` and `final` methods are
80+
virtual. `static` methods are overridable as well.
3881
)
3982

4083
$(SECTION2 $(LNAME2 instance-method, Calling an Instance Method))
@@ -73,7 +116,7 @@ $(HEADERNAV_TOC)
73116

74117
---
75118
extern (Objective-C)
76-
interface NSString
119+
class NSString
77120
{
78121
NSString initWith(in char*) @selector("initWithUTF8String:");
79122
NSString initWith(NSString) @selector("initWithString:");
@@ -133,19 +176,19 @@ $(HEADERNAV_TOC)
133176

134177
$(P
135178
Objective-C linkage is achieved by attaching the `extern (Objective-C)`
136-
attribute to an interface. Example:
179+
attribute to a class. Example:
137180
)
138181

139182
---
140183
extern (Objective-C)
141-
interface NSObject
184+
class NSObject
142185
{
143186
NSObject init() @selector("init");
144187
}
145188
---
146189

147190
$(P
148-
All methods inside an interface declared as `extern (Objective-C)` will
191+
All methods inside a class declared as `extern (Objective-C)` will
149192
get implicit Objective-C linkage.
150193
)
151194

@@ -229,7 +272,7 @@ $(HEADERNAV_TOC)
229272

230273
---
231274
extern (Objective-C)
232-
interface NSString
275+
class NSString
233276
{
234277
static NSString alloc() @selector("alloc");
235278
NSString initWithUTF8String(in char* str) @selector("initWithUTF8String:");
@@ -295,7 +338,7 @@ $(HEADERNAV_TOC)
295338
module main;
296339

297340
extern (Objective-C)
298-
interface NSString
341+
class NSString
299342
{
300343
static NSString alloc() @selector("alloc");
301344
NSString initWithUTF8String(in char* str) @selector("initWithUTF8String:");

0 commit comments

Comments
 (0)