This file is indexed.

/usr/share/doc/mlton/guide/ObjectOrientedProgramming is in mlton-doc 20100608-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>ObjectOrientedProgramming - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton MLTONWIKIVERSION</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      ObjectOrientedProgramming
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "TitleIndex">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
<a href="StandardML">Standard ML</a> does not have explicit support for object-oriented programming.  Here are some papers that show how to express certain object-oriented concepts in SML. 
    <ul>

    <li>
<p>
 <a href = "References#Berthomieu00"> OO Programming styles in ML</a> 
</p>
</li>
    <li class="gap">
<p>
 <a href = "References#ThorupTofte94"> Object-oriented programming and Standard ML</a> 
</p>
</li>
    <li class="gap">
<p>
 <a href = "References#LarsenNiss04"> mGTK: An SML binding of Gtk+</a> 
</p>
</li>
    <li class="gap">
<p>
 <a href = "References#FluetPucella02"> Phantom Types and Subtyping</a> 
</p>
</li>

    </ul>


<hr>
 <p>
The question of OO programming in SML comes up every now and then.  The following discusses a simple object-oriented (OO) programming technique in Standard ML.  The reader is assumed to be able to read Java and SML code. 
</p>
<h3 id="head-d6a06199cc127e571d3588aef63c79108675eabc">Motivation</h3>
<p>
SML doesn't provide subtyping, but it does provide parametric polymorphism, which can be used to encode some forms of subtyping.  Most articles on OO programming in SML concentrate on such encoding techniques. While those techniques are interesting --- and it is recommended to read such articles --- and sometimes useful, it seems that basically all OO gurus agree that (deep) subtyping (or inheritance) hierarchies aren't as practical as they were thought to be in the early OO days.  "Good", flexible, "OO" designs tend to have a flat structure 
</p>

<pre>         Interface
             ^
             |
- - -+-------+-------+- - -
     |       |       |
   ImplA   ImplB   ImplC
</pre><p>
and deep inheritance hierarchies 
</p>

<pre>ClassA
  ^
  |
ClassB
  ^
  |
ClassC
  ^
  |
</pre><p>
tend to be signs of design mistakes.  There are good underlying reasons for this, but a thorough discussion is not in the scope of this article. However, the point is that perhaps the encoding of subtyping is not as important as one might believe.  In the following we ignore subtyping and rather concentrate on a very simple and basic dynamic dispatch technique. 
</p>
<h3 id="head-af906b64b88d8a14633674631d41c471e32ab7ce">Dynamic Dispatch Using a Recursive Record of Functions</h3>
<p>
Quite simply, the basic idea is to implement a "virtual function table" using a record that is wrapped inside a (possibly recursive) datatype. Let's first take a look at a simple concrete example. 
</p>
<p>
Consider the following Java interface: 
</p>

<pre>public interface Counter {
  public void inc();
  public int get();
}
</pre><p>
We can translate the Counter interface to SML as follows: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> counter </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Counter</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> {inc : unit -&gt; unit, get : unit -&gt; int}
</FONT></B></PRE>
<p>
 
</p>
<p>
Each value of type <tt>counter</tt> can be thought of as an object that responds to two messages <tt>inc</tt> and <tt>get</tt>.  To actually send messages to a counter, it is useful to define auxiliary functions 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">local</FONT></B>
   <B><FONT COLOR="#A020F0">fun</FONT></B> mk m (Counter t) = m t ()
<B><FONT COLOR="#0000FF">in</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> cGet = mk#get
   <B><FONT COLOR="#A020F0">val</FONT></B> cInc = mk#inc
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
that basically extract the "function table" <tt>t</tt> from a counter object and then select the specified method <tt>m</tt> from the table. 
</p>
<p>
Let's then implement a simple function that increments a counter until a given maximum is reached: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> incUpto counter max = <B><FONT COLOR="#A020F0">while</FONT></B> cGet counter &lt; max <B><FONT COLOR="#A020F0">do</FONT></B> cInc counter
</PRE>
<p>
 
</p>
<p>
You can easily verify that the above code compiles even without any concrete implementation of a counter, thus it is clear that it doesn't depend on a particular counter implementation. 
</p>
<p>
Let's then implement a couple of counters.  First consider the following Java class implementing the Counter interface given earlier. 
</p>

<pre>public class BasicCounter implements Counter {
  private int cnt;
  public BasicCounter(int initialCnt) { this.cnt = initialCnt; }
  public void inc() { this.cnt += 1; }
  public int get() { return this.cnt; }
}
</pre><p>
We can translate the above to SML as follows: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> newBasicCounter initialCnt = <B><FONT COLOR="#A020F0">let</FONT></B>
       <B><FONT COLOR="#A020F0">val</FONT></B> cnt = ref initialCnt
    <B><FONT COLOR="#A020F0">in</FONT></B>
       Counter {inc = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; cnt := !cnt + <B><FONT COLOR="#5F9EA0">1</FONT></B>,
                get = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; !cnt}
    <B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
