File: language.html

package info (click to toggle)
nice 0.9.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,260 kB
  • ctags: 6,867
  • sloc: java: 42,604; xml: 3,205; lisp: 1,079; makefile: 652; sh: 342; cpp: 21; awk: 3
file content (476 lines) | stat: -rw-r--r-- 16,529 bytes parent folder | download | duplicates (4)
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
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <meta name="Author" content="Daniel Bonniot">
   <meta name="GENERATOR" content="Mozilla/4.76 [en] (X11; U; OSF1 V4.0 alpha) [Netscape]">
   <title>The Nice programming language</title>
<link rel=stylesheet href="style.css">
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#0000EF" vlink="#51188E" alink="#FF0000">

<center>
<h1>
The <font face="Comic Sans MS">Nice</font> programming language</h1></center>

<blockquote>This page is intended to provide a gentle introduction to the
Nice programming language. The goal is to help you write your first Nice
programs. It does not describe every feature of the language, nor gives
it a complete description of the powerful type system.
<br>&nbsp;

<h2>Requirements</h2>

For simplicity and conciseness, this tutorial presents Nice as an extension
of the Java programming language. You should therefore be familiar with
Java. If not, you could read about it, for example 
<a href="http://java.sun.com/docs/books/tutorial/java/index.html">
Javasoft's tutorial</a>.
<br>&nbsp;

<h2>Declaring classes and methods</h2>

Classes and methods can be declared as in Java:
<PRE>
<FONT COLOR="#0000ee">class</FONT> <FONT COLOR="#cd0000">Person</FONT> 
{ 
  <FONT COLOR="#b7860b">String </FONT><FONT COLOR="#cd0000">name</FONT>; 
  <FONT COLOR="#b7860b">int</FONT> <FONT COLOR="#cd0000">age</FONT>; 
  
  <FONT COLOR="#b7860b">String </FONT><FONT COLOR="#cd0000">display</FONT>(); 
} 

<FONT COLOR="#0000ee">class</FONT> <FONT COLOR="#cd0000">Worker</FONT> <FONT COLOR="#0000ee">extends</FONT> <FONT COLOR="#b7860b">Person </FONT>
{ 
  <FONT COLOR="#b7860b">int</FONT> <FONT COLOR="#cd0000">salary</FONT>; 
} 
</PRE>
<p>Note that <tt>String display(); </tt><b>declares</b> a method, that
is informs that this method exists. Now we have to <b>implement</b> it, that
is tell what code is to be executed, depending on the runtime type of the
person (in this short example either <tt>Person</tt> or <tt>Worker</tt>).
<br>&nbsp;

<h2>Implementing methods</h2>

Method implementations can be placed outside of classes. 
Their order does not matter. 
The implementations of a single method may even occur in several files
(this is an important feature that allows modularity).
<p>So after the two above class definitions, we write two
<b>implementations</b> for method <tt>display</tt>:
<PRE>
<FONT COLOR="#cd0000">display</FONT>(<FONT COLOR="#b7860b">Person</FONT> p) 
{ 
  <FONT COLOR="#0000ee">return</FONT> p.name + <FONT COLOR="#a020f0">&quot; (age=&quot;</FONT> + p.age + <FONT COLOR="#a020f0">&quot;)&quot;</FONT>; 
} 

<FONT COLOR="#cd0000">display</FONT>(<FONT COLOR="#b7860b">Worker</FONT> p) 
{ 
  <FONT COLOR="#0000ee">return</FONT> p.name + <FONT COLOR="#a020f0">&quot; (age+&quot;</FONT> + p.age + <FONT COLOR="#a020f0">&quot;, salary=&quot;</FONT> + p.salary + <FONT COLOR="#a020f0">&quot;)&quot;</FONT>; 
} 
</PRE>

<br>&nbsp;

<h2>Multiple dispatch</h2>

In Nice, the choice of the method implementation is made at run-time, based
on <b>all</b> parameters (in java, only the implicit parameter
<tt>this</tt> is used to choose the alternative). 
Such methods are thus called <b>multi-methods</b>.
<p>
Let's take the example of the <tt>equals</tt> method, 
that tests if any two objects are equal.
<br>&nbsp;
<br>&nbsp;
<center><table BORDER WIDTH="85%" BGCOLOR="#CCCCCC" >
<tr>
<td>
<center>Java</center>
</td>

<td>
<center>Nice</center>
</td>
</tr>

<tr>
<td>
<blockquote>
<pre>class Person
{
&nbsp; String name;
&nbsp; int age;

&nbsp; boolean equals(Object that)
&nbsp; {
&nbsp;&nbsp;&nbsp; <font color="#FF0000">if(!(that instanceof Person))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;
</font>&nbsp;&nbsp;&nbsp; return&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name.equals((<font color="#FF0000">(Person)</font> that).name)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; age==(<font color="#FF0000">(Person)</font> that).age;
&nbsp; }
}</pre>
</blockquote>
</td>

