root/trunk/docsrc/interface.dd

Revision 2136, 6.3 kB (checked in by walter, 2 years ago)

bugzilla 2652 DeclDef? grammar is wrong

  • Property svn:eol-style set to native
Line 
1 Ddoc
2
3 $(SPEC_S Interfaces,
4
5 $(GRAMMAR
6 $(GNAME InterfaceDeclaration):
7     $(B interface) $(I Identifier) $(GLINK BaseInterfaceList) $(GLINK InterfaceBody)
8     $(LINK2 template.html#InterfaceTemplateDeclaration, $(I InterfaceTemplateDeclaration))
9
10 $(GNAME BaseInterfaceList):
11     $(I Empty)
12     $(B :) $(LINK2 class.html#InterfaceClasses, $(I InterfaceClasses))
13
14 $(GNAME InterfaceBody):
15     $(B { })
16     $(B {) $(GLINK InterfaceBodyDeclarations) $(B })
17
18 $(GNAME InterfaceBodyDeclarations):
19     $(GLINK InterfaceBodyDeclaration)
20     $(GLINK InterfaceBodyDeclaration) $(I InterfaceBodyDeclarations)
21
22 $(GNAME InterfaceBodyDeclaration):
23     $(B {) $(LINK2 module.html#DeclDef, $(I DeclDef)) $(B })
24 )
25
26     $(P Interfaces describe a list of functions that a class that inherits
27     from the interface must implement.
28     A class that implements an interface can be converted to a reference
29     to that interface.)
30
31     $(P Some operating system objects, like COM/OLE/ActiveX for Win32,
32     have specialized interfaces. D interfaces that are compatible with
33     COM/OLE/ActiveX are called $(LINK2 #COM-Interfaces, $(I COM Interfaces)).
34     )
35
36 $(V2
37     $(P $(LINK2 #CPP-Interfaces, $(I C++ Interfaces)) are another
38     form of interfaces, meant to be binary compatible with C++.
39     )
40 )
41
42     $(P Interfaces cannot derive from classes; only from other interfaces.
43     Classes cannot derive from an interface multiple times.
44     )
45
46 ------
47 interface D
48 {
49     void foo();
50 }
51
52 class A : D, D  // error, duplicate interface
53 {
54 }
55 ------
56
57     An instance of an interface cannot be created.
58
59
60 ------
61 interface D
62 {
63     void foo();
64 }
65
66 ...
67
68 D d = new D();      // error, cannot create instance of interface
69 ------
70
71 $(V1
72     $(P Interface member functions do not have implementations.)
73
74 ------
75 interface D
76 {
77     void bar() { }  // error, implementation not allowed
78 }
79 ------
80 )
81 $(V2
82     $(P Virtual interface member functions do not have implementations.
83     Interfaces are expected to implement static or final functions.
84     )
85
86 ------
87 interface D
88 {
89     void bar() { }  // error, implementation not allowed
90     static void foo() { } // ok
91     final void abc() { } // ok
92 }
93 ------
94
95     $(P Classes that inherit from an interface may not override final or
96     static interface member functions.)
97
98 ------
99 interface D {
100     void bar();
101     static void foo() { }
102     final void abc() { }
103 }
104
105 class C : D {
106     void bar() { } // ok
107     void foo() { } // error, cannot override static D.foo()
108     void abc() { } // error, cannot override final D.abc()
109 }
110 ------
111
112 )
113     $(P All interface functions must be defined in a class that inherits
114     from that interface:
115     )
116 ------
117 interface D
118 {
119     void foo();
120 }
121
122 class A : D
123 {
124     void foo() { }  // ok, provides implementation
125 }
126
127 class B : D
128 {
129     int foo() { }   // error, no void foo() implementation
130 }
131 ------
132
133     Interfaces can be inherited and functions overridden:
134
135 ------
136 interface D
137 {
138     int foo();
139 }
140
141 class A : D
142 {
143     int foo() { return 1; }
144 }
145
146 class B : A
147 {
148     int foo() { return 2; }
149 }
150
151 ...
152
153 B b = new B();
154 b.foo();        // returns 2
155 D d = cast(D) b;    // ok since B inherits A's D implementation
156 d.foo();        // returns 2;
157 ------
158
159     $(P Interfaces can be reimplemented in derived classes:)
160
161 ------
162 interface D
163 {
164     int foo();
165 }
166
167 class A : D
168 {
169     int foo() { return 1; }
170 }
171
172 class B : A, D
173 {
174     int foo() { return 2; }
175 }
176
177 ...
178
179 B b = new B();
180 b.foo();        // returns 2
181 D d = cast(D) b;
182 d.foo();        // returns 2
183 A a = cast(A) b;
184 D d2 = cast(D) a;
185 d2.foo();       // returns 2, even though it is A's D, not B's D
186 ------
187
188     $(P A reimplemented interface must implement all the interface
189     functions, it does not inherit them from a super class:
190     )
191
192 ------
193 interface D
194 {
195     int foo();
196 }
197
198 class A : D
199 {
200     int foo() { return 1; }
201 }
202
203 class B : A, D
204 {
205 }       // error, no foo() for interface D
206 ------
207
208 $(V2
209 $(SECTION2 $(LNAME2 InterfaceContracts, Interfaces with Contracts),
210
211     $(P Interface member functions can have contracts even though there
212     is no body for the function. The contracts are inherited by any
213     class member function that implements that interface member function.
214     )
215
216 ---
217 interface I
218 {
219     int foo(int i)
220       in { assert(i > 7); }
221       out (result) { assert(result & 1); }
222
223     void bar();
224 }
225 ---
226 )
227
228 $(SECTION2 $(LNAME2 ConstInterface, Const and Immutable Interfaces),
229     $(P If an interface has $(CODE const) or $(CODE immutable) storage
230     class, then all members of the interface are
231     $(CODE const) or $(CODE immutable).
232     This storage class is not inherited.
233     )
234 )
235 )
236
237 $(SECTION2 $(LNAME2 COM-Interfaces, COM Interfaces),
238
239     $(P A variant on interfaces is the COM interface. A COM interface is
240     designed to map directly onto a Windows COM object. Any COM object
241     can be represented by a COM interface, and any D object with
242     a COM interface can be used by external COM clients.
243     )
244
245     $(P A COM interface is defined as one that derives from the interface
246     $(TT std.c.windows.com.IUnknown). A COM interface differs from
247     a regular D interface in that:
248     )
249
250     $(UL
251     $(LI It derives from the interface $(TT std.c.windows.com.IUnknown).)
252     $(LI It cannot be the argument of a $(I DeleteExpression).)
253     $(LI References cannot be upcast to the enclosing class object, nor
254     can they be downcast to a derived interface. To accomplish this,
255     an appropriate $(TT QueryInterface()) would have to be implemented
256     for that interface in standard COM fashion.)
257     $(LI Classes derived from COM interfaces are COM classes.)
258     $(LI The default linkage for member functions of COM classes
259     is $(TT extern(System)).)
260     $(LI The first member of the $(TT vtbl[]) is not the pointer
261     to the InterfaceInfo, but the first virtual function pointer.)
262     )
263 )
264
265 $(V2
266 $(SECTION2 $(LNAME2 CPP-Interfaces, C++ Interfaces),
267
268     $(P C++ interfaces are interfaces declared with C++ linkage:
269     )
270
271 ---
272 extern (C++) interface Ifoo
273 {
274     void foo();
275     void bar();
276 }
277 ---
278
279     $(P which is meant to correspond with the following C++ declaration:)
280
281 $(CPPCODE
282 class Ifoo
283 {
284     virtual void foo();
285     virtual void bar();
286 };
287 )
288
289     $(P Any interface that derives from a C++ interface is also
290     a C++ interface.
291     A C++ interface differs from a D interface in that:
292     )
293
294     $(UL
295     $(LI It cannot be the argument of a $(I DeleteExpression).)
296     $(LI References cannot be upcast to the enclosing class object, nor
297     can they be downcast to a derived interface.)
298     $(LI The C++ calling convention is the default convention
299     for its member functions, rather than the D calling convention.)
300     $(LI The first member of the $(TT vtbl[]) is not the pointer
301     to the $(TT Interface), but the first virtual function pointer.)
302     )
303 )
304 )
305
306 )
307
308 Macros:
309     TITLE=Interfaces
310     WIKI=Interface
Note: See TracBrowser for help on using the browser.