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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html >
<head><title>Classes</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)">
<meta name="originator" content="TeX4ht (http://www.tug.org/tex4ht/)">
<!-- 3,html -->
<meta name="src" content="nipguide.tex">
<link rel="stylesheet" type="text/css" href="nipguide.css">
</head><body
>
<!--l. 1045--><div class="crosslinks"><p class="noindent">[<a
href="nipguidese34.html" >next</a>] [<a
href="nipguidese31.html" >prev</a>] [<a
href="nipguidese31.html#tailnipguidese31.html" >prev-tail</a>] [<a
href="nipguidese31.html#tailnipguidese32.html">tail</a>] [<a
href="nipguidech6.html#nipguidese33.html" >up</a>] </p></div>
<h3 class="sectionHead"><span class="titlemark">6.11 </span> <a
id="x43-750006.11"></a>Classes</h3>
<!--l. 1046--><p class="noindent" ><a name="nip_label_sec:class"></a>
<!--l. 1048--><p class="indent" > You can define new types using <span
class="phvr7t-x-x-80">class</span>. For example:
<div class="verbatim" id="verbatim-67">
Pasta_plain = class {
 <br />  lasagne = "large sheets";
 <br />  fusilli = "sort of twisty";
 <br />  radiatori = "lots of ridges";
 <br />}
</div>
<!--l. 1056--><p class="nopar" >
<!--l. 1058--><p class="noindent" >This defines a new class called <span
class="phvr7t-x-x-80">Pasta</span><span
class="phvr7t-x-x-80">_plain</span>. The class has
three members (<span
class="phvr7t-x-x-80">lasagne</span>, <span
class="phvr7t-x-x-80">fusilli </span>and <span
class="phvr7t-x-x-80">radiatori</span>), each of which
has a list of <span
class="phvr7t-x-x-80">char </span>as its value. By convention, we’ve named
classes with an initial capital letter, but of course you can do
what you like.
<!--l. 1064--><p class="indent" > You can refer to the members of a class using the class
project (<span
class="phvr7t-x-x-80">.</span>) operator. For example:
<div class="verbatim" id="verbatim-68">
Pasta_plain.lasagne == "large sheets"
</div>
<!--l. 1069--><p class="nopar" >
<!--l. 1071--><p class="noindent" >You can use an expression to the right of <span
class="phvr7t-x-x-80">. </span>if you enclose it
in brackets. For example:
<div class="verbatim" id="verbatim-69">
Pasta_plain.("las" ++ "agne") ==
 <br />  "large sheets"
</div>
<!--l. 1078--><p class="nopar" >
<!--l. 1080--><p class="indent" > Classes can contain any objects as members, including
functions and sub-classes. Functions may define local
classes, classes may define local functions, and all may
refer to each other using the usual scope rules. For
example:
<div class="verbatim" id="verbatim-70">
Pasta_all = class {
 <br />  filled = class {
 <br />    tortelloni = "venus' navel";
 <br />    ravioli = "square guys";
 <br />  }
 <br />  plain = Pasta_plain;
 <br />}
</div>
<!--l. 1093--><p class="nopar" >
<!--l. 1095--><p class="indent" > When you define a class, <span
class="phvr7t-x-x-80">nip2 </span>adds a few extra members
for you. <span
class="phvr7t-x-x-80">name </span>is a list of <span
class="phvr7t-x-x-80">char </span>giving the name of the class.
<span
class="phvr7t-x-x-80">this </span>and <span
class="phvr7t-x-x-80">super </span>are the most-enclosing class instance and the
class instance this class is derived from (see <span
class="cmsy-10"></span><a
href="#x43-770006.11.2">6.11.2<!--tex4ht:ref: sec:inheritance --></a>).
<span
class="phvr7t-x-x-80">nip2 </span>also adds a default constructor: a member with
the same name as the class, pointing back to the class
constructor.
<!--l. 1102--><p class="indent" > For efficiency reasons <span
class="phvr7t-x-x-80">nip2 </span>does not allow mutual
recursion at the top level. If two functions depend on each
other, neither will ever be calculated. For example:
<div class="verbatim" id="verbatim-71">
a = 1 : b;
 <br />b = 2 : a;
</div>
<!--l. 1109--><p class="nopar" >
<!--l. 1111--><p class="noindent" >Neither <span
class="phvr7t-x-x-80">a </span>nor <span
class="phvr7t-x-x-80">b </span>will have a value.
<!--l. 1114--><p class="indent" > You can have mutual recursion between class members.
For example:
<div class="verbatim" id="verbatim-72">
Fred = class {
 <br />  a = 1 : b;
 <br />  b = 2 : a;
 <br />}