<td>
<blockquote>
<pre><font color="#0000EE">class</font> <font color="#CD0000">Person
</font>{
&nbsp; <font color="#B7860B">String</font> <font color="#CD0000">name</font>;
&nbsp; <font color="#B7860B">int</font> <font color="#CD0000">age</font>;
}

<font color="#CD0000">equals</font>(<font color="#B7860B">Person</font> p1, <font color="#B7860B">Person</font> p2) =
&nbsp; p1.name.equals(p2.name) &amp;&amp;
&nbsp; p1.age==p2.age;</pre>
</blockquote>
</td>
</tr>
</table></center>

<p>In the Nice version, this implementation of <tt>equals</tt> will be executed
when both parameters are instances of class <tt>Person</tt>. 
So the type of the second argument is also known, and no manual 
<tt>instanceof</tt> and no cast are necessary (red parts of the java code). 
This job is automatically done by the compiler for you. 
The code looks cleaner, it is simpler to understand,
and it is automatically guaranteed that no runtime exception will occur
(this simple java code would not break either, but you have to think about
it to get this confidence, and it becomes extremely difficult in large
projects).
<p>Another great advantage of multi-methods is that they offer an attractive
alternative to the <b>Visitor Pattern</b>. 
<a href="visitor.html">This solution is presented in a full example</a>.

<p>

There is a 
<a
href="http://nice.sourceforge.net/cgi-bin/twiki/view/Doc/FunctionsAndMethods">
more detailed introduction to methods in Nice</a> on the Wiki.

<br>&nbsp;

<br>&nbsp;

<h2>Parametric classes</h2>

Classes and interfaces can have type parameters. For example, the <tt>Collection</tt>
interface is parameterized by the type of its elements:
<p>
<PRE>
<FONT COLOR="#0000ee">interface</FONT> <FONT COLOR="#cd0000">Collection</FONT>&lt;T&gt; 
{ 
  ... 
} 
</PRE>
<p>If a class (resp. interface) has type parameters, then all its sub-classes
(resp. sub-interfaces and implementing classes) must have the same type
parameters.
<p>
<PRE>
<FONT COLOR="#0000ee">class</FONT> <FONT COLOR="#cd0000">LinkedList</FONT>&lt;T&gt; <FONT COLOR="#0000ee">implements</FONT> <FONT COLOR="#b7860b">Collection&lt;T&gt; </FONT>
{ 
  <FONT COLOR="#b7860b">T </FONT><FONT COLOR="#cd0000">head</FONT>;
  <FONT COLOR="#b7860b">LinkedList&lt;T&gt; </FONT><FONT COLOR="#cd0000">tail</FONT>; 
} 
</PRE>
<p>A consequence of this rule is that there is no class (like <tt>Object</tt>
in Java) that is an ancestor of all classes. There could not be, since
all classes do not have the same number of type parameters.
<br>However, it is possible to express that a method takes arguments of
any type. For instance the <tt>equals</tt> method is declared in Nice:
<br>
<PRE>
&lt;<FONT COLOR="#b7860b">T</FONT>&gt; <FONT COLOR="#b7860b">boolean</FONT> <FONT COLOR="#cd0000">equals</FONT>(<FONT COLOR="#b7860b">T</FONT>, <FONT COLOR="#b7860b">T</FONT>); 
</PRE>
<p>
One can read this as 
"for Any type T, the method <tt>equals</tt> takes two objects of type T, 
and returns a boolean".
<p>Thanks to the usual subsumption rule, this type makes it possible to
call <tt>equals</tt> with arguments of different type, 
as long as they are compatible (have a common super type). 
For instance, it's legal to use <tt>equals</tt> to compare expressions 
of type <tt>Collection&lt;int></tt> and <tt>LinkedList&lt;int></tt>,
while it is not with types <tt>Collection&lt;String></tt> and <tt>String</tt>.
<p>This approach is more sensible than Java's one, where the latter would
be allowed and would always return false (not even raising a runtime error),
while it is very likely a bug to compare a collection of strings to a string.
<p>Note that it is also possible to define a function that takes two unrelated
and unconstrained types. So it would be possible to define <tt>equals</tt> 
to have the same typing behaviour it has in Java:
<br><PRE>&lt;<FONT COLOR="#b7860b">T</FONT>, <FONT COLOR="#b7860b">U</FONT>&gt; <FONT COLOR="#b7860b">boolean</FONT> <FONT COLOR="#cd0000">equals</FONT>(<FONT COLOR="#b7860b">T</FONT>, <FONT COLOR="#b7860b">U</FONT>);</PRE>

