Skip to content

Commit 11153b2

Browse files
committed
More detail in remove-duplicates example
1 parent 873fb8e commit 11153b2

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

component-model/src/design/why-component-model.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ remove-duplicates: func(offset: i32, length: i32) -> [i32, i32]
7474
supposing that `remove-duplicates` is a function
7575
to create a new string consisting of the unique characters
7676
in its argument.
77-
The return type is a pair of 32-bit integers,
78-
reflecting that the function must return
79-
the new offset for the newly allocated string, as well as its length.
77+
The return type is a list of two 32-bit integers.
78+
The first integer is an offset into one of the linear memories
79+
declared by the module—where the newly allocated string starts—and
80+
the second integer is the length of the string.
81+
After calling the function,
82+
the caller has to reach into the appropriate linear memory
83+
and read the output string, using the returned offset and length.
8084

8185
For this to work, the module defining the `remove-duplicates` function
8286
would also need to include
@@ -98,6 +102,15 @@ import "strings" "string_mem"
98102
also needs to declare the size of the memory being
99103
imported.)
100104

105+
Note that there is nothing in the type system to prevent
106+
the returned length from being confused with the returned offset,
107+
since both are integers.
108+
Also, the name of the memory used for the input and output strings
109+
must be established by convention,
110+
and there is also nothing in the type system to stop client code
111+
from indexing into a different memory
112+
(as long as the sum of the offset and length is within bounds).
113+
101114
We would prefer to write a pseudocode type signature like this:
102115

103116
```

0 commit comments

Comments
 (0)