</div>
<!--l. 1121--><p class="nopar" >
<!--l. 1123--><p class="noindent" >Now <span
class="phvr7t-x-x-80">Fred.a </span>will have the value <span
class="phvr7t-x-x-80">[1, 2, 1, 2, 1, </span><span
class="phvr7t-x-x-80">…]</span>.
<!--l. 1126--><p class="noindent" >
<h4 class="subsectionHead"><span class="titlemark">6.11.1 </span> <a
id="x43-760006.11.1"></a>Parameterised classes</h4>
<!--l. 1128--><p class="noindent" >Classes can have parameters. Parameters behave like class
members initialised from arguments to the class constructor.
For example:
<div class="verbatim" id="verbatim-73">
My_pasta pasta_name cooked = class {
 <br />  is_ready t = "your " ++
 <br />    pasta_name ++ " is " ++ state
 <br />  {
 <br />    state
 <br />      = "underdone!", t < cooked
 <br />      = "perfect", t == cooked
 <br />      = "yuk!";
 <br />  }
 <br />}
</div>
<!--l. 1142--><p class="nopar" >
<!--l. 1144--><p class="noindent" >This defines a class called <span
class="phvr7t-x-x-80">My</span><span
class="phvr7t-x-x-80">_pasta </span>which takes a pasta
name and a cooking time as parameters. Once you have
made an instance of <span
class="phvr7t-x-x-80">My</span><span
class="phvr7t-x-x-80">_pasta</span>, you can test if it’s been
cooked at a certain time with the <span
class="phvr7t-x-x-80">is</span><span
class="phvr7t-x-x-80">_ready </span>member. For
example:
<div class="verbatim" id="verbatim-74">
tele = My_pasta "telephoni" 10;
 <br />tele.is_ready 5 ==
 <br />  "your telephoni is underdone!"
</div>
<!--l. 1154--><p class="nopar" >
<!--l. 1156--><p class="noindent" >
<h4 class="subsectionHead"><span class="titlemark">6.11.2 </span> <a
id="x43-770006.11.2"></a>Inheritance</h4>
<!--l. 1157--><p class="noindent" ><a name="nip_label_sec:inheritance"></a>
<!--l. 1159--><p class="indent" > Classes can inherit from a super-class. For example:
<div class="verbatim" id="verbatim-75">
Pasta_more = class Pasta_plain {
 <br />  macaroni = "tubes";
 <br />  spaghetti = "long and thin";
 <br />  lasagne = "fairly large sheets";
 <br />}
</div>
<!--l. 1167--><p class="nopar" >
<!--l. 1169--><p class="noindent" >Here the new class <span
class="phvr7t-x-x-80">Pasta</span><span
class="phvr7t-x-x-80">_more </span>inherits members from the
previous class <span
class="phvr7t-x-x-80">Pasta</span><span
class="phvr7t-x-x-80">_plain</span>. It also overrides the definition of
<span
class="phvr7t-x-x-80">lasagne </span>from <span
class="phvr7t-x-x-80">Pasta</span><span
class="phvr7t-x-x-80">_plain </span>with a new value. For example:
<div class="verbatim" id="verbatim-76">
Pasta_more.macaroni == "tubes"
 <br />Pasta_more.fusilli == "sort of twisty"
 <br />Pasta_more.lasagne == "fairly large sheets"
</div>
<!--l. 1178--><p class="nopar" >
<!--l. 1180--><p class="indent" > You can use <span
class="phvr7t-x-x-80">this </span>and <span
class="phvr7t-x-x-80">super </span>to refer to other members up
and down the class hierarchy. <span
class="phvr7t-x-x-80">super </span>is the class instance that
the current class inherits from (if there’s no super-class,
<span
class="phvr7t-x-x-80">super </span>has the value <span
class="phvr7t-x-x-80">[]</span>), and <span
class="phvr7t-x-x-80">this </span>is the most-enclosing class
instance.
<div class="verbatim" id="verbatim-77">
Pasta_more.super == Pasta_plain
 <br />Pasta_more.this == Pasta_more
 <br />Pasta_more.super.this == Pasta_more
</div>
<!--l. 1190--><p class="nopar" >
<!--l. 1192--><p class="noindent" >therefore:
<div class="verbatim" id="verbatim-78">
Pasta_more.lasagne == "fairly large sheets"
 <br />Pasta_more.super.lasagne == "large sheets"
 <br />Pasta_more.super.this.lasagne ==
 <br />  "fairly large sheets"
</div>
<!--l. 1200--><p class="nopar" >
<!--l. 1202--><p class="indent" > There’s a special symbol <span
class="phvr7t-x-x-80">root </span>which encloses all symbols.
For example:
<div class="verbatim" id="verbatim-79">
fred = 12;
 <br />
 <br />Freddage = class {
 <br />  fred = 42;
 <br />  mystery = root.fred;
 <br />}
