Skip to content

Commit 97f4032

Browse files
committed
added deduplication
Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
1 parent 0f597ef commit 97f4032

File tree

1 file changed

+58
-29
lines changed

1 file changed

+58
-29
lines changed

design/mvp/WIT.md

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ explicitly chosen then bindings cannot be generated.
187187

188188
### Union of Worlds with `include`
189189

190-
A World can be created by taking the union of two or more worlds. This operation allows
191-
world builders to form larger worlds from smaller worlds.
190+
A World can be created by taking the union of two or more worlds. This operation allows world builders to form larger worlds from smaller worlds.
192191

193192
Below is a simple example of a world that includes two other worlds.
194193

@@ -212,9 +211,7 @@ world union-my-world {
212211
}
213212
```
214213

215-
The `include` statement is used to include the imports and exports of another World to the
216-
current World. It says that the new World should be able to run all components that target
217-
the included worlds and more.
214+
The `include` statement is used to include the imports and exports of another World to the current World. It says that the new World should be able to run all components that target the included worlds and more.
218215

219216
The `union-my-world` World defined above is equivalent to the following World:
220217

@@ -270,15 +267,15 @@ The following example shows how to resolve name conflicts where `union-my-world-
270267
```wit
271268
// my-world-1.wit
272269
world my-world-1 {
273-
import a: self.a
274-
import b: self.b
270+
import a: self.a1
271+
import b: self.b1
275272
export d: self.d
276273
}
277274
278275
// my-world-2.wit
279276
world my-world-2 {
280-
import a: self.a
281-
import b: self.b
277+
import a: self.a2
278+
import b: self.b2
282279
export c: self.c
283280
}
284281
@@ -290,39 +287,34 @@ world union-my-world-1 {
290287
291288
world union-my-world-2 {
292289
// resolve conflicts
293-
import a1: pkg.my-world-1.a
294-
import b1: pkg.my-world-1.b
290+
import a1: pkg.my-world-1.a1
291+
import b1: pkg.my-world-1.b1
295292
export d: pkg.my-world-1.d
296293
297-
import a: pkg.my-world-2.a
298-
import b: pkg.my-world-2.b
294+
import a: pkg.my-world-2.a2
295+
import b: pkg.my-world-2.b2
299296
export c: pkg.my-world-2.c
300297
}
301298
```
302299

303-
### De-duplication (In the future)
304-
305-
As of now, the `include` statement requires the world author to explicitly rename the imports and exports that have the same name.
300+
### De-duplication
306301

307-
In the future, we may allow to de-duplicate the imports and exports of the included worlds if the yare structurally equivalent following the [Subtyping](Subtyping.md) rules. For example, the following world `union-my-world-3` is equivalent to `union-my-world-4`:
302+
If two interfaces have the same structure, then these two interfaces are considered to be structurally equivalent. The `include` statement can
303+
deduplicate the imports and exports of the included worlds if the they are structurally equivalent following the [Subtyping](Subtyping.md) rules. For example, the following world `union-my-world-3` is equivalent to `union-my-world-4`:
308304

309305
```wit
310-
// a.wit
311-
// b.wit
312-
// c.wit
313-
314306
// my-world-1.wit
315307
world my-world-1 {
316-
import a: pkg.a
317-
import b: pkg.b
318-
export c: pkg.c
308+
import a1: pkg.a
309+
import b1: pkg.b
310+
export c1: pkg.c
319311
}
320312
321313
// my-world-2.wit
322314
world my-world-2 {
323-
import a: pkg.a
324-
import b: pkg.b
325-
export c: pkg.c
315+
import a2: pkg.a
316+
import b2: pkg.b
317+
export c2: pkg.c
326318
}
327319
328320
// union.wit
@@ -332,10 +324,47 @@ world union-my-world-3 {
332324
}
333325
334326
world union-my-world-4 {
335-
import a: pkg.a
336-
import b: pkg.b
327+
import a1: pkg.a
328+
import b1: pkg.b
329+
export c1: pkg.c
330+
}
331+
```
332+
333+
As you can see, the names of the imports and exports in "union-my-world-4" are picking up the names from the first included world. This is because the second included world has structurally the same imports and exports and are deduplicated.
334+
335+
### De-duplication with `with`
336+
337+
When two worlds have both name conflicts and structurally equivalent imports and exports, the semantics of `include` will do deduplication first and then resolve the name conflicts with `with` statements. For example, the following world `union-my-world-5` is equivalent to `union-my-world-6`:
338+
339+
```wit
340+
341+
// my-world-1.wit
342+
world my-world-1 {
343+
import a1: pkg.a
344+
import b1: pkg.b
337345
export c: pkg.c
338346
}
347+
348+
// my-world-2.wit
349+
world my-world-2 {
350+
import a1: pkg.a
351+
import b1: pkg.b
352+
export c: pkg.d
353+
}
354+
355+
// union
356+
world union-my-world-5 {
357+
include pkg.my-world-1 with { c as c1 }
358+
include pkg.my-world-2
359+
}
360+
361+
world union-my-world-6 {
362+
import a1: pkg.a
363+
import b1: pkg.b
364+
export c1: pkg.c
365+
export c: pkg.d
366+
}
367+
339368
```
340369

341370
### A Note on SubTyping

0 commit comments

Comments
 (0)