The SML function <tt>newBasicCounter</tt> can be described as a constructor function for counter objects of the <tt>BasicCounter</tt> "class".  We can also have other counter implementations.  Here is the constructor for a counter decorator that logs messages: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> newLoggedCounter counter =
    Counter {inc = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; (print <B><FONT COLOR="#BC8F8F">&quot;inc\n&quot;</FONT></B> ; cInc counter),
             get = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; (print <B><FONT COLOR="#BC8F8F">&quot;get\n&quot;</FONT></B> ; cGet counter)}
</PRE>
<p>
 
</p>
<p>
The <tt>incUpto</tt> function works just as well with objects of either class: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> aCounter = newBasicCounter <B><FONT COLOR="#5F9EA0">0</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> () = incUpto aCounter <B><FONT COLOR="#5F9EA0">5</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> () = print (Int.toString (cGet aCounter) ^<B><FONT COLOR="#BC8F8F">&quot;\n&quot;</FONT></B>)

<B><FONT COLOR="#A020F0">val</FONT></B> aCounter = newLoggedCounter (newBasicCounter <B><FONT COLOR="#5F9EA0">0</FONT></B>)
<B><FONT COLOR="#A020F0">val</FONT></B> () = incUpto aCounter <B><FONT COLOR="#5F9EA0">5</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> () = print (Int.toString (cGet aCounter) ^<B><FONT COLOR="#BC8F8F">&quot;\n&quot;</FONT></B>)
</PRE>
<p>
 
</p>
<p>
In general, a dynamic dispatch interface is represented as a record type wrapped inside a datatype.  Each field of the record corresponds to a public method or field of the object: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> interface </FONT></B>=<B><FONT COLOR="#228B22">
   <FONT COLOR="#B8860B">Interface</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> {method : t1 -&gt; t2,
                 immutableField : t,
                 mutableField : t ref}
</FONT></B></PRE>
<p>
 
</p>
<p>
The reason for wrapping the record inside a datatype is that records, in SML, can not be recursive.  However, SML datatypes can be recursive.  A record wrapped in a datatype can contain fields that contain the datatype. For example, an interface such as Cloneable 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> cloneable </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Cloneable</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> {clone : unit -&gt; cloneable}
</FONT></B></PRE>
<p>
 