<h2>Precise types</h2>

Java's type system is too simple to express many useful types. 
For instance, let's suppose we want to declare the method <tt>filter</tt> 
on collections. This method applies a function to each element of 
the collection, and returns a new collection containing the elements for which
that function returns <tt>true</tt>. 
Furthermore, the new collection is an instance of the same class as 
the original collection: 
<tt>filter</tt> applied to a <tt>List</tt> return a new <tt>List</tt>,
<tt>filter</tt> applied to a <tt>Vector</tt> return a new <tt>Vector</tt>,
...
<br>&nbsp;
<br>&nbsp;
<center><table BORDER WIDTH="85%" BGCOLOR="#CCCCCC" >
<tr>
<td>
<center>Java</center>
</td>

<td>
<center>Nice</center>
</td>
</tr>

<tr>
<td>
<blockquote>
<pre>interface BoolFunction
{
&nbsp; boolean apply(Object);
}

interface Collection
{
&nbsp; Collection filter(BoolFunction f);
}

interface List extends Collection
{ ... }

static void main(String[] args)
{
&nbsp; List l1;
&nbsp; BoolFunction f;
&nbsp; ...
&nbsp; List l2 = <font color="#FF0000">(List)</font> l1.filter(f);
}</pre>
</blockquote>
</td>

<td>
<blockquote>
<pre><font color="#0000EE">interface</font> <font color="#CD0000">Collection</font>&lt;T>
{
&nbsp; <font color="#0000EE">alike</font>&lt;T> <font color="#CD0000">filter</font>(T-><font color="#B7860B">boolean</font> f);
}

<font color="#0000EE">interface</font> <font color="#CD0000">List</font>&lt;T>
<font color="#0000EE">extends</font> <font color="#B7860B">Collection</font>&lt;T>&nbsp;
{ ... }

<font color="#CD0000">void main</font>(String[] args)
{
&nbsp; <font color="#B7860B">List</font>&lt;<font color="#B7860B">int</font>> <font color="#CD0000">l1</font>;
&nbsp; <font color="#B7860B">int</font>-><font color="#B7860B">boolean</font> f;
&nbsp; ...
&nbsp; <font color="#B7860B">List</font>&lt;<font color="#B7860B">int</font>> <font color="#CD0000">l2</font> = l1.filter(f);
}</pre>
</blockquote>
</td>
</tr>
</table></center>

<p>In the Nice version, the <tt>alike</tt> keyword is a type that means 
"the same type as the implicit receiver argument".
<p>Note that there is not special treatment for the <tt>alike</tt> construct in
the core type system. 
<TT>alike</TT> is just syntactic sugar for a more general concept:
polymorphic constrained types.
<p>
The <tt>clone</tt> example might be more familiar to Java users. In Java, the
<tt>clone</tt> method, defined in class Object, has a Object return type. 
In fact, one would like to express that the <tt>clone</tt> method returns 
an object of the same type as its argument. 
This is possible in Nice, and it allows for cast-less use of <tt>clone</tt>.
<br>&nbsp;
<center><table BORDER WIDTH="85%" BGCOLOR="#CCCCCC" >
<tr>
<td>
<center>Java</center>
</td>

<td>
<center>Nice</center>
</td>
</tr>

<tr>
<td>
<blockquote>
<pre>class Object
{
&nbsp; Object clone();
&nbsp; ...
}

void main(String[] args)
{
&nbsp; java.util.Date d1;
&nbsp; ...
&nbsp; java.util.Date d2 = <font color="#FF0000">(Date)</font> d1.clone();
}</pre>
</blockquote>
</td>

<td>
<blockquote>
<pre>&lt;T> T <font color="#CD0000">clone</font>(T);

<font color="#CD0000">void main</font>(String[] args)
{
&nbsp; java.util.Date d1;
&nbsp; ...
&nbsp; java.util.Date d2 = d1.clone();
}</pre>
</blockquote>
</td>
</tr>
</table></center>

<br>&nbsp;

<h2>Instructions and expressions</h2>

The code of a method implementation, that goes between <tt>{</tt> and
<tt>}</tt>, can be almost any legal Java code.
<br>Here is a list of the differences:
<center><b>Missing constructs</b></center>

<ul>
<li>
<b>No casts</b>. We claim that our powerful type system makes it possible
to avoid nearly every cast used in other object-oriented languages (say
95% of them!). For the 5% remaining, it is possible to write methods that
do the same job, except you have to explicitly write what happens if
the "cast" fails, which is a nice property.</li>