</div>
<!--l. 1212--><p class="nopar" >
<!--l. 1214--><p class="noindent" >Now <span
class="phvr7t-x-x-80">Fred.mystery </span>will have the value 12.
<!--l. 1217--><p class="indent" > There’s another special symbol called <span
class="phvr7t-x-x-80">scope </span>which
encloses all symbols in the file this definition was loaded
from. If you want to refer to another definition in the same
file which is being masked somehow, use <span
class="phvr7t-x-x-80">scope</span>.
<!--l. 1221--><p class="indent" > You can use the built in function <span
class="phvr7t-x-x-80">is</span><span
class="phvr7t-x-x-80">_instanceof </span>to test
whether an instance is or inherits from a class. For
example:
<div class="verbatim" id="verbatim-80">
is_instanceof "Pasta_more" Pasta_more == true
 <br />is_instanceof "Pasta_plain" Pasta_more == true
 <br />is_instanceof "Pasta_more" Pasta_plain ==
 <br />  false
</div>
<!--l. 1229--><p class="nopar" >
<!--l. 1231--><p class="indent" > The super-class constructor can take arguments, and these
arguments can refer to class members. For example:
<div class="verbatim" id="verbatim-81">
Fresh_pasta pasta_name = class
 <br />  My_pasta pasta_name cooked {
 <br />  cooked = 2;
 <br />}
</div>
<!--l. 1239--><p class="nopar" >
<!--l. 1241--><p class="noindent" >Defines a class for fresh pasta, which always cooks in 2
minutes. You need to be careful not to make loops: if <span
class="phvr7t-x-x-80">cooked</span>
did tried to refer to something in the super-class, this class
would never construct properly. <span
class="phvr7t-x-x-80">nip2 </span>unfortunately does not
check for this error.
<!--l. 1247--><p class="indent" > Finally, the superclass can be a fully constructed
class. In this case, the superclass is cloned and the new
class members wrapped around it. You can use this
to write a class which can wrap any other class and
add members to it. Many of the toolkit menu items
use this trick to enable them to work for any object
type.
<!--l. 1253--><p class="noindent" >
<h4 class="subsectionHead"><span class="titlemark">6.11.3 </span> <a
id="x43-780006.11.3"></a>Minor class features</h4>
<!--l. 1255--><p class="noindent" >There are a couple of other things you can do with classes.
You can define a special member called <span
class="phvr7t-x-x-80">_check</span>. If this
member is defined, then when a class instance is created, the
check member is returned instead of the class itself. You can
use this to implement class argument type checks, for
example:
<div class="verbatim" id="verbatim-82">
Fred a b = class {
 <br />  _check
 <br />    = this, is_real a && is_real b
 <br />    = error "args to Fred must " ++
 <br />      "both be real"
 <br />}
</div>
<!--l. 1267--><p class="nopar" >
<!--l. 1269--><p class="noindent" >Defines a class called <span
class="phvr7t-x-x-80">Fred </span>which has to have two real
numbers as arguments.
<!--l. 1273--><p class="indent" > You can define members called <span
class="phvr7t-x-x-80">oo</span><span
class="phvr7t-x-x-80">_binary</span>, <span
class="phvr7t-x-x-80">oo</span><span
class="phvr7t-x-x-80">_binary’ </span>and
<span
class="phvr7t-x-x-80">oo</span><span
class="phvr7t-x-x-80">_unary </span>and do operator overloading. When <span
class="phvr7t-x-x-80">nip2 </span>sees one of
the standard operators being used on an instance of your
class, it will look up one of these members and pass in the
name of the operator and the argument. The two forms of
the binary operator member are called for the class-on-left
and the class-on-rights cases. So:
<div class="verbatim" id="verbatim-83">
x = Fred 1 2
 <br />x + 12 == x.oo_binary "add" 12
 <br />12 + x == x.oo_binary' "add" 12
 <br />!x == x.oo_unary "negate"
</div>
<!--l. 1285--><p class="nopar" >
<!--l. 1287--><p class="indent" > These two features are very primitive. The <span
class="phvr7t-x-x-80">_Object </span>class in
the <span
class="phvr7t-x-x-80">_types </span>toolkit builds on these to provide a fairly
high-level system for checking class arguments and defining
the meaning of operators. See <span
class="cmsy-10"></span><a
href="nipguidese34.html#x45-860006.13">6.13<!--tex4ht:ref: sec:object --></a>.
<!--l. 1292--><div class="crosslinks"><p class="noindent">[<a
href="nipguidese34.html" >next</a>] [<a
href="nipguidese31.html" >prev</a>] [<a
href="nipguidese31.html#tailnipguidese31.html" >prev-tail</a>] [<a
href="nipguidese32.html" >front</a>] [<a
href="nipguidech6.html#nipguidese33.html" >up</a>] </p></div>
<!--l. 1292--><p class="indent" > <a
id="tailnipguidese32.html"></a>
</body></html>
|