</p>
<p>
can be represented using recursive datatypes. 
</p>
<p>
Like in OO languages, interfaces are abstract and can not be instantiated to produce objects.  To be able to instantiate objects, the constructors of a concrete class are needed.  In SML, we can implement constructors as simple functions from arbitrary arguments to values of the interface type. Such a constructor function can encapsulate arbitrary private state and functions using lexical closure.  It is also easy to share implementations of methods between two or more constructors. 
</p>
<p>
While the <tt>Counter</tt> example is rather trivial, it should not be difficult to see that this technique quite simply doesn't require a huge amount of extra verbiage and is more than usable in practice. 
</p>
<h3 id="head-343f19d7557070bf1d1603ce9f1f8c2f1220bd70">SML Modules and Dynamic Dispatch</h3>
<p>
One might wonder about how SML modules and the dynamic dispatch technique work together.  Let's investigate!  Let's use a simple dispenser framework as a concrete example.  (Note that this isn't intended to be an introduction to the SML module system.) 
</p>
<h4 id="head-8657a6eb69095e43b608460695dc355aaf3781a7">Programming with SML Modules</h4>
<p>
Using SML signatures we can specify abstract data types (ADTs) such as dispensers.  Here is a signature for an "abstract" functional (as opposed to imperative) dispenser: 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">signature</FONT></B> ABSTRACT_DISPENSER = <B><FONT COLOR="#0000FF">sig</FONT></B>
   <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t
   </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> isEmpty : 'a t -&gt; bool
   <B><FONT COLOR="#A020F0">val</FONT></B> push : 'a * 'a t -&gt; 'a t
   <B><FONT COLOR="#A020F0">val</FONT></B> pop : 'a t -&gt; ('a * 'a t) option
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
The term "abstract" in the name of the signature refers to the fact that the signature gives no way to instantiate a dispenser.  It has nothing to do with the concept of abstract data types. 
</p>
<p>
Using SML functors we can write "generic" algorithms that manipulate dispensers of an unknown type.  Here are a couple of very simple algorithms: 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">functor</FONT></B> DispenserAlgs (D : ABSTRACT_DISPENSER) = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> D

   <B><FONT COLOR="#A020F0">fun</FONT></B> pushAll (xs, d) = foldl push d xs

   <B><FONT COLOR="#A020F0">fun</FONT></B> popAll d = <B><FONT COLOR="#A020F0">let</FONT></B>
          <B><FONT COLOR="#A020F0">fun</FONT></B> lp (xs, NONE) = rev xs
            | lp (xs, SOME (x, d)) = lp (x::xs, pop d)
       <B><FONT COLOR="#A020F0">in</FONT></B>
          lp ([], pop d)
       <B><FONT COLOR="#A020F0">end</FONT></B>

   <B><FONT COLOR="#A020F0">fun</FONT></B> cp (from, to) = pushAll (popAll from, to)
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
As one can easily verify, the above compiles even without any concrete dispenser structure.  Functors essentially provide a form a static dispatch that one can use to break compile-time dependencies. 
</p>
<p>
We can also give a signature for a concrete dispenser 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">signature</FONT></B> DISPENSER = <B><FONT COLOR="#0000FF">sig</FONT></B>
   <B><FONT COLOR="#0000FF">include</FONT></B> ABSTRACT_DISPENSER
   <B><FONT COLOR="#A020F0">val</FONT></B> empty : 'a t
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
and write any number of concrete structures implementing the signature. For example, we could implement stacks 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Stack :&gt; DISPENSER = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22"> 'a list
   </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> empty = []
   <B><FONT COLOR="#A020F0">val</FONT></B> isEmpty = null
   <B><FONT COLOR="#A020F0">val</FONT></B> push = <B><FONT COLOR="#A020F0">op</FONT></B> ::
   <B><FONT COLOR="#A020F0">val</FONT></B> pop = List.getItem
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
and queues 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Queue :&gt; DISPENSER = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">T</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'a list * 'a list
   </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> empty = T ([], [])
   <B><FONT COLOR="#A020F0">val</FONT></B> isEmpty = <B><FONT COLOR="#A020F0">fn</FONT></B> T ([], _) =&gt; true | _ =&gt; false
   <B><FONT COLOR="#A020F0">val</FONT></B> normalize = <B><FONT COLOR="#A020F0">fn</FONT></B> ([], ys) =&gt; (rev ys, []) | q =&gt; q
   <B><FONT COLOR="#A020F0">fun</FONT></B> push (y, T (xs, ys)) = T (normalize (xs, y::ys))
   <B><FONT COLOR="#A020F0">val</FONT></B> pop = <B><FONT COLOR="#A020F0">fn</FONT></B> (T (x::xs, ys)) =&gt; SOME (x, T (normalize (xs, ys))) | _ =&gt; NONE
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
One can now write code that uses either the <tt>Stack</tt> or the <tt>Queue</tt> dispenser.  One can also instantiate the previously defined functor to create functions for manipulating dispensers of a type: 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> S = DispenserAlgs (Stack)
<B><FONT COLOR="#A020F0">val</FONT></B> [<B><FONT COLOR="#5F9EA0">4</FONT></B>,<B><FONT COLOR="#5F9EA0">3</FONT></B>,<B><FONT COLOR="#5F9EA0">2</FONT></B>,<B><FONT COLOR="#5F9EA0">1</FONT></B>] = S.popAll (S.pushAll ([<B><FONT COLOR="#5F9EA0">1</FONT></B>,<B><FONT COLOR="#5F9EA0">2</FONT></B>,<B><FONT COLOR="#5F9EA0">3</FONT></B>,<B><FONT COLOR="#5F9EA0">4</FONT></B>], Stack.empty))

<B><FONT COLOR="#0000FF">structure</FONT></B> Q = DispenserAlgs (Queue)
<B><FONT COLOR="#A020F0">val</FONT></B> [<B><FONT COLOR="#5F9EA0">1</FONT></B>,<B><FONT COLOR="#5F9EA0">2</FONT></B>,<B><FONT COLOR="#5F9EA0">3</FONT></B>,<B><FONT COLOR="#5F9EA0">4</FONT></B>] = Q.popAll (Q.pushAll ([<B><FONT COLOR="#5F9EA0">1</FONT></B>,<B><FONT COLOR="#5F9EA0">2</FONT></B>,<B><FONT COLOR="#5F9EA0">3</FONT></B>,<B><FONT COLOR="#5F9EA0">4</FONT></B>], Queue.empty))
</PRE>
<p>
 
</p>
<p>
There is no dynamic dispatch involved at the module level in SML.  An attempt to do dynamic dispatch 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> q = Q.push (<B><FONT COLOR="#5F9EA0">1</FONT></B>, Stack.empty)
</PRE>
<p>
 
</p>
<p>
will give a type error. 
</p>
<h4 id="head-32ba757ff59bb9cc712ae2effacaf072778872d7">Combining SML Modules and Dynamic Dispatch</h4>
<p>
Let's then combine SML modules and the dynamic dispatch technique introduced in this article.  First we define an interface for dispensers: 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Dispenser = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22">
      <FONT COLOR="#B8860B">I</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> {isEmpty : unit -&gt; bool,
            push : 'a -&gt; 'a t,
            pop : unit -&gt; ('a * 'a t) option}

   </FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> O m (I t) = m t

   <B><FONT COLOR="#A020F0">fun</FONT></B> isEmpty t = O#isEmpty t ()
   <B><FONT COLOR="#A020F0">fun</FONT></B> push (v, t) = O#push t v
   <B><FONT COLOR="#A020F0">fun</FONT></B> pop t = O#pop t ()
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
The <tt>Dispenser</tt> module, which we can think of as an interface for dispensers, implements the <tt>ABSTRACT_DISPENSER</tt> signature using the dynamic dispatch technique, but we leave the signature ascription until later. 
</p>
<p>
Then we define a <tt>DispenserClass</tt> functor that makes a "class" out of a given dispenser module: 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">functor</FONT></B> DispenserClass (D : DISPENSER) : DISPENSER = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> Dispenser

   <B><FONT COLOR="#A020F0">fun</FONT></B> make d =
       I {isEmpty = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; D.isEmpty d,
          push = <B><FONT COLOR="#A020F0">fn</FONT></B> x =&gt; make (D.push (x, d)),
          pop = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt;
                   <B><FONT COLOR="#A020F0">case</FONT></B> D.pop d <B><FONT COLOR="#A020F0">of</FONT></B>
                      NONE =&gt; NONE
                    | SOME (x, d) =&gt; SOME (x, make d)}

   <B><FONT COLOR="#A020F0">val</FONT></B> empty =
       I {isEmpty = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; true,
          push = <B><FONT COLOR="#A020F0">fn</FONT></B> x =&gt; make (D.push (x, D.empty)),
          pop = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; NONE}
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
Finally we seal the <tt>Dispenser</tt> module: 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Dispenser : ABSTRACT_DISPENSER = Dispenser
</PRE>
<p>
 
</p>
<p>
This isn't necessary for type safety, because the unsealed <tt>Dispenser</tt> module does not allow one to break encapsulation, but makes sure that only the <tt>DispenserClass</tt> functor can create dispenser classes (because the constructor <tt>Dispenser.I</tt> is no longer accessible). 
</p>
<p>
Using the <tt>DispenserClass</tt> functor we can turn any concrete dispenser module into a dispenser class: 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> StackClass = DispenserClass (Stack)
<B><FONT COLOR="#0000FF">structure</FONT></B> QueueClass = DispenserClass (Queue)
</PRE>
<p>
 
</p>
<p>
Each dispenser class implements the same dynamic dispatch interface and the <tt>ABSTRACT_DISPENSER</tt> -signature. 
</p>
<p>
Because the dynamic dispatch <tt>Dispenser</tt> module implements the <tt>ABSTRACT_DISPENSER</tt> -signature, we can use it to instantiate the <tt>DispenserAlgs</tt> -functor: 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> D = DispenserAlgs (Dispenser)
</PRE>
<p>
 
</p>
<p>
The resulting <tt>D</tt> module, like the <tt>Dispenser</tt> module, works with any dispenser class and uses dynamic dispatch: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> [<B><FONT COLOR="#5F9EA0">4</FONT></B>, <B><FONT COLOR="#5F9EA0">3</FONT></B>, <B><FONT COLOR="#5F9EA0">2</FONT></B>, <B><FONT COLOR="#5F9EA0">1</FONT></B>] = D.popAll (D.pushAll ([<B><FONT COLOR="#5F9EA0">1</FONT></B>, <B><FONT COLOR="#5F9EA0">2</FONT></B>, <B><FONT COLOR="#5F9EA0">3</FONT></B>, <B><FONT COLOR="#5F9EA0">4</FONT></B>], StackClass.empty))
<B><FONT COLOR="#A020F0">val</FONT></B> [<B><FONT COLOR="#5F9EA0">1</FONT></B>, <B><FONT COLOR="#5F9EA0">2</FONT></B>, <B><FONT COLOR="#5F9EA0">3</FONT></B>, <B><FONT COLOR="#5F9EA0">4</FONT></B>] = D.popAll (D.pushAll ([<B><FONT COLOR="#5F9EA0">1</FONT></B>, <B><FONT COLOR="#5F9EA0">2</FONT></B>, <B><FONT COLOR="#5F9EA0">3</FONT></B>, <B><FONT COLOR="#5F9EA0">4</FONT></B>], QueueClass.empty))
</PRE>
<p>
 
</p>
</div>



<p>
<hr>
Last edited on 2008-10-20 11:18:45 by <span title="otaku.housemarque.fi"><a href="VesaKarvonen">VesaKarvonen</a></span>.
</body></html>