<li>
<b>Visibility modifiers</b> (<tt>public, private</tt>) are accepted for
class members, but are currently ignored. There is no theoretical problem
to add visibility modifiers. We are <a
href="roadmap.html">planning</a> 
to support them before version <tt>1.0</tt>.</li>
</ul>

<center><b>Additional constructs</b></center>

<ul>
<li>
<b>Closures</b> (functions written inside expressions). The expression
<tt>(T1
p1, T2 p2)=>e</tt> is the two parameters function that returns
<tt>e</tt>.
The return type of the function is computed, so you don't have to write
it. Parameters <tt>p1</tt> and <tt>p2 </tt>can of course occur in <tt>e</tt>.
Outside variables can also safely occur in e, in which case their <b>reference</b>
is captured (not their value). An alternate syntax is <tt>(T1 p1, T2
p2)=>{ inst1; ...; instn; }</tt>. Every execution of the body should lead
to a return statement. The return type of the function is the lowest common
type of the returned expressions.</li>

<li>
<b>Functional arguments</b>. A function or a method can have functional
arguments. The syntax for functional types is <tt>(T1, T2, ..., Tn)
-> T</tt>. The parentheses can be omitted if there is only one argument type:
<tt>int->int</tt>.
</li>

<li>
<b>Functional-like syntax for method implementations</b>. One can write <tt>f(P x)
= e; </tt>as an equivalent form for <tt>f(P x) { return e; }</tt>. This
is very handy when defining methods that return simple values, depending
on the type of the argument -- that is doing <i>pattern-matching</i>.</li>

<li>
<b>Tuples</b>. Tuples allow for grouping several values in a single expression,
without the pain of creating a specific class. For instance, <tt>("String",
2*3, x.toString())</tt> is a tuple (a triplet in that case). Its type
is written <tt>(String, int, String)</tt>. Tuple types are covariant,
so a couple of <tt>int</tt> can be used where a couple of <tt>long</tt>
is expected. A funny feature is that swapping two variables can be done
without a temporary variable, using tuples: <tt>(x, y) = (y, x)</tt>.</li>

<li>
<b>Named and optional parameters</b>. If a method is declared with named
parameters, it is possible to specify them at the call site, which makes
it unnecessary to remember the order of the parameters, and makes the call
clearer. For instance:</li>

<br><tt>void copy(File from, File to);</tt>
<br><tt>...</tt>
<br><tt>copy(from: f1, to: f2);</tt>
<br><tt>copy(to: f3, from: f4);</tt>
<p>It is possible to omit the names at the call site, in which case the
usual declaration order is used:
<br><tt>copy(f1, f2); // from f1 to f2</tt>
<p>Additionally, named parameters can be given a default value, in which
case their presence is optional at the call site.
<p><tt>void copy(File from, File to, int bufferSize = 1000);</tt>
<br><tt>...</tt>
<br><tt>copy(from: f1, to: f2); // with a buffer size of 1000</tt>
<br><tt>copy(from: f1, to: f2, bufferSize: 4000);</tt>
<br>&nbsp;
<br>&nbsp;</ul>

<h2>The main function</h2>

The main function is the start of any program.
It is a normal function, that takes as its parameters
the array containing the arguments of the program.

<PRE>
void main(String[] args)
{
  System.out.println("Hello, world!");
}
</PRE>

<h2>Interfacing with Java</h2>

It is possible to use java classes and methods from Nice programs. This
is a great advantage, since it gives access to the gigantic and ever-growing
set of Java libraries.
<p>One can just use java classes in types, and call java methods in Nice
code. It is not necessary to explicitly import classes or methods. An
import statement can be used if one does not want to repeat a package
name.
<p><tt>import java.io.*;</tt>
<p><tt>{</tt>
<br><tt>&nbsp; String s;</tt>
<br><tt>&nbsp; ...</tt>
<br><tt>&nbsp; java.io.Writer w = new FileWriter(s);</tt>
<br><tt>&nbsp; w.write("Hello world");</tt>
<br><tt>&nbsp; w.close();</tt>
<br><tt>}</tt>
<br>&nbsp;
<p>
More advanced usage is described in the 
<a href="manual.html#interfacingWithJava">corresponding section
of the User's manual</a>: giving more precise types to Java methods, 
calling Nice code from a Java program, ...
<br>&nbsp;

<h2>To go further</h2>

Chech this
<a href="http://nice.sourceforge.net/cgi-bin/twiki/view/Doc/QuickIntroduction">
list of documents to get started</a> with Nice.

For a complete description of the language, see
<a href="manual.html">the User's manual</a>.

<h2>General information about Nice : 
the <a href="index.html">Nice home page</a></h2>
</blockquote>

</body>
</html>