<!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>DefineTypeBeforeUse - 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%;"> DefineTypeBeforeUse <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> <a href = "TitleIndex">Index</a> </table> <div id="content" lang="en" dir="ltr"> <a href="StandardML">Standard ML</a> requires types to be defined before they are used. Because of type inference, the use of a type can be implicit; hence, this requirement is more subtle than it might appear. For example, the following program is not type correct, because the type of <tt>r</tt> is <tt>t option ref</tt>, but <tt>t</tt> is defined after <tt>r</tt>. <pre class=code> <B><FONT COLOR="#A020F0">val</FONT></B> r = ref NONE <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">A</FONT> </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">B</FONT> </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> () = r := SOME A </PRE> <p> </p> <p> MLton reports the following error, indicating that the type defined on line 2 is used on line 1. </p> <pre>Error: z.sml 1.1. Type escapes the scope of its definition at z.sml 2.10. type: t in: val r = ref NONE </pre><p> While the above example is benign, the following example shows how to cast an integer to a function by (implicitly) using a type before it is defined. In the example, the ref cell <tt>r</tt> is of type <tt>t option ref</tt>, where <tt>t</tt> is defined <em>after</em> <tt>r</tt>, as a parameter to functor <tt>F</tt>. This example causes <a href="PolyML">PolyML</a> 4.1.3 to seg fault. <pre class=code> <B><FONT COLOR="#A020F0">val</FONT></B> r = ref NONE <B><FONT COLOR="#0000FF">functor</FONT></B> F (<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> x: t) = <B><FONT COLOR="#0000FF">struct</FONT></B> <B><FONT COLOR="#A020F0">val</FONT></B> () = r := SOME x <B><FONT COLOR="#A020F0">fun</FONT></B> get () = valOf (!r) <B><FONT COLOR="#0000FF">end</FONT></B> <B><FONT COLOR="#0000FF">structure</FONT></B> S1 = F (<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> unit -> unit </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> x = <B><FONT COLOR="#A020F0">fn</FONT></B> () => ()) <B><FONT COLOR="#0000FF">structure</FONT></B> S2 = F (<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> int </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> x = <B><FONT COLOR="#5F9EA0">13</FONT></B>) <B><FONT COLOR="#A020F0">val</FONT></B> () = S1.get () () </PRE> </p> <p> MLton reports the following error. </p> <pre>Warning: z.sml 1.1. Unable to locally determine type of variable: r. type: ??? option ref in: val r = ref NONE Error: z.sml 1.1. Type escapes the scope of its definition at z.sml 2.17. type: t in: val r = ref NONE </pre></div> <p> <hr> Last edited on 2005-12-01 03:38:39 by <span title="ppp-71-139-183-221.dsl.snfc21.pacbell.net"><a href="StephenWeeks">StephenWeeks</a></span>. </body></html>