File: next-tutorial.html

package info (click to toggle)
nsf 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,992 kB
  • sloc: ansic: 31,929; tcl: 10,357; sh: 524; pascal: 170; lisp: 41; makefile: 24
file content (3472 lines) | stat: -rw-r--r-- 178,441 bytes parent folder | download
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<meta name="generator" content="AsciiDoc 8.6.9" />
<title>Tutorial for the Next Scripting Language</title>
<style type="text/css">
/* Shared CSS for AsciiDoc xhtml11 and html5 backends */

/* Default font. */
body {
  font-family: Georgia,serif;
}

/* Title font. */
h1, h2, h3, h4, h5, h6,
div.title, caption.title,
thead, p.table.header,
#toctitle,
#author, #revnumber, #revdate, #revremark,
#footer {
  font-family: Arial,Helvetica,sans-serif;
}

body {
  margin: 1em 5% 1em 5%;
}

a {
  color: blue;
  text-decoration: underline;
}
a:visited {
  color: fuchsia;
}

em {
  font-style: italic;
  color: navy;
}

strong {
  font-weight: bold;
  color: #083194;
}

h1, h2, h3, h4, h5, h6 {
  color: #527bbd;
  margin-top: 1.2em;
  margin-bottom: 0.5em;
  line-height: 1.3;
}

h1, h2, h3 {
  border-bottom: 2px solid silver;
}
h2 {
  padding-top: 0.5em;
}
h3 {
  float: left;
}
h3 + * {
  clear: left;
}
h5 {
  font-size: 1.0em;
}

div.sectionbody {
  margin-left: 0;
}

hr {
  border: 1px solid silver;
}

p {
  margin-top: 0.5em;
  margin-bottom: 0.5em;
}

ul, ol, li > p {
  margin-top: 0;
}
ul > li     { color: #aaa; }
ul > li > * { color: black; }

.monospaced, code, pre {
  font-family: "Courier New", Courier, monospace;
  font-size: inherit;
  color: navy;
  padding: 0;
  margin: 0;
}
pre {
  white-space: pre-wrap;
}

#author {
  color: #527bbd;
  font-weight: bold;
  font-size: 1.1em;
}
#email {
}
#revnumber, #revdate, #revremark {
}

#footer {
  font-size: small;
  border-top: 2px solid silver;
  padding-top: 0.5em;
  margin-top: 4.0em;
}
#footer-text {
  float: left;
  padding-bottom: 0.5em;
}
#footer-badges {
  float: right;
  padding-bottom: 0.5em;
}

#preamble {
  margin-top: 1.5em;
  margin-bottom: 1.5em;
}
div.imageblock, div.exampleblock, div.verseblock,
div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
div.admonitionblock {
  margin-top: 1.0em;
  margin-bottom: 1.5em;
}
div.admonitionblock {
  margin-top: 2.0em;
  margin-bottom: 2.0em;
  margin-right: 10%;
  color: #606060;
}

div.content { /* Block element content. */
  padding: 0;
}

/* Block element titles. */
div.title, caption.title {
  color: #527bbd;
  font-weight: bold;
  text-align: left;
  margin-top: 1.0em;
  margin-bottom: 0.5em;
}
div.title + * {
  margin-top: 0;
}

td div.title:first-child {
  margin-top: 0.0em;
}
div.content div.title:first-child {
  margin-top: 0.0em;
}
div.content + div.title {
  margin-top: 0.0em;
}

div.sidebarblock > div.content {
  background: #ffffee;
  border: 1px solid #dddddd;
  border-left: 4px solid #f0f0f0;
  padding: 0.5em;
}

div.listingblock > div.content {
  border: 1px solid #dddddd;
  border-left: 5px solid #f0f0f0;
  background: #f8f8f8;
  padding: 0.5em;
}

div.quoteblock, div.verseblock {
  padding-left: 1.0em;
  margin-left: 1.0em;
  margin-right: 10%;
  border-left: 5px solid #f0f0f0;
  color: #888;
}

div.quoteblock > div.attribution {
  padding-top: 0.5em;
  text-align: right;
}

div.verseblock > pre.content {
  font-family: inherit;
  font-size: inherit;
}
div.verseblock > div.attribution {
  padding-top: 0.75em;
  text-align: left;
}
/* DEPRECATED: Pre version 8.2.7 verse style literal block. */
div.verseblock + div.attribution {
  text-align: left;
}

div.admonitionblock .icon {
  vertical-align: top;
  font-size: 1.1em;
  font-weight: bold;
  text-decoration: underline;
  color: #527bbd;
  padding-right: 0.5em;
}
div.admonitionblock td.content {
  padding-left: 0.5em;
  border-left: 3px solid #dddddd;
}

div.exampleblock > div.content {
  border-left: 3px solid #dddddd;
  padding-left: 0.5em;
}

div.imageblock div.content { padding-left: 0; }
span.image img { border-style: none; vertical-align: text-bottom; }
a.image:visited { color: white; }

dl {
  margin-top: 0.8em;
  margin-bottom: 0.8em;
}
dt {
  margin-top: 0.5em;
  margin-bottom: 0;
  font-style: normal;
  color: navy;
}
dd > *:first-child {
  margin-top: 0.1em;
}

ul, ol {
    list-style-position: outside;
}
ol.arabic {
  list-style-type: decimal;
}
ol.loweralpha {
  list-style-type: lower-alpha;
}
ol.upperalpha {
  list-style-type: upper-alpha;
}
ol.lowerroman {
  list-style-type: lower-roman;
}
ol.upperroman {
  list-style-type: upper-roman;
}

div.compact ul, div.compact ol,
div.compact p, div.compact p,
div.compact div, div.compact div {
  margin-top: 0.1em;
  margin-bottom: 0.1em;
}

tfoot {
  font-weight: bold;
}
td > div.verse {
  white-space: pre;
}

div.hdlist {
  margin-top: 0.8em;
  margin-bottom: 0.8em;
}
div.hdlist tr {
  padding-bottom: 15px;
}
dt.hdlist1.strong, td.hdlist1.strong {
  font-weight: bold;
}
td.hdlist1 {
  vertical-align: top;
  font-style: normal;
  padding-right: 0.8em;
  color: navy;
}
td.hdlist2 {
  vertical-align: top;
}
div.hdlist.compact tr {
  margin: 0;
  padding-bottom: 0;
}

.comment {
  background: yellow;
}

.footnote, .footnoteref {
  font-size: 0.8em;
}

span.footnote, span.footnoteref {
  vertical-align: super;
}

#footnotes {
  margin: 20px 0 20px 0;
  padding: 7px 0 0 0;
}

#footnotes div.footnote {
  margin: 0 0 5px 0;
}

#footnotes hr {
  border: none;
  border-top: 1px solid silver;
  height: 1px;
  text-align: left;
  margin-left: 0;
  width: 20%;
  min-width: 100px;
}

div.colist td {
  padding-right: 0.5em;
  padding-bottom: 0.3em;
  vertical-align: top;
}
div.colist td img {
  margin-top: 0.3em;
}

@media print {
  #footer-badges { display: none; }
}

#toc {
  margin-bottom: 2.5em;
}

#toctitle {
  color: #527bbd;
  font-size: 1.1em;
  font-weight: bold;
  margin-top: 1.0em;
  margin-bottom: 0.1em;
}

div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
  margin-top: 0;
  margin-bottom: 0;
}
div.toclevel2 {
  margin-left: 2em;
  font-size: 0.9em;
}
div.toclevel3 {
  margin-left: 4em;
  font-size: 0.9em;
}
div.toclevel4 {
  margin-left: 6em;
  font-size: 0.9em;
}

span.aqua { color: aqua; }
span.black { color: black; }
span.blue { color: blue; }
span.fuchsia { color: fuchsia; }
span.gray { color: gray; }
span.green { color: green; }
span.lime { color: lime; }
span.maroon { color: maroon; }
span.navy { color: navy; }
span.olive { color: olive; }
span.purple { color: purple; }
span.red { color: red; }
span.silver { color: silver; }
span.teal { color: teal; }
span.white { color: white; }
span.yellow { color: yellow; }

span.aqua-background { background: aqua; }
span.black-background { background: black; }
span.blue-background { background: blue; }
span.fuchsia-background { background: fuchsia; }
span.gray-background { background: gray; }
span.green-background { background: green; }
span.lime-background { background: lime; }
span.maroon-background { background: maroon; }
span.navy-background { background: navy; }
span.olive-background { background: olive; }
span.purple-background { background: purple; }
span.red-background { background: red; }
span.silver-background { background: silver; }
span.teal-background { background: teal; }
span.white-background { background: white; }
span.yellow-background { background: yellow; }

span.big { font-size: 2em; }
span.small { font-size: 0.6em; }

span.underline { text-decoration: underline; }
span.overline { text-decoration: overline; }
span.line-through { text-decoration: line-through; }

div.unbreakable { page-break-inside: avoid; }


/*
 * xhtml11 specific
 *
 * */

div.tableblock {
  margin-top: 1.0em;
  margin-bottom: 1.5em;
}
div.tableblock > table {
  border: 3px solid #527bbd;
}
thead, p.table.header {
  font-weight: bold;
  color: #527bbd;
}
p.table {
  margin-top: 0;
}
/* Because the table frame attribute is overridden by CSS in most browsers. */
div.tableblock > table[frame="void"] {
  border-style: none;
}
div.tableblock > table[frame="hsides"] {
  border-left-style: none;
  border-right-style: none;
}
div.tableblock > table[frame="vsides"] {
  border-top-style: none;
  border-bottom-style: none;
}


/*
 * html5 specific
 *
 * */

table.tableblock {
  margin-top: 1.0em;
  margin-bottom: 1.5em;
}
thead, p.tableblock.header {
  font-weight: bold;
  color: #527bbd;
}
p.tableblock {
  margin-top: 0;
}
table.tableblock {
  border-width: 3px;
  border-spacing: 0px;
  border-style: solid;
  border-color: #527bbd;
  border-collapse: collapse;
}
th.tableblock, td.tableblock {
  border-width: 1px;
  padding: 4px;
  border-style: solid;
  border-color: #527bbd;
}

table.tableblock.frame-topbot {
  border-left-style: hidden;
  border-right-style: hidden;
}
table.tableblock.frame-sides {
  border-top-style: hidden;
  border-bottom-style: hidden;
}
table.tableblock.frame-none {
  border-style: hidden;
}

th.tableblock.halign-left, td.tableblock.halign-left {
  text-align: left;
}
th.tableblock.halign-center, td.tableblock.halign-center {
  text-align: center;
}
th.tableblock.halign-right, td.tableblock.halign-right {
  text-align: right;
}

th.tableblock.valign-top, td.tableblock.valign-top {
  vertical-align: top;
}
th.tableblock.valign-middle, td.tableblock.valign-middle {
  vertical-align: middle;
}
th.tableblock.valign-bottom, td.tableblock.valign-bottom {
  vertical-align: bottom;
}


/*
 * manpage specific
 *
 * */

body.manpage h1 {
  padding-top: 0.5em;
  padding-bottom: 0.5em;
  border-top: 2px solid silver;
  border-bottom: 2px solid silver;
}
body.manpage h2 {
  border-style: none;
}
body.manpage div.sectionbody {
  margin-left: 3em;
}

@media print {
  body.manpage div#toc { display: none; }
}


</style>
<script type="text/javascript">
/*<![CDATA[*/
var asciidoc = {  // Namespace.

/////////////////////////////////////////////////////////////////////
// Table Of Contents generator
/////////////////////////////////////////////////////////////////////

/* Author: Mihai Bazon, September 2002
 * http://students.infoiasi.ro/~mishoo
 *
 * Table Of Content generator
 * Version: 0.4
 *
 * Feel free to use this script under the terms of the GNU General Public
 * License, as long as you do not remove or alter this notice.
 */

 /* modified by Troy D. Hanson, September 2006. License: GPL */
 /* modified by Stuart Rackham, 2006, 2009. License: GPL */

// toclevels = 1..4.
toc: function (toclevels) {

  function getText(el) {
    var text = "";
    for (var i = el.firstChild; i != null; i = i.nextSibling) {
      if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
        text += i.data;
      else if (i.firstChild != null)
        text += getText(i);
    }
    return text;
  }

  function TocEntry(el, text, toclevel) {
    this.element = el;
    this.text = text;
    this.toclevel = toclevel;
  }

  function tocEntries(el, toclevels) {
    var result = new Array;
    var re = new RegExp('[hH]([1-'+(toclevels+1)+'])');
    // Function that scans the DOM tree for header elements (the DOM2
    // nodeIterator API would be a better technique but not supported by all
    // browsers).
    var iterate = function (el) {
      for (var i = el.firstChild; i != null; i = i.nextSibling) {
        if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
          var mo = re.exec(i.tagName);
          if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
            result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
          }
          iterate(i);
        }
      }
    }
    iterate(el);
    return result;
  }

  var toc = document.getElementById("toc");
  if (!toc) {
    return;
  }

  // Delete existing TOC entries in case we're reloading the TOC.
  var tocEntriesToRemove = [];
  var i;
  for (i = 0; i < toc.childNodes.length; i++) {
    var entry = toc.childNodes[i];
    if (entry.nodeName.toLowerCase() == 'div'
     && entry.getAttribute("class")
     && entry.getAttribute("class").match(/^toclevel/))
      tocEntriesToRemove.push(entry);
  }
  for (i = 0; i < tocEntriesToRemove.length; i++) {
    toc.removeChild(tocEntriesToRemove[i]);
  }

  // Rebuild TOC entries.
  var entries = tocEntries(document.getElementById("content"), toclevels);
  for (var i = 0; i < entries.length; ++i) {
    var entry = entries[i];
    if (entry.element.id == "")
      entry.element.id = "_toc_" + i;
    var a = document.createElement("a");
    a.href = "#" + entry.element.id;
    a.appendChild(document.createTextNode(entry.text));
    var div = document.createElement("div");
    div.appendChild(a);
    div.className = "toclevel" + entry.toclevel;
    toc.appendChild(div);
  }
  if (entries.length == 0)
    toc.parentNode.removeChild(toc);
},


/////////////////////////////////////////////////////////////////////
// Footnotes generator
/////////////////////////////////////////////////////////////////////

/* Based on footnote generation code from:
 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
 */

footnotes: function () {
  // Delete existing footnote entries in case we're reloading the footnodes.
  var i;
  var noteholder = document.getElementById("footnotes");
  if (!noteholder) {
    return;
  }
  var entriesToRemove = [];
  for (i = 0; i < noteholder.childNodes.length; i++) {
    var entry = noteholder.childNodes[i];
    if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote")
      entriesToRemove.push(entry);
  }
  for (i = 0; i < entriesToRemove.length; i++) {
    noteholder.removeChild(entriesToRemove[i]);
  }

  // Rebuild footnote entries.
  var cont = document.getElementById("content");
  var spans = cont.getElementsByTagName("span");
  var refs = {};
  var n = 0;
  for (i=0; i<spans.length; i++) {
    if (spans[i].className == "footnote") {
      n++;
      var note = spans[i].getAttribute("data-note");
      if (!note) {
        // Use [\s\S] in place of . so multi-line matches work.
        // Because JavaScript has no s (dotall) regex flag.
        note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
        spans[i].innerHTML =
          "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
          "' title='View footnote' class='footnote'>" + n + "</a>]";
        spans[i].setAttribute("data-note", note);
      }
      noteholder.innerHTML +=
        "<div class='footnote' id='_footnote_" + n + "'>" +
        "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
        n + "</a>. " + note + "</div>";
      var id =spans[i].getAttribute("id");
      if (id != null) refs["#"+id] = n;
    }
  }
  if (n == 0)
    noteholder.parentNode.removeChild(noteholder);
  else {
    // Process footnoterefs.
    for (i=0; i<spans.length; i++) {
      if (spans[i].className == "footnoteref") {
        var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
        href = href.match(/#.*/)[0];  // Because IE return full URL.
        n = refs[href];
        spans[i].innerHTML =
          "[<a href='#_footnote_" + n +
          "' title='View footnote' class='footnote'>" + n + "</a>]";
      }
    }
  }
},

install: function(toclevels) {
  var timerId;

  function reinstall() {
    asciidoc.footnotes();
    if (toclevels) {
      asciidoc.toc(toclevels);
    }
  }

  function reinstallAndRemoveTimer() {
    clearInterval(timerId);
    reinstall();
  }

  timerId = setInterval(reinstall, 500);
  if (document.addEventListener)
    document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false);
  else
    window.onload = reinstallAndRemoveTimer;
}

}
asciidoc.install(4);
/*]]>*/
</script>
</head>
<body class="article">
<div id="header">
<h1>Tutorial for the Next Scripting Language</h1>
<span id="author">Gustaf Neumann &lt;neumann@wu-wien.ac.at&gt;, Stefan Sobernig &lt;stefan.sobernig@wu.ac.at&gt;</span><br />
<span id="revnumber">version 2.2.0,</span>
<span id="revdate">September 2018</span>
<br /><span id="revremark">Written for the Initial Release of the Next Scripting Framework.</span>
<div id="toc">
  <div id="toctitle">Table of Contents</div>
  <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="sidebarblock">
<div class="content">
<div class="title">Abstract</div>
<div class="paragraph"><p>This document provides a tutorial for the Next Scripting
Language NX.</p></div>
</div></div>
<div class="paragraph"><p>The Next Scripting Language (NX) is a highly flexible object oriented
scripting language based on Tcl <a href="#Ousterhout 1990">[Ousterhout 1990]</a>. NX is a successor
of XOTcl 1 <a href="#Neumann and Zdun 2000a">[Neumann and Zdun 2000a]</a> and was developed based on 10
years of experience with XOTcl in projects containing several hundred
thousand lines of code. While XOTcl was the first language designed to
provide <em>language support for design patterns</em>, the focus of the Next
Scripting Framework and NX is on combining this with <em>Language
Oriented Programming</em>. In many respects, NX was designed to ease the
learning of the language for novices (by using a more mainstream
terminology, higher orthogonality of the methods, less predefined
methods), to improve maintainability (remove sources of common errors)
and to encourage developers to write better structured programs (to
provide interfaces) especially for large projects, where many
developers are involved.</p></div>
<div class="paragraph"><p>The Next Scripting Language is based on the Next Scripting Framework
(NSF) which was developed based on the notion of language oriented
programming. The Next Scripting Frameworks provides C-level support
for defining and hosting multiple object systems in a single Tcl
interpreter.  The name of the Next Scripting Framework is derived from
the universal method combinator "next", which was introduced in XOTcl.
The combinator "next" serves as a single instrument for method
combination with filters, per-object and transitive per-class mixin
classes, object methods and multiple inheritance.</p></div>
<div class="paragraph"><p>The definition of NX is fully scripted (e.g.  defined in
<code>nx.tcl</code>). The Next Scripting Framework is shipped with three language
definitions, containing NX and XOTcl 2. Most of the existing XOTcl 1
programs can be used without modification in the Next Scripting
Framework by using XOTcl 2. The Next Scripting Framework requires Tcl
8.5 or newer.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_nx_and_its_roots">1. NX and its Roots</h2>
<div class="sectionbody">
<div class="paragraph"><p>Object oriented extensions of Tcl have quite a
long history. Two of the most prominent early Tcl based OO languages
were <em>incr Tcl</em> (abbreviated as itcl) and Object Tcl (<em>OTcl</em>
<a href="#Wetherall and Lindblad 1995">[Wetherall and Lindblad 1995]</a>). While itcl provides a traditional
C++/Java-like object system, OTcl was following the CLOS approach and
supports a dynamic object system, allowing incremental class and
object extensions and re-classing of objects.</p></div>
<div class="paragraph"><p>Extended Object Tcl (abbreviated as XOTcl <a href="#Neumann and Zdun 2000a">[Neumann and Zdun 2000a]</a>)
is a successor of OTcl and was the first language providing language
support for design patterns. XOTcl extends OTcl by providing namespace
support, adding assertions, dynamic object aggregations, slots and by
introducing per-object and per-class filters and per-object and
per-class mixins.</p></div>
<div class="paragraph"><p>XOTcl was so far released in more than 30 versions. It is described in
its detail in more than 20 papers and serves as a basis for other
object systems like TclOO [Donal ???]. The scripting language <em>NX</em> and
the <em>Next Scripting Framework</em> <a href="#Neumann and Sobernig 2009">[Neumann and Sobernig 2009]</a> extend
the basic ideas of XOTcl by providing support for <em>language-oriented
programming</em>. The the Next Scripting Framework supports multiple
object systems concurrently. Effectively, every object system has
different base classes for creating objects and classes. Therefore,
these object systems can have different interfaces and can
follow different naming conventions for built-in methods. Currently,
the Next Scripting Framework is packaged with three object systems:
NX, XOTcl 2.0, and TclCool (the language introduced by TIP#279).</p></div>
<div class="imageblock" style="text-align:center;">
<div class="content">
<img src="languages.png" alt="Languages" width="500" />
</div>
<div class="title">Figure 1. Language History of the Next Scripting Language</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>The primary purpose of this document is to introduce NX to beginners.
We expect some prior knowledge of programming languages, and some
knowledge about Tcl. In the following sections we introduce NX by
examples. In later sections we introduce the more advanced concepts of
the language. Conceptually, most of the addressed concepts are very
similar to XOTcl. Concerning the differences between NX and XOTcl,
please refer to the <em>Migration Guide for the Next Scripting Language</em>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_introductory_overview_example_stack">2. Introductory Overview Example: Stack</h2>
<div class="sectionbody">
<div class="paragraph"><p>A classical programming example is the implementation of a stack, which
is most likely familiar to many readers from many introductory
programming courses. A stack is a last-in first-out data structure
which is manipulated via operations like <code>push</code> (add something to the
stack) and <code>pop</code> remove an entry from the stack. These operations are
called <em>methods</em> in the context of object oriented programming
systems. Primary goals of object orientation are encapsulation and
abstraction. Therefore, we define a common unit (a class) that defines
and encapsulates the behavior of a stack and provides methods to a user
of the data structure that abstract from the actual implementation.</p></div>
<div class="sect2">
<h3 id="_define_a_class_stack">2.1. Define a Class "Stack"</h3>
<div class="paragraph"><p>In our first example, we define a class named <code>Stack</code> with the methods
<code>push</code> and <code>pop</code>. When an instance of the stack is created (e.g. a
concrete stack <code>s1</code>) the stack will contain an instance variable named
<code>things</code>, initialized with the an empty list.</p></div>
<div class="paragraph" id="xmp-class-stack"><div class="title">Listing 2: Class Stack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Stack {

   <span class='nx-comment'>#
</span>   <span class='nx-comment'># Stack of Things
</span>   <span class='nx-comment'>#
</span>
   <span class='nx-keyword'>:variable</span> things {}

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> push {thing} {
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>linsert</span> <span class='nx-variable'>${</span><span class='nx-variable'>:things}</span> 0 <span class='nx-variable'>$thing</span>]
      <span class='nx-keyword'>return</span> <span class='nx-variable'>$thing</span>
   }

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> pop {} {
      <span class='nx-keyword'>set</span> top [<span class='nx-keyword'>lindex</span> <span class='nx-variable'>${</span><span class='nx-variable'>:things}</span> 0]
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>lrange</span> <span class='nx-variable'>${</span><span class='nx-variable'>:things}</span> 1 end]
      <span class='nx-keyword'>return</span> <span class='nx-variable'>$top</span>
   }
}</pre></div></div>
<div class="paragraph"><p>Typically, classes are defined in NX via <code>nx::Class create</code>, followed
by the name of the new class (here: <code>Stack</code>). The definition of the
stack placed between curly braces and contains here just the method
definitions. Methods of the class are defined via <code>:method</code> followed
by the name of the method, an argument list and the body of the
method, consisting of Tcl and NX statements.</p></div>
<div class="paragraph"><p>When an instance of <code>Stack</code> is created, it will contain an instance
variable named <code>things</code>. If several <code>Stack</code> instances are created,
each of the instances will have their own (same-named but different)
instance variable. The instance variable <code>things</code> is used in our
example as a list for the internal representation of the stack. We
define in a next step the methods to access and modify this list
structure. A user of the stack using the provided methods does not
have to have any knowledge about the name or the structure of the
internal representation (the instance variable <code>things</code>).</p></div>
<div class="paragraph"><p>The method <code>push</code> receives an argument <code>thing</code> which should be placed
on the stack. Note that we do not have to specify the type of the
element on the stack, so we can push strings as well as numbers or
other kind of things. When an element is pushed, we add this element
as the first element to the list <code>things</code>. We insert the element using
the Tcl command <code>linsert</code> which receives the list as first element,
the position where the element should be added as second and the new
element as third argument. To access the value of the instance
variable we use Tcl&#8217;s dollar operator followed by the name. The
names of instance variables are preceded with a colon <code>:</code>. Since the
name contains a non-plain character, Tcl requires us to put braces
around the name. The command <code>linsert</code> and its arguments are placed
between square brackets. This means that the function <code>linsert</code> is called and
a new list is returned, where the new element is inserted at the first
position (index 0) in the list <code>things</code>. The result of the <code>linsert</code>
function is assigned again to the instance variable <code>things</code>, which is
updated this way.  Finally the method <code>push</code> returns the pushed thing
using the <code>return</code> statement.</p></div>
<div class="paragraph"><p>The method <code>pop</code> returns the most recently stacked element and removes
it from the stack. Therefore, it takes the first element from the list
(using the Tcl command <code>lindex</code>), assigns it to the method-scoped
variable <code>top</code>, removes the element from the instance variable
<code>things</code> (by using the Tcl command <code>lrange</code>) and returns the value
popped element <code>top</code>.</p></div>
<div class="paragraph"><p>This finishes our first implementation of the stack, more enhanced
versions will follow. Note that the methods <code>push</code> and <code>pop</code> are
defined as <code>public</code>; this means that these methods can be
used from all other objects in the system. Therefore, these methods
provide an interface to the stack implementation.</p></div>
<div class="paragraph" id="xmp-using-stack"><div class="title">Listing 3: Using the Stack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-comment'>#!/usr/bin/env tclsh
</span><span class='nx-keyword'>package</span> <span class='nx-keyword'>require</span> nx

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Stack {

   <span class='nx-comment'>#
</span>   <span class='nx-comment'># Stack of Things
</span>   <span class='nx-comment'>#
</span>   ....
}

Stack <span class='nx-keyword'>create</span> s1
s1 push a
s1 push b
s1 push c
<span class='nx-keyword'>puts</span> [s1 pop]
<span class='nx-keyword'>puts</span> [s1 pop]
s1 <span class='nx-keyword'>destroy</span></pre></div></div>
<div class="paragraph"><p>Now we want to use the stack. The code snippet in <a href="#xmp-using-stack">Listing 3</a> shows how to use the class Stack in a script.
Since NX is based on Tcl, the script will be called with the Tcl shell
<code>tclsh</code>. In the Tcl shell we have to <code>require package nx</code> to use the
Next Scripting Framework and NX. The next lines contain the definition
of the stack as presented before. Of course, it is as well possible to
make the definition of the stack an own package, such we could simple
say <code>package require stack</code>, or to save the definition of a stack
simply in a file and load it via <code>source</code>.</p></div>
<div class="paragraph"><p>In line 12 we create an instance of the stack, namely the stack object
<code>s1</code>. The object <code>s1</code> is an instance of <code>Stack</code> and has therefore
access to its methods. The methods like <code>push</code> or <code>pop</code> can be invoked
via a command starting with the object name followed by the
method name. In lines 13-15 we push on the stack the values <code>a</code>, then
<code>b</code>, and <code>c</code>. In line 16 we output the result of the <code>pop</code> method
using the Tcl command <code>puts</code>. We will see on standard output the
value+c+ (the last stacked item). The output of the line 17 is the
value <code>b</code> (the previously stacked item). Finally, in line 18 we
destroy the object. This is not necessary here, but shows the life
cycle of an object. In some respects, <code>destroy</code> is the counterpart of
<code>create</code> from line 12.</p></div>
<div class="imageblock" id="fig-class-object" style="text-align:center;">
<div class="content">
<img src="object-class-appclass.png" alt="object-class-appclass.png" />
</div>
<div class="title">Figure 4. Class and Object Diagram</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p><a href="#fig-class-object">Figure 4</a> shows the actual class and
object structure of the first <code>Stack</code> example. Note that the common
root class is <code>nx::Object</code> that contains methods for all objects.
Since classes are as well objects in NX, <code>nx::Class</code> is a
specialization of <code>nx::Object</code>. <code>nx::Class</code> provides methods for
creating objects, such as the method <code>create</code> which is used to create
objects (and classes as well).</p></div>
</div>
<div class="sect2">
<h3 id="_define_an_object_named_stack">2.2. Define an Object Named "stack"</h3>
<div class="paragraph"><p>The definition of the stack in <a href="#xmp-class-stack">Listing 2</a>
follows the traditional object oriented approach, found in
practically every object oriented programming language: Define a class
with some methods, create instances from this class, and use the
methods defined in the class in the instances of the class.</p></div>
<div class="paragraph"><p>In our next example, we introduce <em>generic objects</em> and <em>object
specific methods</em>. With NX, we can define generic objects, which are
instances of the most generic class <code>nx::Object</code> (sometimes called
<em>common root class</em>). <code>nx::Object</code> is predefined and contains a
minimal set of methods applicable to all NX objects.  In this example,
we define a generic object named <code>stack</code> and provide methods for this
object. The methods defined above were methods provided by a class for
objects. Now we define object specific methods, which are methods
applicable only to the object for which they are defined.</p></div>
<div class="paragraph" id="xmp-object-stack"><div class="title">Listing 5: Object stack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> stack {

   <span class='nx-keyword'>:object</span> <span class='nx-keyword'>variable</span> things {}

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> push {thing} {
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>linsert</span> <span class='nx-variable'>${</span><span class='nx-variable'>:things}</span> 0 <span class='nx-variable'>$thing</span>]
      <span class='nx-keyword'>return</span> <span class='nx-variable'>$thing</span>
   }

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> pop {} {
      <span class='nx-keyword'>set</span> top [<span class='nx-keyword'>lindex</span> <span class='nx-variable'>${</span><span class='nx-variable'>:things}</span> 0]
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>lrange</span> <span class='nx-variable'>${</span><span class='nx-variable'>:things}</span> 1 end]
      <span class='nx-keyword'>return</span> <span class='nx-variable'>$top</span>
   }
}</pre></div></div>
<div class="paragraph"><p>The example in <a href="#xmp-object-stack">Listing 5</a> defines the
object <code>stack</code> in a very similar way as the class <code>Stack</code>. But the
following points are different.</p></div>
<div class="ulist"><ul>
<li>
<p>
First, we use <code>nx::Object</code> instead of <code>nx::Class</code> to denote
  that we want to create a generic object, not a class.
</p>
</li>
<li>
<p>
We use <code>:object variable</code> to define the variable <code>things</code>  just for
   this single instance  (the object <code>stack</code>).
</p>
</li>
<li>
<p>
The definition for the methods <code>push</code> and <code>pop</code> are the same as
  before, but here we defined these with <code>object method</code>. Therefore,
  these two methods <code>push</code> and <code>pop</code> are object-specific.
</p>
</li>
</ul></div>
<div class="paragraph"><p>In order to use
the stack, we can use directly the object <code>stack</code> in the same way as
we have used the object <code>s1</code> in <a href="#xmp-using-stack">Listing 3</a>
the class diagram for this the object <code>stack</code>.</p></div>
<div class="imageblock" id="img-object-stack" style="text-align:center;">
<div class="content">
<img src="object-stack.png" alt="object-stack.png" />
</div>
<div class="title">Figure 6. Object stack</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>A reader might wonder when to use a class <code>Stack</code> or rather an object
<code>stack</code>. A big difference is certainly that one can define easily
multiple instances of a class, while the object is actually a
single, tailored entity. The concept of the object <code>stack</code> is similar to a module,
providing a certain functionality via a common interface, without
providing the functionality to create multiple instances. The reuse of
methods provided by the class to objects is as well a difference. If
the methods of the class are updated, all instances of the class will
immediately get the modified behavior. However, this does not mean that
the reuse for the methods of <code>stack</code> is not possible. NX allows for
example to copy objects (similar to prototype based languages) or to
reuse methods via e.g. aliases (more about this later).</p></div>
<div class="paragraph"><p>Note that we use capitalized names for classes and lowercase names for
instances. This is not required and a pure convention making it easier
to understand scripts without much analysis.</p></div>
</div>
<div class="sect2">
<h3 id="_implementing_features_using_mixin_classes">2.3. Implementing Features using Mixin Classes</h3>
<div class="paragraph"><p>So far, the definition of the stack methods was pretty minimal.
Suppose, we want to define "safe stacks" that protect e.g. against
stack under-runs (a stack under-run happens, when more <code>pop</code> than
<code>push</code> operations are issued on a stack). Safety checking can be
implemented mostly independent from the implementation details of the
stack (usage of internal data structures). There are as well different
ways of checking the safety. Therefore we say that safety checking is
orthogonal to the stack core implementation.</p></div>
<div class="paragraph"><p>With NX we can define stack-safety as a separate class using methods
with the same names as the implementations before, and "mix" this
behavior into classes or objects. The implementation of <code>Safety</code> in
stack under-runs and to issue error messages, when this happens.</p></div>
<div class="paragraph" id="xmp-class-safety"><div class="title">Listing 7: Class Safety</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Safety {

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Implement stack safety by defining an additional
</span>  <span class='nx-comment'># instance variable named "count" that keeps track of
</span>  <span class='nx-comment'># the number of stacked elements. The methods of
</span>  <span class='nx-comment'># this class have the same names and argument lists
</span>  <span class='nx-comment'># as the methods of Stack; these methods "shadow"
</span>  <span class='nx-comment'># the methods of class Stack.
</span>  <span class='nx-comment'>#
</span>
  <span class='nx-keyword'>:variable</span> count 0

  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> push {thing} {
    <span class='nx-keyword'>incr</span> :count
    <span class='nx-keyword'>next</span>
  }

  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> pop {} {
    <span class='nx-keyword'>if</span> {<span class='nx-variable'>${</span><span class='nx-variable'>:count}</span> == 0} { <span class='nx-keyword'>error</span> <span class='nx-string'>"Stack empty!"</span> }
    <span class='nx-keyword'>incr</span> :count -1
    <span class='nx-keyword'>next</span>
  }
}</pre></div></div>
<div class="paragraph"><p>Note that all the methods of the class <code>Safety</code> end with <code>next</code>.
This command is a primitive command of NX, which calls the
same-named method with the same argument list as the current
invocation.</p></div>
<div class="paragraph"><p>Assume we save the definition of the class <code>Stack</code> in a file named
<code>Stack.tcl</code> and the definition of the class <code>Safety</code> in a file named
<code>Safety.tcl</code> in the current directory. When we load the classes
<code>Stack</code> and <code>Safety</code> into the same script (see the terminal dialog in
e.g. a certain stack <code>s2</code> as a safe stack, while all other stacks
(such as <code>s1</code>) might be still "unsafe". This can be achieved via the
option <code>-mixin</code> at the object creation time (see line 9 in
option <code>-mixin</code> mixes the class <code>Safety</code> into the new instance <code>s2</code>.</p></div>
<div class="paragraph" id="xmp-using-class-safety"><div class="title">Listing 8: Using the Class Safety</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'>% <span class='nx-keyword'>package</span> <span class='nx-keyword'>require</span> nx
2.0
% <span class='nx-keyword'>source</span> Stack.tcl
::Stack
% <span class='nx-keyword'>source</span> Safety.tcl
::Safety
% Stack <span class='nx-keyword'>create</span> s1
::s1
% Stack <span class='nx-keyword'>create</span> s2 -object-mixin Safety
::s2
% s2 push a
% s2 pop
a
% s2 pop
Stack empty!

% s1 <span class='nx-keyword'>info</span> precedence
::Stack ::nx::Object

% s2 <span class='nx-keyword'>info</span> precedence
::Safety ::Stack ::nx::Object</pre></div></div>
<div class="paragraph"><p>When the method <code>push</code> of <code>s2</code> is called, first the method of the
mixin class <code>Safety</code> will be invoked that increments the counter and
continues with <code>next</code> to call the shadowed method, here the method
<code>push</code> of the <code>Stack</code> implementation that actually pushes the item.
The same happens, when <code>s2 pop</code> is invoked, first the method of
<code>Safety</code> is called, then the method of the <code>Stack</code>. When the stack is
empty (the value of <code>count</code> reaches 0), and <code>pop</code> is invoked, the
mixin class <code>Safety</code> generates an error message (raises an exception),
and does not invoke the method of the <code>Stack</code>.</p></div>
<div class="paragraph"><p>The last two commands in
<a href="#xmp-using-class-safety">Listing 8</a>
use introspection to query for the objects
<code>s1</code> and <code>s2</code> in which order the involved classes are processed. This
order is called the <code>precedence order</code> and is obtained via <code>info
precedence</code>. We see that the mixin class <code>Safety</code> is only in use for
<code>s2</code>, and takes there precedence over <code>Stack</code>. The common root class
<code>nx::Object</code> is for both <code>s1</code> and <code>s2</code> the base class.</p></div>
<div class="imageblock" id="img-per-object-mixin" style="text-align:center;">
<div class="content">
<img src="per-object-mixin.png" alt="per-object-mixin.png" />
</div>
<div class="title">Figure 9. Per-object Mixin</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>Note that in <a href="#xmp-using-class-safety">Listing 8</a>,
the class <code>Safety</code> is only mixed into a single object (here
<code>s2</code>), therefore we refer to this case as a <em>per-object mixin</em>.
<a href="#img-per-object-mixin">Figure 9</a> shows the class
diagram, where the class <code>Safety</code> is used as a per-object mixin for
<code>s2</code>.</p></div>
<div class="paragraph"><p>The mixin class <code>Safety</code> can be used as well in other ways, such as e.g. for
defining classes of safe stacks:</p></div>
<div class="paragraph" id="xmp-class-safestack"><div class="title">Listing 10: Class SafeStack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-comment'>#
</span><span class='nx-comment'># Create a safe stack class by using Stack and mixin
</span><span class='nx-comment'># Safety
</span><span class='nx-comment'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> SafeStack -superclasses Stack -mixins Safety

SafeStack <span class='nx-keyword'>create</span> s3</pre></div></div>
<div class="paragraph"><p>The difference of a per-class mixin and an per-object mixin is that
the per-class mixin is applicable to all instances of the
class. Therefore, we call these mixins also sometimes instance mixins.
In our example in <a href="#xmp-class-safestack">Listing 10</a>,
<code>Safety</code> is mixed into the definition of
<code>SafeStack</code>. Therefore, all instances of the class <code>SafeStack</code> (here
the instance <code>s3</code>) will be using the safety definitions.</p></div>
<div class="imageblock" id="img-per-class-mixin" style="text-align:center;">
<div class="content">
<img src="per-class-mixin.png" alt="per-class-mixin.png" />
</div>
<div class="title">Figure 11. Per-class Mixin</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p><a href="#img-per-class-mixin">Figure 11</a> shows the class diagram
for this definition.
Note that we could use <code>Safety</code> as well as a per-class mixin on
<code>Stack</code>. In this case, all stacks would be safe stacks and we could
not provide a selective feature selection (which might be perfectly
fine).</p></div>
</div>
<div class="sect2">
<h3 id="_define_different_kinds_of_stacks">2.4. Define Different Kinds of Stacks</h3>
<div class="paragraph"><p>The definition of <code>Stack</code> is generic and allows all kind of elements
to be stacked. Suppose, we want to use the generic stack definition,
but a certain stack (say, stack <code>s4</code>) should be a stack for integers
only. This behavior can be achieved by the same means as introduced
already in <a href="#xmp-object-stack">Listing 5</a>, namely
object-specific methods.</p></div>
<div class="paragraph" id="xmp-object-integer-stack"><div class="title">Listing 12: Object Integer Stack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'>Stack <span class='nx-keyword'>create</span> s4 {

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Create a stack with a object-specific method
</span>  <span class='nx-comment'># to check the type of entries
</span>  <span class='nx-comment'>#
</span>
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> push {thing:integer} {
    <span class='nx-keyword'>next</span>
  }
}</pre></div></div>
<div class="paragraph"><p>The program snippet in <a href="#xmp-object-integer-stack">Listing 12</a> defines an instance <code>s4</code> of the class
<code>Stack</code> and provides an object specific method for <code>push</code> to implement
an integer stack. The method <code>pull</code> is the same for the integer stack
as for all other stacks, so it will be reused as usual from the class
<code>Stack</code>. The object-specific method <code>push</code> of <code>s4</code> has a value
constraint in its argument list (<code>thing:integer</code>) that makes sure,
that only integers can be stacked. In case the argument is not an
integer, an exception will be raised. Of course, one could perform the
value constraint checking as well in the body of the method <code>proc</code> by
accepting an generic argument and by performing the test for the value
in the body of the method. In the case, the passed value is an
integer, the <code>push</code> method of <a href="#xmp-object-integer-stack">Listing 12</a> calls <code>next</code>, and therefore calls the
shadowed generic definition of <code>push</code> as provided by <code>Stack</code>.</p></div>
<div class="paragraph" id="xmp-class-integer-stack"><div class="title">Listing 13: Class IntegerStack</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> IntegerStack -superclass Stack {

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Create a Stack accepting only integers
</span>  <span class='nx-comment'>#
</span>
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> push {thing:integer} {
    <span class='nx-keyword'>next</span>
  }
}</pre></div></div>
<div class="paragraph"><p>An alternative approach is shown in
<a href="#xmp-class-integer-stack">Listing 13</a>, where the class
<code>IntegerStack</code> is defined, using the same method definition
as <code>s4</code>, this time on the class level.</p></div>
</div>
<div class="sect2">
<h3 id="_define_object_specific_methods_on_classes">2.5. Define Object Specific Methods on Classes</h3>
<div class="paragraph"><p>In our previous examples we defined methods provided by classes
(applicable for their instances) and object-specific methods (methods
defined on objects, which are only applicable for these objects). In
this section, we introduce methods that are defined on the class
objects. Such methods are sometimes called <em>class methods</em> or
<em>static methods</em>.</p></div>
<div class="paragraph"><p>In NX classes are objects, they are specialized objects with
additional methods. Methods for classes are often used for managing
the life-cycles of the instances of the classes (we will come to this
point in later sections in more detail). Since classes are objects, we
can use exactly the same notation as above to define class methods by
using <code>object method</code>. The methods defined on the class object are
in all respects identical with object specific methods shown in the
examples above.</p></div>
<div class="paragraph" id="xmp-stack2"><div class="title">Listing 14: Class Stack2</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Stack2 {

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> available_stacks {} {
      <span class='nx-keyword'>return</span> [<span class='nx-keyword'>llength</span> [<span class='nx-keyword'>:info</span> instances]]
   }

   <span class='nx-keyword'>:variable</span> things {}

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> push {thing} {
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>linsert</span> <span class='nx-variable'>${</span><span class='nx-variable'>:things}</span> 0 <span class='nx-variable'>$thing</span>]
      <span class='nx-keyword'>return</span> <span class='nx-variable'>$thing</span>
   }

   <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> pop {} {
      <span class='nx-keyword'>set</span> top [<span class='nx-keyword'>lindex</span> <span class='nx-variable'>${</span><span class='nx-variable'>:things}</span> 0]
      <span class='nx-keyword'>set</span> :things [<span class='nx-keyword'>lrange</span> <span class='nx-variable'>${</span><span class='nx-variable'>:things}</span> 1 end]
      <span class='nx-keyword'>return</span> <span class='nx-variable'>$top</span>
   }
}

Stack2 <span class='nx-keyword'>create</span> s1
Stack2 <span class='nx-keyword'>create</span> s2

<span class='nx-keyword'>puts</span> [Stack2 available_stacks]</pre></div></div>
<div class="paragraph"><p>The class <code>Stack2</code> in <a href="#xmp-stack2">Listing 14</a> consists of the
earlier definition of the class <code>Stack</code> and is extended by the
class-specific method <code>available_stacks</code>, which returns the
current number of instances of the stack. The final command <code>puts</code>
(line 26) prints 2 to the console.</p></div>
<div class="imageblock" id="img-stack2" style="text-align:center;">
<div class="content">
<img src="stack2.png" alt="stack2.png" />
</div>
<div class="title">Figure 15. Stack2</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>The class diagram in <a href="#img-stack2">Figure 15</a> shows the
diagrammatic representation of the class object-specific method
<code>available_stacks</code>.  Since every class is a specialization of the
common root class <code>nx::Object</code>, the common root class is often omitted
from the class diagrams, so it was omitted here as well in the diagram.</p></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_basic_language_features_of_nx">3. Basic Language Features of NX</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_variables_and_properties">3.1. Variables and Properties</h3>
<div class="paragraph"><p>In general, NX does not need variable declarations. It allows one to
create or modify variables on the fly by using for example the Tcl
commands <code>set</code> and <code>unset</code>. Depending on the variable name (or more
precisely, depending on the variable name&#8217;s prefix consisting of
colons "<code>:</code>") a variable is either local to a method, or it is an
instance variable, or a global variable.  The rules are:</p></div>
<div class="ulist"><ul>
<li>
<p>
A variable without any colon prefix refers typically to a method
   scoped variable. Such a variable is created during the invocation
   of the method, and it is deleted, when the method ends.  In the
   example below, the variable <code>a</code> is method scoped.
</p>
</li>
<li>
<p>
A variable with a single colon prefix refers to an instance
    variable.  An instance variable is part of the object; when the
    object is destroyed, its instance variables are deleted as well. In the
    example below, the variable <code>b</code> is an instance variable.
</p>
</li>
<li>
<p>
A variable with two leading colons refers to a global variable. The
  lifespan of a globale variable ends when the variable is explicitly
  unset or the script terminates. Variables, which are placed in Tcl
  namespaces, are also global variables. In the example below, the
  variable <code>c</code> is a global variable.
</p>
</li>
</ul></div>
<div class="paragraph" id="xmp-var-resolver"><div class="title">Listing 16: Scopes of Variables</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Foo {

  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo args {...}
    <span class='nx-comment'># "a" is a method scoped variable
</span>    <span class='nx-keyword'>set</span> a 1
    <span class='nx-comment'># "b" is an Instance variable
</span>    <span class='nx-keyword'>set</span> :b 2
    <span class='nx-comment'># "c" is a global variable/namespaced variable
</span>    <span class='nx-keyword'>set</span> ::c 3
  }
}</pre></div></div>
<div class="paragraph"><p><a href="#xmp-var-resolver">Listing 16</a> shows a method <code>foo</code>
of some class <code>Foo</code> referring to differently scoped variables.</p></div>
<div class="sect3">
<h4 id="_properties_configurable_instance_variables">3.1.1. Properties: Configurable Instance Variables</h4>
<div class="paragraph"><p>As described above, there is no need to declare instance variables in
NX. In many cases, a developer might want to define some value
constraints for variables, or to provide defaults, or to make
variables configurable upon object creation. Often, variables are
"inherited", meaning that the variables declared in a general class
are also available in a more specialized class. For these purposes NX
provides <em>variable handlers</em> responsible for the management of
instance variables. We distinguish in NX between configurable
variables (called <code>property</code>) and variables that are not configurable
(called <code>variable</code>).</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>A <strong>property</strong> is a definition of a configurable instance variable.</p></div>
</div></div>
<div class="paragraph"><p>The term configurable means that (a) one can provide at creation time of
an instance a value for this variable, and (b), one can query the
value via the accessor function <code>cget</code> and (c), one can change the
value of the variable via <code>configure</code> at runtime. Since the general
accessor function <code>cget</code> and <code>configure</code> are available, an application
developer does not have to program own accessor methods. When value
checkers are provided, each time, the value of the variable is to be
changed, the constrained are checked as well.</p></div>
<div class="imageblock" id="img-person-student" style="text-align:center;">
<div class="content">
<img src="person-student.png" alt="person-student.png" />
</div>
<div class="title">Figure 17. Classes Person and Student</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>The class diagram above defines the classes <code>Person</code> and
<code>Student</code>. For both classes, configurable instance variable are
specified by defining these as properties. The listing below shows
an implementation of this conceptual model in NX.</p></div>
<div class="paragraph" id="xmp-properties"><div class="title">Listing 18: Properties</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-comment'>#
</span><span class='nx-comment'># Define a class Person with properties "name"
</span><span class='nx-comment'># and "birthday"
</span><span class='nx-comment'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Person {
  <span class='nx-keyword'>:property</span> name:required
  <span class='nx-keyword'>:property</span> birthday
}

<span class='nx-comment'>#
</span><span class='nx-comment'># Define a class Student as specialization of Person
</span><span class='nx-comment'># with additional properties
</span><span class='nx-comment'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Student -superclass Person {
  <span class='nx-keyword'>:property</span> matnr:required
  <span class='nx-keyword'>:property</span> {oncampus:boolean true}
}

<span class='nx-comment'>#
</span><span class='nx-comment'># Create instances using configure parameters
</span><span class='nx-comment'># for the initialization
</span><span class='nx-comment'>#
</span>Person <span class='nx-keyword'>create</span> p1 -name Bob
Student <span class='nx-keyword'>create</span> s1 -name Susan -matnr 4711

<span class='nx-comment'># Access property value via accessor method
</span><span class='nx-keyword'>puts</span> <span class='nx-string'>"The name of s1 is [s1 cget -name]"</span></pre></div></div>
<div class="paragraph"><p>By defining <code>name</code> and <code>birthday</code> as properties of <code>Person</code>, NX makes
these configurable.  When we create an instance of <code>Person</code> named
<code>p1</code>, we can provide a value for e.g. the name by specifying <code>-name</code>
during creation. The properties result in non-positional configure parameters
which can be provided in any order. In our listing, we create an instance of
<code>Person</code> using the configure  parameter <code>name</code> and provide the value of
<code>Bob</code> to the instance variable <code>name</code>.</p></div>
<div class="paragraph"><p>The class <code>Student</code> is defined as a specialization of <code>Person</code> with
two additional properties: <code>matnr</code> and <code>oncampus</code>. The property
<code>matnr</code> is required (it has to be provided, when an instance of this
class is created), and the property <code>oncampus</code> is boolean, and is per
default set to <code>true</code>. Note that the class <code>Student</code> inherits the
properties of <code>Person</code>. So, <code>Student</code> has four properties in total.</p></div>
<div class="paragraph"><p>The property definitions provide the <code>configure parameters</code> for
instance creation. Many other languages require such parameters to be
passed via arguments of a constructor, which is often error prone,
when values are to be passed to superclasses. Also in dynamic
languages, the relationships between classes can be easily changed,
and different superclasses might have different requirements in their
constructors. The declarative approach in NX reduces the need for
tailored constructor methods significantly.</p></div>
<div class="paragraph"><p>Note, that the property <code>matnr</code> of class <code>Student</code> is required. This
means, that if we try to create an instance of <code>Student</code>, a runtime
exception will be triggered. The property <code>oncamups</code> is boolean and
contains a default value. Providing a default value means that
whenever we create an instance of this class the object will contain
such an instance variable, even when we provide no value via the
configure parameters.</p></div>
<div class="paragraph"><p>In our listing, we create an instance of <code>Student</code> using the two
configure parameters <code>name</code> and <code>matnr</code>. Finally, we use method <code>cget</code>
to obtain the value of the instance variable <code>name</code> of object <code>s1</code>.</p></div>
</div>
<div class="sect3">
<h4 id="_non_configurable_instance_variables">3.1.2. Non-configurable Instance Variables</h4>
<div class="paragraph"><p>In practice, not all instance variables should be configurable. But
still, we want to be able to provide defaults similar to
properties. To define non-configurable instance variables the
predefined method <code>variable</code> can be used. Such instance variables are
often used for e.g. keeping the internal state of an object.  The
usage of <code>variable</code> is in many respects similar to <code>property</code>. One
difference is, that <code>property</code> uses the same syntax as for method
parameters, whereas <code>variable</code> receives the default value as a
separate argument (similar to the <code>variable</code> command in plain
Tcl). The introductory Stack example in <a href="#xmp-class-stack">Listing 2</a> uses already the method <code>variable</code>.</p></div>
<div class="paragraph" id="xmp-variable"><div class="title">Listing 19: Declaring Variables</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Base {
  <span class='nx-keyword'>:variable</span> x 1
  <span class='nx-comment'># ...
</span>}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Derived -superclass Base {
  <span class='nx-keyword'>:variable</span> y 2
  <span class='nx-comment'># ...
</span>}

<span class='nx-comment'># Create instance of the class Derived
</span>Derived <span class='nx-keyword'>create</span> d1

<span class='nx-comment'># Object d1 has instance variables
</span><span class='nx-comment'># x == 1 and y == 2</span></pre></div></div>
<div class="paragraph"><p>Note that the variable definitions are inherited in the same way as
properties. The example in <a href="#xmp-variable">Listing 19</a> shows a
class <code>Derived</code> that inherits from <code>Base</code>. When an instance <code>d1</code> is
created, it will contain the two instance variables <code>x</code> and <code>y</code>.
Note that the variable declarations from <code>property</code> and <code>variable</code> are
used to initialize (and to configure) the instances variables of an object.</p></div>
<div class="paragraph" id="xmp-constructor"><div class="title">Listing 20: Setting Variables in the Constructor</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Base2 {
 <span class='nx-comment'># ...
</span> <span class='nx-keyword'>:method</span> <span class='nx-keyword'>init</span> {} {
   <span class='nx-keyword'>set</span> :x 1
   <span class='nx-comment'># ....
</span> }
}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Derived2 -superclass Base2 {
 <span class='nx-comment'># ...
</span> <span class='nx-keyword'>:method</span> <span class='nx-keyword'>init</span> {} {
   <span class='nx-keyword'>set</span> :y 2
   <span class='nx-keyword'>next</span>
   <span class='nx-comment'># ....
</span> }
}

<span class='nx-comment'># Create instance of the class Derived2
</span>Derived2 <span class='nx-keyword'>create</span> d2</pre></div></div>
<div class="paragraph"><p>In many other object oriented languages, the instance variables are
initialized solely by the constructor (similar to class <code>Derived2</code> in
<a href="#xmp-constructor">Listing 20</a>). This approach is certainly
also possible in NX. Note that the approach using constructors
requires an explicit method chaining between the constructors and is
less declarative than the approach in NX using <code>property</code> and <code>variable</code>.</p></div>
<div class="paragraph"><p>Both, <code>property</code> and <code>variable</code> provide much more functionalities. One
can for example declare <code>public</code>, <code>protected</code> or <code>private</code> accessor
methods, or one can define variables to be incremental (for
e.g. adding values to a list of values), or one can define variables
specific behavior.</p></div>
</div>
</div>
<div class="sect2">
<h3 id="_method_definitions">3.2. Method Definitions</h3>
<div class="paragraph"><p>The basic building blocks of an object oriented program are object and
classes, which contain named pieces of code, the methods.</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p><strong>Methods</strong> are subroutines (pieces of code) associated with objects
and/or classes. A method has a name, receives optionally arguments
during invocation and returns a value.</p></div>
</div></div>
<div class="paragraph"><p>Plain Tcl provides subroutines, which are not associated with objects
or classes. Tcl distinguishes between +proc+s (scripted subroutines)
and  commands (system-languages implemented subroutines).</p></div>
<div class="paragraph"><p>Methods might have different scopes, defining, on which kind of
objects these methods are applicable to. These are described in more
detail later on. For the time being, we deal here with methods defined
on classes, which are applicable for the instance of these classes.</p></div>
<div class="sect3">
<h4 id="_scripted_methods">3.2.1. Scripted Methods</h4>
<div class="paragraph"><p>Since NX is a scripting language, most methods are most likely
scripted methods, in which the method body contains Tcl code.</p></div>
<div class="paragraph" id="xmp-fido1"><div class="title">Listing 21: Scripted method</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-comment'># Define a class
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Dog {

  <span class='nx-comment'># Define a scripted method for the class
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bark {} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"[self] Bark, bark, bark."</span>
  }
}

<span class='nx-comment'># Create an instance of the class
</span>Dog <span class='nx-keyword'>create</span> fido

<span class='nx-comment'># The following line prints "::fido Bark, bark, bark."
</span>fido bark</pre></div></div>
<div class="paragraph"><p>In the example above we create a class <code>Dog</code> with a scripted method
named <code>bark</code>. The method body defines the code, which is executed when
the method is invoked. In this example, the method <code>bar</code> prints out a
line on the terminal starting with the object name (this is determined
by the built in command <code>self</code>) followed by "Bark, bark, bark.".  This
method is defined on a class and applicable to instances of the class
(here the instance <code>fido</code>).</p></div>
</div>
<div class="sect3">
<h4 id="_c_implemented_methods">3.2.2. C-implemented Methods</h4>
<div class="paragraph"><p>Not all of the methods usable in NX are scripted methods; many
predefined methods are defined in the underlying system language,
which is typically C.  For example, in <a href="#xmp-fido1">Listing 21</a> we
used the method <code>create</code> to create the class <code>Dog</code> and to create the
dog instance <code>fido</code>. These methods are implemented in C in the next
scripting framework.</p></div>
<div class="paragraph"><p>C-implemented methods are not only provided by the underlying
framework but might be as well defined by application developers. This
is an advanced topic, not covered here. However, application developer
might reuse some generic C code to define their own C-implemented
methods. Such methods are for example <em>accessors</em>, <em>forwarders</em> and
<em>aliases</em>.</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>An <strong>accessor method</strong> is a method that accesses instance
variables of an object. A call to an accessor
without arguments uses the accessor as a getter, obtaining the actual
value of the associated variable. A call to an accessor with an
argument uses it as a setter, setting the value of the associated
variable.</p></div>
</div></div>
<div class="paragraph"><p>NX provides support for C-implemented accessor methods. Accessors have
already been mentioned in the section about properties. When
the option <code>-accessor public|protected|private</code> is provided to a
<code>variable</code> or <code>property</code> definition, NX creates automatically a
same-named accessors method.</p></div>
<div class="paragraph" id="xmp-fido2"><div class="title">Listing 22: Accessor Methods</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Dog {
 <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bark {} { <span class='nx-keyword'>puts</span> <span class='nx-string'>"[self] Bark, bark, bark."</span> }
 <span class='nx-keyword'>:method</span> <span class='nx-keyword'>init</span> {} { Tail <span class='nx-keyword'>create</span> [<span class='nx-keyword'>self</span>]::tail}
}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Tail {
  <span class='nx-keyword'>:property</span> -accessor <span class='nx-keyword'>public</span> {length:double 5}
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> wag {} {<span class='nx-keyword'>return</span> Joy}
}

<span class='nx-comment'># Create an instance of the class
</span>Dog <span class='nx-keyword'>create</span> fido

<span class='nx-comment'># Use the accessor "length" as a getter, to obtain the value
</span><span class='nx-comment'># of a property. The following call returns the length of the
</span><span class='nx-comment'># tail of fido
</span>fido::tail length get

<span class='nx-comment'># Use the accessor "length" as a setter, to alter the value
</span><span class='nx-comment'># of a property. The following call changes the length of
</span><span class='nx-comment'># the tail of fido
</span>fido::tail length <span class='nx-keyword'>set</span> 10

<span class='nx-comment'># Proving an invalid values will raise an error
</span>fido::tail length <span class='nx-keyword'>set</span> <span class='nx-string'>"Hello"</span></pre></div></div>
<div class="paragraph"><p><a href="#xmp-fido2">Listing 22</a> shows an extended example, where every dog
has a tail. The object <code>tail</code> is created as a subobject of the dog in
the constructor <code>init</code>. The subobject can be accessed by providing the
full name of the subobject <code>fido::tail</code>. The method <code>length</code> is an
C-implemented accessor, that enforces the value constraint (here a
floating point number, since length uses the value constraint
<code>double</code>). Line 25 will therefore raise an exception, since the
provided values cannot be converted to a double number.</p></div>
<div class="paragraph" id="xmp-fido3"><div class="title">Listing 23: Forwarder Methods</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Dog {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bark {} { <span class='nx-keyword'>puts</span> <span class='nx-string'>"[self] Bark, bark, bark."</span> }
  <span class='nx-keyword'>:method</span> <span class='nx-keyword'>init</span> {} {
    Tail <span class='nx-keyword'>create</span> [<span class='nx-keyword'>self</span>]::tail
    <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>forward</span> wag [<span class='nx-keyword'>self</span>]::tail wag
  }
}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Tail {
  <span class='nx-keyword'>:property</span> {length 5}
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> wag {} {<span class='nx-keyword'>return</span> Joy}
}

<span class='nx-comment'># Create an instance of the class
</span>Dog <span class='nx-keyword'>create</span> fido

<span class='nx-comment'># The invocation of "fido wag" is delegated to "fido::tail wag".
</span><span class='nx-comment'># Therefore, the following method returns "Joy".
</span>fido wag</pre></div></div>
<div class="paragraph"><p><a href="#xmp-fido3">Listing 23</a> again extends the example by adding a
forwarder named <code>wag</code> to the object (e.g. <code>fido</code>). The forwarder
redirects all calls of the form <code>fido wag</code> with arbitrary arguments to
the subobject <code>fido::tail</code>.</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>A <strong>forwarder method</strong> is a
C-implemented method that redirects an invocation for a certain method
to either a method of another object or to some other method of the
same object. Forwarding an invocation of a method to some other
object is a means of delegation.</p></div>
</div></div>
<div class="paragraph"><p>The functionality of the forwarder can just as well be implemented as
a scripted method, but for the most common cases, the forward
implementation is more efficient, and the <code>forward</code> method expresses
the intention of the developer.</p></div>
<div class="paragraph"><p>The method <code>forwarder</code> has several options to change e.g. the order of
the arguments, or to substitute certain patterns in the argument list
etc. This will be described in later sections.</p></div>
</div>
<div class="sect3">
<h4 id="_method_aliases">3.2.3. Method-Aliases</h4>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>An <strong>alias method</strong> is a means to register either an existing method,
or a Tcl proc, or a Tcl command as a method with the provided
name on a class or object.</p></div>
</div></div>
<div class="paragraph"><p>In some way, the method alias is a restricted form of a forwarder,
though it does not support delegation to different objects or argument
reordering. The advantage of the method alias compared to a forwarder
is that it has close to zero overhead, especially for aliasing
c-implemented methods.</p></div>
<div class="paragraph" id="xmp-fido4"><div class="title">Listing 24: Method-Alias</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Dog {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bark {} { <span class='nx-keyword'>puts</span> <span class='nx-string'>"[self] Bark, bark, bark."</span> }

  <span class='nx-comment'># Define a public alias for the method "bark"
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>alias</span> warn [<span class='nx-keyword'>:info</span> <span class='nx-keyword'>method</span> handle bark]
  <span class='nx-comment'># ...
</span>}

<span class='nx-comment'># Create an instance of the class
</span>Dog <span class='nx-keyword'>create</span> fido

<span class='nx-comment'># The following line prints "::fido Bark, bark, bark."
</span>fido warn</pre></div></div>
<div class="paragraph"><p><a href="#xmp-fido4">Listing 24</a> extends the last example by defining an
alias for the method <code>bark</code>. The example only shows the bare
mechanism. In general, method aliases are very powerful means for
reusing pre-existing functionality. The full object system of NX and
XOTcl2 is built from aliases, reusing functionality provided by the
next scripting framework under different names. Method aliases
are as well a means for implementing traits in NX.</p></div>
</div>
</div>
<div class="sect2">
<h3 id="_method_protection">3.3. Method Protection</h3>
<div class="paragraph"><p>All kinds of methods might have different kind of protections in NX.
The call-protection defines from which calling context methods might
be called. The Next Scripting Framework supports as well redefinition
protection for methods.</p></div>
<div class="paragraph"><p>NX distinguishes between <code>public</code>, <code>protected</code> and <code>private</code> methods,
where the default call-protection is <code>protected</code>.</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>A <strong>public</strong> method can be called from every context. A <strong>protected</strong>
method can only be invoked from the same object. A <strong>private</strong> method
can only be invoked from methods defined on the same entity
(defined on the same class or on the same object) via the invocation
with the local flag (i.e. "<code>: -local foo</code>").</p></div>
</div></div>
<div class="paragraph"><p>All kind of method protections are applicable for all kind of methods,
either scripted or C-implemented.</p></div>
<div class="paragraph"><p>The distinction between public and protected leads to interfaces for
classes and objects. Public methods are intended for consumers of
these entities. Public methods define the intended ways of providing
methods for external usages (usages, from other objects or
classes). Protected methods are intended for the implementor of the
class or subclasses and not for public usage. The distinction between
protected and public reduces the coupling between consumers and the
implementation, and offers more flexibility to the developer.</p></div>
<div class="paragraph" id="xmp-protected-method"><div class="title">Listing 25: Protected Methods</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Foo {

  <span class='nx-comment'># Define a public method
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {
    <span class='nx-comment'># ....
</span>    <span class='nx-keyword'>return</span> [:helper]
  }

  <span class='nx-comment'># Define a protected method
</span>  <span class='nx-keyword'>:method</span> helper {} {
     <span class='nx-keyword'>return</span> 1
  }
}

<span class='nx-comment'># Create an instance of the class:
</span>Foo <span class='nx-keyword'>create</span> f1

<span class='nx-comment'># The invocation of the public method "foo" returns 1
</span>f1 foo

<span class='nx-comment'># The invocation of the protected method "helper" raises an error:
</span>f1 helper</pre></div></div>
<div class="paragraph"><p>The example above uses <code>:protected method helper &#8230;</code>. We could have
used here as well <code>:method helper &#8230;</code>, since the default method
call-protection is already protected.</p></div>
<div class="paragraph"><p>The method call-protection of <code>private</code> goes one step further and
helps to hide implementation details also for implementors of
subclasses. Private methods are a means for avoiding unanticipated name
clashes. Consider the following example:</p></div>
<div class="paragraph" id="xmp-private-method"><div class="title">Listing 26: Private Methods</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Base {
  <span class='nx-keyword'>:private</span> <span class='nx-keyword'>method</span> helper {a b} {<span class='nx-keyword'>expr</span> {<span class='nx-variable'>$a</span> + <span class='nx-variable'>$b</span>}}
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo     {a b} {: -local helper <span class='nx-variable'>$a</span> <span class='nx-variable'>$b</span>}
}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Sub -superclass Base {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bar     {a b} {: -local helper <span class='nx-variable'>$a</span> <span class='nx-variable'>$b</span>}
  <span class='nx-keyword'>:private</span> <span class='nx-keyword'>method</span> helper {a b} {<span class='nx-keyword'>expr</span> {<span class='nx-variable'>$a</span> * <span class='nx-variable'>$b</span>}}
  <span class='nx-keyword'>:create</span> s1
}

s1 foo 3 4     ;<span class='nx-comment'># returns 7
</span>s1 bar 3 4     ;<span class='nx-comment'># returns 12
</span>s1 helper 3 4  ;<span class='nx-comment'># raises error: unable to dispatch method helper</span></pre></div></div>
<div class="paragraph"><p>The base class implements a public method <code>foo</code> using the helper
method named <code>helper</code>. The derived class implements a as well a public
method <code>bar</code>, which is also using a helper method named <code>helper</code>. When
an instance <code>s1</code> is created from the derived class, the method <code>foo</code>
is invoked which uses in turn the private method of the base
class. Therefore, the invocation <code>s1 foo 3 4</code> returns its sum. If
the <code>local</code> flag had not beed used in helper, <code>s1</code> would
have tried to call the helper of <code>Sub</code>, which would be incorrect. For
all other purposes, the private methods are "invisible" in all
situations, e.g., when mixins are used, or within the <code>next</code>-path, etc.</p></div>
<div class="paragraph"><p>By using the <code>-local</code> flag at the call site it is possible to invoke
only the local definition of the method. If we would call the method
without this flag, the resolution order would be the standard
resolution order, starting with filters, mixins, object methods
and the full intrinsic class hierarchy.</p></div>
<div class="paragraph"><p>NX supports the modifier <code>private</code> for methods and properties. In all
cases <code>private</code> is an instrument to avoid unanticipated interactions
and means actually "accessible for methods defined on the same entity
(object or class)". The main usage for <code>private</code> is to improve
locality of the code e.g. for compositional operations.</p></div>
<div class="paragraph"><p>In order to improve locality for properties, a private property
defines therefore internally a variable with a different name to avoid
unintended interactions. The variable should be accessed via the
private accessor, which can be invoked with the <code>-local</code> flag.  In the
following example class <code>D</code> introduces a private property with the
same name as a property in the superclass.</p></div>
<div class="paragraph" id="xmp-private-properties"><div class="title">Listing 27: Private Properties</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-comment'>#
</span><span class='nx-comment'># Define a class C with a property "x" and a public accessor
</span><span class='nx-comment'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {
  <span class='nx-keyword'>:property</span> -accessor <span class='nx-keyword'>public</span> {x c}
}

<span class='nx-comment'>#
</span><span class='nx-comment'># Define a subclass D with a private property "x"
</span><span class='nx-comment'># and a method bar, which is capable of accessing
</span><span class='nx-comment'># the private property.
</span><span class='nx-comment'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> D -superclass C {
  <span class='nx-keyword'>:property</span> -accessor <span class='nx-keyword'>private</span> {x d}
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bar {p} {<span class='nx-keyword'>return</span> [: -local <span class='nx-variable'>$p</span> get]}
}

<span class='nx-comment'>#
</span><span class='nx-comment'># The private and public (or protected) properties
</span><span class='nx-comment'># define internally separate variable that do not
</span><span class='nx-comment'># conflict.
</span><span class='nx-comment'>#
</span>D <span class='nx-keyword'>create</span> d1
<span class='nx-keyword'>puts</span> [d1 x get]   ;<span class='nx-comment'># prints "c"
</span><span class='nx-keyword'>puts</span> [d1 bar x]   ;<span class='nx-comment'># prints "d"</span></pre></div></div>
<div class="paragraph"><p>Without the <code>private</code> definition of the property, the definition of
property <code>x</code> in class <code>D</code> would shadow the
definition of the property in the superclass <code>C</code> for its instances
(<code>d1 x</code> or <code>set :x</code> would return <code>d</code> instead of <code>c</code>).</p></div>
</div>
<div class="sect2">
<h3 id="_applicability_of_methods">3.4. Applicability of Methods</h3>
<div class="paragraph"><p>As defined above, a method is a subroutine defined on an object or
class. This object (or class) contains the method. If the object (or
class) is deleted, the contained methods will be deleted as well.</p></div>
<div class="sect3">
<h4 id="_instance_methods">3.4.1. Instance Methods</h4>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>Typically, methods are defined on a class, and the methods defined on the
class are applicable to the instances (direct or indirect) of this
class. These methods are called <strong>instance methods</strong>.</p></div>
</div></div>
<div class="paragraph"><p>In the following example method, <code>foo</code> is an instance method defined
on class <code>C</code>.</p></div>
<div class="paragraph" id="xmp-instance-applicable"><div class="title">Listing 28: Methods applicable for instances</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {<span class='nx-keyword'>return</span> 1}
  <span class='nx-keyword'>:create</span> c1
}

<span class='nx-comment'># Method "foo" is defined on class "C"
</span><span class='nx-comment'># and applicable to the instances of "C"
</span>c1 foo</pre></div></div>
<div class="paragraph"><p>There are many programming languages that only allow these types of methods.
However, NX also allows methods to be defined on objects.</p></div>
</div>
<div class="sect3">
<h4 id="_object_methods">3.4.2. Object Methods</h4>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>Methods defined on objects are <strong>object methods</strong>. Object
methods are only applicable on the object, on which they are defined.
Object methods cannot be inherited from other objects.</p></div>
</div></div>
<div class="paragraph"><p>The following example defines an object method <code>bar</code> on the
instance <code>c1</code> of class <code>C</code>, and as well as the object specific method
<code>baz</code> defined on the object <code>o1</code>. An object method is defined
via <code>object method</code>.</p></div>
<div class="paragraph"><p>Note that we can define a object method that shadows (redefines)
for this object methods provided from classes.</p></div>
<div class="paragraph" id="xmp-object-applicable1"><div class="title">Listing 29: Object Method</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {<span class='nx-keyword'>return</span> 1}
  <span class='nx-keyword'>:create</span> c1 {
     <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {} {<span class='nx-keyword'>return</span> 2}
     <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {} {<span class='nx-keyword'>return</span> 3}
  }
}

<span class='nx-comment'># Method "bar" is an object specific method of "c1"
</span>c1 bar

<span class='nx-comment'># object-specific method "foo" returns 2
</span>c1 foo

<span class='nx-comment'># Method "baz" is an object specific method of "o1"
</span><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o1 {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> baz {} {<span class='nx-keyword'>return</span> 4}
}
o1 baz</pre></div></div>
</div>
<div class="sect3">
<h4 id="_class_methods">3.4.3. Class Methods</h4>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>A <strong>class method</strong> is a method defined on a class, which is only
applicable to the class object itself. The class method is actually
an object method of the class object.</p></div>
</div></div>
<div class="paragraph"><p>In NX, all classes are objects. Classes are in NX special kind of
objects that have e.g. the ability to create instances and to provide
methods for the instances. Classes manage their instances. The general
method set for classes is defined on the meta-classes (more about
this later).</p></div>
<div class="paragraph"><p>The following example defines a public class method <code>bar</code> on class
<code>C</code>. The class method is specified by using the modifier <code>object</code> in
front of <code>method</code> in the method definition command.</p></div>
<div class="paragraph" id="xmp-object-applicable2"><div class="title">Listing 30: Class Methods</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {
  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Define a class method "bar" and an instance
</span>  <span class='nx-comment'># method "foo"
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {} {<span class='nx-keyword'>return</span> 2}
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {<span class='nx-keyword'>return</span> 1}

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Create an instance of the current class
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:create</span> c1
}

<span class='nx-comment'># Method "bar" is a class method of class "C"
</span><span class='nx-comment'># therefore applicable on the class object "C"
</span>C bar

<span class='nx-comment'># Method "foo" is an instance method of "C"
</span><span class='nx-comment'># therefore applicable on instance "c1"
</span>c1 foo

<span class='nx-comment'># When trying to invoke the class method on the
</span><span class='nx-comment'># instance, an error will be raised.
</span>c1 bar</pre></div></div>
<div class="paragraph"><p>In some other object-oriented programming languages, class methods
are called "static methods".</p></div>
</div>
</div>
<div class="sect2">
<h3 id="_ensemble_methods">3.5. Ensemble Methods</h3>
<div class="paragraph"><p>NX provides <em>ensemble methods</em> as a means to structure the method name
space and to group related methods. Ensemble methods are similar in
concept to Tcl&#8217;s ensemble commands.</p></div>
<div class="exampleblock">
<div class="content">
<div class="paragraph"><p>An <strong>ensemble method</strong> is a form of a hierarchical method consisting of
a container method and sub-methods. The first argument of the
container method is interpreted as a selector (the sub-method). Every
sub-method can be an container method as well.</p></div>
</div></div>
<div class="paragraph"><p>Ensemble methods provide a means to group related commands together,
and they are extensible in various ways. It is possible to add
sub-methods at any time to existing ensembles. Furthermore, it is
possible to extend ensemble methods via mixin classes.</p></div>
<div class="paragraph"><p>The following example defines an ensemble method for <code>string</code>. An
ensemble method is defined when the provide method name contains a
space.</p></div>
<div class="paragraph" id="xmp-ensemble-methods"><div class="title">Listing 31: Ensemble Method</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {

    <span class='nx-comment'># Define an ensemble method "string" with sub-methods
</span>    <span class='nx-comment'># "length", "tolower" and "info"
</span>
    <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> <span class='nx-string'>"string length"</span>  {x} {....}
    <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> <span class='nx-string'>"string tolower"</span> {x} {...}
    <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> <span class='nx-string'>"string info"</span> {x} {...}
    <span class='nx-comment'>#...
</span>    <span class='nx-keyword'>:create</span> c1
}

<span class='nx-comment'># Invoke the ensemble method
</span>c1 <span class='nx-keyword'>string</span> length <span class='nx-string'>"hello world"</span></pre></div></div>
</div>
<div class="sect2">
<h3 id="_method_resolution">3.6. Method Resolution</h3>
<div class="paragraph"><p>When a method is invoked, the applicable method is searched in the
following order:</p></div>
Per-object Mixins -> Per-class Mixins -> Object -> Intrinsic Class Hierarchy
<div class="paragraph"><p>In the case, no mixins are involved, first the object is searched for
an object method with the given name, and then the class hierarchy
of the object. The method can be defined multiple times on the search
path, so some of these method definitions might be <em>shadowed</em> by the
more specific definitions.</p></div>
<div class="paragraph" id="xmp-method-resolution"><div class="title">Listing 32: Method Resolution with Intrinsic Classes</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> C {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {
    <span class='nx-keyword'>return</span> <span class='nx-string'>"C foo: [next]"</span>
  }
}

<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> D -superclass C {

  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} {
    <span class='nx-keyword'>return</span> <span class='nx-string'>"D foo: [next]"</span>
  }

   <span class='nx-keyword'>:create</span> d1 {
     <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {} {
       <span class='nx-keyword'>return</span> <span class='nx-string'>"d1 foo: [next]"</span>
     }
   }
}

<span class='nx-comment'># Invoke the method foo
</span>d1 foo
<span class='nx-comment'># result: "d1 foo: D foo: C foo: "
</span>
<span class='nx-comment'># Query the precedence order from NX via introspection
</span>d1 <span class='nx-keyword'>info</span> precedence
<span class='nx-comment'># result: "::D ::C ::nx::Object"</span></pre></div></div>
<div class="paragraph"><p>Consider the example in
<a href="#xmp-method-resolution">Listing 32</a>. When the method
<code>foo</code> is invoked on object <code>d1</code>, the object method has the highest
precedence and is therefore invoked. The object methods shadows
the same-named methods in the class hierarchy, namely the method <code>foo</code>
of class <code>D</code> and the method <code>foo</code> of class <code>C</code>. The shadowed methods
can be still invoked, either via the primitive <code>next</code> or via method
handles (we used already method handles in the section about method
aliases). In the example above, <code>next</code> calls the shadowed method and
add their results to the results of every method. So, the final result
contains parts from <code>d1</code>, <code>D</code> and <code>C</code>. Note, that the topmost <code>next</code>
in method <code>foo</code> of class <code>C</code> shadows no method <code>foo</code> and simply
returns empty (and not an error message).</p></div>
<div class="paragraph"><p>The introspection method <code>info precedence</code> provides information about
the order, in which classes processed during method resolution.</p></div>
<div class="paragraph" id="xmp-method-resolution2"><div class="title">Listing 33: Method Resolution with Mixin Classes</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> M1 {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} { <span class='nx-keyword'>return</span> <span class='nx-string'>"M1 foo: [next]"</span>}
}
<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> M2 {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {} { <span class='nx-keyword'>return</span> <span class='nx-string'>"M2 foo: [next]"</span>}
}

<span class='nx-comment'>#
</span><span class='nx-comment'># "d1" is created based on the definitions of the last example
</span><span class='nx-comment'>#
</span><span class='nx-comment'># Add the methods from "M1" as per-object mixin to "d1"
</span>d1 <span class='nx-keyword'>object</span> mixins add M1

<span class='nx-comment'>#
</span><span class='nx-comment'># Add the methods from "M2" as per-class mixin to class "C"
</span>C mixins add M2

<span class='nx-comment'># Invoke the method foo
</span>d1 foo
<span class='nx-comment'># result: "M1 foo: M2 foo: d1 foo: D foo: C foo: "
</span>
<span class='nx-comment'># Query the precedence order from NX via introspection
</span>d1 <span class='nx-keyword'>info</span> precedence
<span class='nx-comment'># result: "::M1 ::M2 ::D ::C ::nx::Object"</span></pre></div></div>
<div class="paragraph"><p>The example in <a href="#xmp-method-resolution2">Listing 33</a> is
an extension of the previous example. We define here two additional
classes <code>M1</code> and <code>M2</code> which are used as per-object and per-class
mixins.  Both classes define the method <code>foo</code>, these methods shadow
the definitions of the intrinsic class hierarchy. Therefore an
invocation of <code>foo</code> on object <code>d1</code> causes first an invocation of
method in the per-object mixin.</p></div>
<div class="paragraph" id="xmp-method-resolution3"><div class="title">Listing 34: Method Invocation Flags</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-comment'>#
</span><span class='nx-comment'># "d1" is created based on the definitions of the last two examples,
</span><span class='nx-comment'># the mixins "M1" and "M2" are registered.
</span><span class='nx-comment'>#
</span><span class='nx-comment'># Define a public object method "bar", which calls the method
</span><span class='nx-comment'># "foo" which various invocation options:
</span><span class='nx-comment'>#
</span>d1 <span class='nx-keyword'>public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {} {
   <span class='nx-keyword'>puts</span> [:foo]
   <span class='nx-keyword'>puts</span> [: -local foo]
   <span class='nx-keyword'>puts</span> [: -intrinsic foo]
   <span class='nx-keyword'>puts</span> [: -system foo]
}

<span class='nx-comment'># Invoke the method "bar"
</span>d1 bar</pre></div></div>
<div class="paragraph"><p>In the first line of the body of method <code>bar</code>, the method <code>foo</code> is
called as usual with an implicit receiver, which defaults to the
current object (therefore, the call is equivalent to <code>d1 foo</code>). The
next three calls show how to provide flags that influence the method
resolution. The flags can be provided between the colon and the method
name. These flags are used rather seldom but can be helpful in some
situations.</p></div>
<div class="paragraph"><p>The invocation flag <code>-local</code> means that the method has to be resolved
from the same place, where the current method is defined. Since the
current method is defined as a object method, <code>foo</code> is resolved as
a object method. The effect is that the mixin definitions are
ignored. The invocation flag <code>-local</code> was already introduced int the
section about method protection, where it was used to call <em>private</em>
methods.</p></div>
<div class="paragraph"><p>The invocation flag <code>-intrinsic</code> means that the method has to be resolved
from the intrinsic definitions, meaning simply without mixins. The
effect is here the same as with the invocation flag <code>-local</code>.</p></div>
<div class="paragraph"><p>The invocation flag <code>-system</code> means that the method has to be resolved
from basic - typically predefined - classes of the object system. This
can be useful, when script overloads system methods, but still want to
call the shadowed methods from the base classes. In our case, we have
no definitions of <code>foo</code> on the base clases, therefore an error message
is returned.</p></div>
<div class="paragraph"><p>The output of <a href="#xmp-method-resolution3">Listing 34</a> is:</p></div>
<div class="listingblock">
<div class="content">
<pre><code>   M1 foo: M2 foo: d1 foo: D foo: C foo:
   d1 foo: D foo: C foo:
   d1 foo: D foo: C foo:
   ::d1: unable to dispatch method 'foo'</code></pre>
</div></div>
</div>
<div class="sect2">
<h3 id="_parameters">3.7. Parameters</h3>
<div class="paragraph"><p>NX provides a generalized mechanism for passing values to either
methods (we refer to these as <em>method parameters</em>) or to objects
(these are called <em>configure parameters</em>). Both kind of parameters
might have different features, such as:</p></div>
<div class="ulist"><ul>
<li>
<p>
Positional and non-positional parameters
</p>
</li>
<li>
<p>
Required and non-required parameters
</p>
</li>
<li>
<p>
Default values for parameters
</p>
</li>
<li>
<p>
Value-checking for parameters
</p>
</li>
<li>
<p>
Multiplicity of parameters
</p>
</li>
</ul></div>
<div class="paragraph"><p>TODO: complete list above and provide a short summary of the section</p></div>
<div class="paragraph"><p>Before we discuss method and configure parameters in more detail, we
describe the parameter features in the subsequent sections based on
method parameters.</p></div>
<div class="sect3">
<h4 id="_positional_and_non_positional_parameters">3.7.1. Positional and Non-Positional Parameters</h4>
<div class="paragraph"><p>If the position of a parameter in the list of formal arguments
(e.g. passed to a function) is significant for its meaning, this is a
<em>positional</em> parameter. If the meaning of the parameter is independent
of its position, this is a <em>non-positional</em> parameter. When we call a
method with positional parameters, the meaning of the parameters (the
association with the argument in the argument list of the method) is
determined by its position. When we call a method with non-positional
parameters, their meaning is determined via a name passed with the
argument during invocation.</p></div>
<div class="paragraph" id="xmp-posnonpos"><div class="title">Listing 35: Positional and Non-Positional Method Parameters</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o1 {

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Method foo has positional parameters:
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {x y} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y=$y"</span>
  }

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Method bar has non-positional parameters:
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {-x -y} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y=$y"</span>
  }

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Method baz has non-positional and
</span>  <span class='nx-comment'># positional parameters:
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> baz {-x -y a} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x? [info exists x] y? [info exists y] a=$a"</span>
  }
}

<span class='nx-comment'># invoke foo (positional parameters)
</span>o1 foo 1 2

<span class='nx-comment'># invoke bar (non-positional parameters)
</span>o1 bar -y 3 -x 1
o1 bar -x 1 -y 3

<span class='nx-comment'># invoke baz (positional and non-positional parameters)
</span>o1 baz -x 1 100
o1 baz 200
o1 baz -- -y</pre></div></div>
<div class="paragraph"><p>Consider the example in <a href="#xmp-posnonpos">Listing 35</a>. The method
<code>foo</code> has the argument list <code>x y</code>. This means that the first argument
is passed in an invocation like <code>o1 foo 1 2</code> to <code>x</code> (here, the value
<code>1</code>), and the second argument is passed to <code>y</code> (here the value <code>2</code>).
Method <code>bar</code> has in contrary just with non-positional arguments. Here
we pass the names of the parameter together with the values. In the
invocation <code>o1 bar -y 3 -x 1</code> the names of the parameters are prefixed
with a dash ("-"). No matter whether in which order we write the
non-positional parameters in the invocation (see line 30 and 31 in
<a href="#xmp-posnonpos">Listing 35</a>) in both cases the variables <code>x</code>
and <code>y</code> in the body of the method <code>bar</code> get the same values assigned
(<code>x</code> becomes <code>1</code>, <code>y</code> becomes <code>3</code>).</p></div>
<div class="paragraph"><p>It is certainly possible to combine positional and non-positional
arguments. Method <code>baz</code> provides two non-positional parameter (<code>-y</code>
and <code>-y</code>) and one positional parameter (namely <code>a</code>). The invocation in
line 34 passes the value of <code>1</code> to <code>x</code> and the value of <code>100</code> to <code>a</code>.
There is no value passed to <code>y</code>, therefore value of <code>y</code> will be
undefined in the body of <code>baz</code>, <code>info exists y</code> checks for the
existence of the variable <code>y</code> and returns <code>0</code>.</p></div>
<div class="paragraph"><p>The invocation in line 35 passes only a value to the positional
parameter. A more tricky case is in line 36, where we want to pass
<code>-y</code> as a value to the positional parameter <code>a</code>. The case is more
tricky since syntactically the argument parser might consider <code>-y</code> as
the name of one of the non-positional parameter. Therefore we use <code>--</code>
(double dash) to indicate the end of the block of the non-positional
parameters and therefore the value of <code>-y</code> is passed to <code>a</code>.</p></div>
</div>
<div class="sect3">
<h4 id="_optional_and_required_parameters">3.7.2. Optional and Required Parameters</h4>
<div class="paragraph"><p>Per default positional parameters are required, and non-positional
parameters are optional (they can be left out). By using parameter
options, we can as well define positional parameters, which are
optional, and non-positional parameters, which are required.</p></div>
<div class="paragraph" id="xmp-optional-req"><div class="title">Listing 36: Optional and Required Method Parameters</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o2 {

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Method foo has one required and one optional
</span>  <span class='nx-comment'># positional parameter:
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {x:required y:optional} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y? [info exists y]"</span>
  }

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Method bar has one required and one optional
</span>  <span class='nx-comment'># non-positional parameter:
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {-x:required -y:optional} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y? [info exists y]"</span>
  }
}

<span class='nx-comment'># invoke foo (one optional positional parameter is missing)
</span>o2 foo 1</pre></div></div>
<div class="paragraph"><p>The example in <a href="#xmp-optional-req">Listing 36</a> defined method <code>foo</code>
with one required and one optional positional parameter. For this
purpose we use the parameter options <code>required</code> and <code>optional</code>. The
parameter options are separated from the parameter name by a colon. If
there are multiple parameter options, these are separated by commas
(we show this in later examples).</p></div>
<div class="paragraph"><p>The parameter definition <code>x:required</code> for method <code>foo</code> is equivalent
to <code>x</code> without any parameter options (see e.g. previous example),
since positional parameters are per default required. The invocation
in line 21 of <a href="#xmp-optional-req">Listing 36</a> will lead to an
undefined variable <code>y</code> in method <code>foo</code>, because no value us passed to
the optional parameter. Note that only trailing positional parameters might be
optional. If we would call method <code>foo</code> of <a href="#xmp-posnonpos">Listing 35</a> with only one argument, the system would raise an
exception.</p></div>
<div class="paragraph"><p>Similarly, we define method <code>bar</code> in <a href="#xmp-optional-req">Listing 36</a> with one required and one optional non-positional
parameter. The parameter definition <code>-y:optional</code> is equivalent to
<code>-y</code>, since non-positional parameter are per default optional.
However, the non-positional parameter <code>-x:required</code> is required. If we
invoke <code>bar</code> without it, the system will raise an exception.</p></div>
</div>
<div class="sect3">
<h4 id="_default_values_for_parameters">3.7.3. Default Values for Parameters</h4>
<div class="paragraph"><p>Optional parameters might have a default value. This default value is used,
when no argument is provided for the corresponding parameter.  Default values can be
specified for positional and non-positional parameters.</p></div>
<div class="paragraph" id="xmp-default-value"><div class="title">Listing 37: Method Parameters with Default Values</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o3 {

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Positional parameter with default value:
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {{x 1} {y 2}} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y=$y"</span>
  }

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Non-positional parameter with default value:
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {{-x 10} {-y 20}} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x y=$y"</span>
  }
}

<span class='nx-comment'># use default values
</span>o3 foo
o3 bar</pre></div></div>
<div class="paragraph"><p>In order to define a default value for a parameter, the parameter
specification must be of the form of a 2 element list, where the
second argument is the default value. See for an example in
<a href="#xmp-default-value">Listing 37</a>.</p></div>
</div>
<div class="sect3">
<h4 id="_value_constraints">3.7.4. Value Constraints</h4>
<div class="paragraph"><p>NX provides value constraints for all kind of parameters. By
specifying value constraints a developer can restrict the permissible
values for a parameter and document the expected values in the source
code. Value checking in NX is conditional, it can be turned on or off
in general or on a per-usage level (more about this later). The same
mechanisms can be used not only for input value checking, but as well
for return value checking (we will address this point as well later).</p></div>
<div class="sect4">
<h5 id="_built_in_value_constraints">Built-in Value Constraints</h5>
<div class="paragraph"><p>NX comes with a set of built-in value constraints, which can be
extended on the scripting level. The built-in checkers are either the
native checkers provided directly by the Next Scripting Framework (the
most efficient checkers) or the value checkers provided by Tcl through
<code>string is &#8230;</code>. The built-in checkers have as well the advantage that
they can be used also at any time during bootstrap of an object
system, at a time, when e.g. no objects or methods are defined. The
same checkers are used as well for all C-implemented primitives of NX
and the Next Scripting Framework.</p></div>
<div class="imageblock" id="img-value-checkers" style="text-align:center;">
<div class="content">
<img src="value-checkers.png" alt="value-checkers.png" />
</div>
<div class="title">Figure 38. General Applicable Value Checkers in NX</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p><a href="#img-value-checkers">Figure 38</a> shows the built-in
general applicable value checkers available in NX, which can be used
for all method and configure parameters. In the next step, we show how to
use these value-checkers for checking permissible values for method
parameters. Then we will show, how to provide more detailed value
constraints.</p></div>
<div class="paragraph" id="xmp-value-check"><div class="title">Listing 39: Method Parameters with Value Constraints</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o4 {

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Positional parameter with value constraints:
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {x:integer o<span class='nx-keyword'>:object</span>,optional} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x o? [info exists o]"</span>
  }

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Non-positional parameter with value constraints:
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {{-x:integer 10} {-verbose:boolean false}} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x verbose=$verbose"</span>
  }
}

<span class='nx-comment'># The following invocation raises an exception, since the
</span><span class='nx-comment'># value "a" for parameter "x" is not an integer
</span>o4 foo a</pre></div></div>
<div class="paragraph"><p>Value constraints are specified as parameter options in the parameter
specifications. The parameter specification <code>x:integer</code> defines <code>x</code> as
a required positional parameter which value is constraint to an
integer. The parameter specification <code>o:object,optional</code> shows how to
combine multiple parameter options. The parameter <code>o</code> is an optional
positional parameter, its value must be an object (see
<a href="#xmp-value-check">Listing 39</a>). Value constraints are
specified exactly the same way for non-positional parameters (see
method <code>bar</code> in <a href="#xmp-value-check">Listing 39</a>).</p></div>
<div class="paragraph" id="xmp-check-parameterized"><div class="title">Listing 40: Parameterized Value Constraints</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-comment'>#
</span><span class='nx-comment'># Create classes for Person and Project
</span><span class='nx-comment'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Person
<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Project

<span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o5 {
  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Parameterized value constraints
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> work {
     -person<span class='nx-keyword'>:object</span>,type=Person
     -project<span class='nx-keyword'>:object</span>,type=Project
   } {
    <span class='nx-comment'># ...
</span>  }
}

<span class='nx-comment'>#
</span><span class='nx-comment'># Create a Person and a Project instance
</span><span class='nx-comment'>#
</span>Person <span class='nx-keyword'>create</span> gustaf
Project <span class='nx-keyword'>create</span> nx

<span class='nx-comment'>#
</span><span class='nx-comment'># Use method with value constraints
</span><span class='nx-comment'>#
</span>o5 work -person gustaf -project nx</pre></div></div>
<div class="paragraph"><p>The native checkers <code>object</code>, <code>class</code>, <code>metaclass</code> and <code>baseclass</code> can
be further specialized with the parameter option <code>type</code> to restrict
the permissible values to instances of certain classes. We can use for
example the native value constraint <code>object</code> either for testing
whether an argument is some object (without further constraints, as in
<a href="#xmp-default-value">Listing 37</a>, method <code>foo</code>), or we can
constrain the value further to some type (direct or indirect instance
of a class). This is shown by method <code>work</code> in
<a href="#xmp-check-parameterized">Listing 40</a> which requires
the parameter <code>-person</code> to be an instance of class <code>Person</code> and the
parameter <code>-project</code> to be an instance of class <code>Project</code>.</p></div>
</div>
<div class="sect4">
<h5 id="_scripted_value_constraints">Scripted Value Constraints</h5>
<div class="paragraph"><p>The set of predefined value checkers can be extended by application
programs via defining methods following certain conventions. The user
defined value checkers are defined as methods of the class <code>nx::Slot</code>
or of one of its subclasses or instances. We will address such cases
in the next sections. In the following example we define two new
value checkers on class <code>nx::Slot</code>. The first value checker is called
<code>groupsize</code>, the second one is called <code>choice</code>.</p></div>
<div class="paragraph" id="xmp-user-types"><div class="title">Listing 41: Scripted Value Checker for Method Parameters</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-comment'>#
</span><span class='nx-comment'># Value checker named "groupsize"
</span><span class='nx-comment'>#
</span>::nx::Slot <span class='nx-keyword'>method</span> type=groupsize {name value} {
  <span class='nx-keyword'>if</span> {<span class='nx-variable'>$value</span> &lt; 1 || <span class='nx-variable'>$value</span> &gt; 6} {
    <span class='nx-keyword'>error</span> <span class='nx-string'>"Value '$value' of parameter $name is not between 1 and 6"</span>
  }
}

<span class='nx-comment'>#
</span><span class='nx-comment'># Value checker named "choice" with extra argument
</span><span class='nx-comment'>#
</span>::nx::Slot <span class='nx-keyword'>method</span> type=choice {name value arg} {
  <span class='nx-keyword'>if</span> {<span class='nx-variable'>$value</span> ni [<span class='nx-keyword'>split</span> <span class='nx-variable'>$arg</span> |]} {
    <span class='nx-keyword'>error</span> <span class='nx-string'>"Value '$value' of parameter $name not in permissible values $arg"</span>
  }
}

<span class='nx-comment'>#
</span><span class='nx-comment'># Create an application class D
</span><span class='nx-comment'># using the new value checkers
</span><span class='nx-comment'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> D {
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> foo {a:groupsize} {
    <span class='nx-comment'># ...
</span>  }
  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>method</span> bar {a:choice,arg=red|yellow|green b:choice,arg=good|bad} {
    <span class='nx-comment'># ...
</span>  }
}

D <span class='nx-keyword'>create</span> d1

<span class='nx-comment'># testing "groupsize";
</span><span class='nx-comment'># the second call (with value 10) will raise an exception:
</span>d1 foo 2
d1 foo 10

<span class='nx-comment'># testing "choice"
</span><span class='nx-comment'># the second call (with value pink for parameter a)
</span><span class='nx-comment'># will raise an exception:
</span>d1 bar green good
d1 bar pink bad</pre></div></div>
<div class="paragraph"><p>In order to define a checker <code>groupsize</code> a method of the name
<code>type=groupsize</code> is defined. This method receives two arguments,
<code>name</code> and <code>value</code>. The first argument is the name of the parameter
(mostly used for the error message) and the second parameter is
provided value. The value checker simply tests whether the provided
value is between 1 and 3 and raises an exception if this is not the
case (invocation in line 36 in <a href="#xmp-user-types">Listing 41</a>).</p></div>
<div class="paragraph"><p>The checker <code>groupsize</code> has the permissible values defined in its
method&#8217;s body. It is as well possible to define more generic checkers
that can be parameterized. For this parameterization, one can pass an
argument to the checker method (last argument). The checker <code>choice</code>
can be used for restricting the values to a set of predefined
constants. This set is defined in the parameter specification. The
parameter <code>a</code> of method <code>bar</code> in <a href="#xmp-user-types">Listing 41</a>
is restricted to the values <code>red</code>, <code>yellow</code> or <code>green</code>, and the
parameter <code>b</code> is restricted to <code>good</code> or <code>bad</code>. Note that the syntax
of the permissible values is solely defined by the definition of the
value checker in lines 13 to 17. The invocation in line 39 will be ok,
the invocation in line 40 will raise an exception, since <code>pink</code> is not
allowed.</p></div>
<div class="paragraph"><p>If the same checks are used in many places in the program,
defining names for the value checker will be the better choice since
it improves maintainability. For seldom used kind of checks, the
parameterized value checkers might be more convenient.</p></div>
</div>
</div>
<div class="sect3">
<h4 id="_multiplicity">3.7.5. Multiplicity</h4>
<div class="sidebarblock">
<div class="content">
<div class="paragraph"><p><strong>Multiplicity</strong> is used to define whether a parameter should receive
single or multiple values.</p></div>
</div></div>
<div class="paragraph"><p>A multiplicity specification has a lower and an upper bound. A lower
bound of <code>0</code> means that the value might be empty. A lower bound of <code>1</code>
means that the parameter needs at least one value. The upper bound
might be <code>1</code> or <code>n</code> (or synonymously <code>*</code>). While the upper bound of
<code>1</code> states that at most one value has to be passed, the upper bound of
<code>n</code> says that multiple values are permitted. Other kinds of
multiplicity are currently not allowed.</p></div>
<div class="paragraph"><p>The multiplicity is written as parameter option in the parameter
specification in the form <em>lower-bound</em>..<em>upper-bound</em>. If no
multiplicity is defined the default multiplicity is <code>1..1</code>, which
means: provide exactly one (atomic) value (this was the case in the
previous examples).</p></div>
<div class="paragraph" id="xmp-multiplicity"><div class="title">Listing 42: Method Parameters with Explicit Multiplicity</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Object</span> <span class='nx-keyword'>create</span> o6 {

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Positional parameter with an possibly empty
</span>  <span class='nx-comment'># single value
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> foo {x:integer,0..1} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x"</span>
  }

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Positional parameter with an possibly empty
</span>  <span class='nx-comment'># list of values value
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> bar {x:integer,0..n} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x"</span>
  }

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># Positional parameter with a non-empty
</span>  <span class='nx-comment'># list of values
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> baz {x:integer,1..n} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"x=$x"</span>
  }
}</pre></div></div>
<div class="paragraph"><p><a href="#xmp-multiplicity">Listing 42</a> contains three examples for
positional parameters with different multiplicities. Multiplicity is
often combined with value constraints. A parameter specification of
the form <code>x:integer,0..n</code> means that the parameter <code>x</code> receives a list
of integers, which might be empty. Note that the value constraints are
applied to every single element of the list.</p></div>
<div class="paragraph"><p>The parameter specification <code>x:integer,0..1</code> means that <code>x</code> might be
an integer or it might be empty. This is one style of specifying that
no explicit value is passed for a certain parameter. Another style is
to use required or optional parameters. NX does not enforce any
particular style for handling unspecified values.</p></div>
<div class="paragraph"><p>All the examples in <a href="#xmp-multiplicity">Listing 42</a> are for
single positional parameters. Certainly, multiplicity is fully
orthogonal with the other parameter features and can be used as well
for multiple parameters, non-positional parameter, default values,
etc.</p></div>
</div>
<div class="sect3">
<h4 id="_defaults_substitution">3.7.6. Defaults substitution</h4>
<div class="paragraph"><p>Optional object and method parameters can set a default value. Recall
that default values can be specified for positional and non-positional
parameters, alike. This default value is used to define a
corresponding method-local and object variable, respectively, and to
set it to the default value. By default, the default value is taken
literally (without any substitutions). Default values can also be
preprocessed into a final value using Tcl substitution as provided by
the Tcl <code>[subst]</code> command. To control the kind of substitutions to be
performed, the parameter option <code>substdefault</code> can be provided.</p></div>
<div class="paragraph" id="substdefault"><div class="title">Listing 43: Default-value substitution using <code>substdefault</code></div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> ::D
<span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> ::C {
  <span class='nx-comment'>#
</span>  <span class='nx-comment'># By default all substitutions (command, variable, control
</span>  <span class='nx-comment'># characters) are active, when "substdefault" is used:
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:property</span> {d<span class='nx-keyword'>:object</span>,type=::D,substdefault {[::D <span class='nx-keyword'>new</span>]}}

  <span class='nx-comment'>#
</span>  <span class='nx-comment'># The actual property values are computed and
</span>  <span class='nx-comment'># set at instantiation time.
</span>  <span class='nx-comment'>#
</span>  <span class='nx-keyword'>:create</span> ::c
}

::c <span class='nx-keyword'>cget</span> -d</pre></div></div>
<div class="paragraph"><p><a href="#substdefault">Listing 43</a> uses <code>substdefault</code>
to provide a default value for the property <code>d</code>. In this example, the
default value is a fresh instance of
class <code>::D</code>. When the parameter option <code>substdefault</code> is used
default, all substitution kinds of Tcl are active: command, variable, and
backslash substitution. <code>substdefault</code> can be
parametrized to include or to exclude any combination of substitution
kinds by providing a bitmask:</p></div>
<div class="ulist"><ul>
<li>
<p>
<code>substdefault=0b111</code>: all substitutions active (default)
</p>
</li>
<li>
<p>
<code>substdefault=0b100</code>: substitute backslashes only (like <code>subst -novariables -nocommands</code>)
</p>
</li>
<li>
<p>
<code>substdefault=0b010</code>: substitute variables only (like <code>subst -nobackslashes -nocommands</code>)
</p>
</li>
<li>
<p>
<code>substdefault=0b001</code>: substitute commands only (like <code>subst -nobackslashes -novariables</code>)
</p>
</li>
<li>
<p>
<code>substdefault=0b000</code>: substitute nothing (like <code>subst -nobackslashes -nocommands -novariables</code>, noop)
</p>
</li>
</ul></div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_advanced_language_features">4. Advanced Language Features</h2>
<div class="sectionbody">
<div class="paragraph"><p>&#8230;</p></div>
<div class="sect2">
<h3 id="_objects_classes_and_meta_classes">4.1. Objects, Classes and Meta-Classes</h3>
<div class="paragraph"><p>&#8230;</p></div>
</div>
<div class="sect2">
<h3 id="_resolution_order_and_next_path">4.2. Resolution Order and Next-Path</h3>
<div class="paragraph"><p>&#8230;</p></div>
</div>
<div class="sect2">
<h3 id="_details_on_method_and_configure_parameters">4.3. Details on Method and Configure Parameters</h3>
<div class="paragraph"><p>The parameter specifications are used in NX for the following
purposes. They are used for</p></div>
<div class="ulist"><ul>
<li>
<p>
the specification of input arguments of methods and commands, for
</p>
</li>
<li>
<p>
the specification of return values of methods and commands, and for
</p>
</li>
<li>
<p>
the specification for the initialization of objects.
</p>
</li>
</ul></div>
<div class="paragraph"><p>We refer to the first two as method parameters and the last one as
configure parameters. The examples in the previous sections all parameter
specification were specifications of method parameters.</p></div>
<div class="sidebarblock">
<div class="content">
<div class="paragraph"><p><strong>Method parameters</strong> specify properties about permissible values passed
to methods.</p></div>
</div></div>
<div class="paragraph"><p>The method parameter specify how methods are invoked, how the
actual arguments are passed to local variables of the invoked method
and what kind of checks should be performed on these.</p></div>
<div class="sidebarblock">
<div class="content">
<div class="paragraph"><p><strong>Configure parameters</strong> are parameters that specify, how objects
can be parameterized upon creation.</p></div>
</div></div>
<div class="paragraph"><p>Syntactically, configure parameters and method parameters are the same,
although there are certain differences (e.g. some parameter options
are only applicable for objects parameters, the list of object
parameters is computed dynamically from the class structures, object
parameters are often used in combination with special setter methods,
etc.).  Consider the following example, where we define the two
application classes <code>Person</code> and <code>Student</code> with a few properties.</p></div>
<div class="paragraph" id="xmp-object-parameters"><div class="title">Listing 44: Configure Parameters</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-comment'>#
</span><span class='nx-comment'># Define a class Person with properties "name"
</span><span class='nx-comment'># and "birthday"
</span><span class='nx-comment'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Person {
  <span class='nx-keyword'>:property</span> name:required
  <span class='nx-keyword'>:property</span> birthday
}

<span class='nx-comment'>#
</span><span class='nx-comment'># Define a class Student as specialization of Person
</span><span class='nx-comment'># with and additional property
</span><span class='nx-comment'>#
</span><span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>create</span> Student -superclass Person {
  <span class='nx-keyword'>:property</span> matnr:required
  <span class='nx-keyword'>:property</span> {oncampus:boolean true}
}

<span class='nx-comment'>#
</span><span class='nx-comment'># Create instances using configure parameters
</span><span class='nx-comment'># for the initialization
</span><span class='nx-comment'>#
</span>Person <span class='nx-keyword'>create</span> p1 -name Bob
Student <span class='nx-keyword'>create</span> s1 -name Susan -matnr 4711

<span class='nx-comment'># Access property value via "cget" method
</span><span class='nx-keyword'>puts</span> <span class='nx-string'>"The name of s1 is [s1 cget -name]"</span></pre></div></div>
<div class="paragraph"><p>The class <code>Person</code> has two properties <code>name</code> and <code>birthday</code>, where the
property <code>name</code> is required, the property <code>birthday</code> is not. The
class <code>Student</code> is a subclass of <code>Person</code> with the additional required
property <code>matnr</code> and an optional property <code>oncampus</code> with the
default value <code>true</code> (see <a href="#xmp-object-parameters">Listing 44</a>). The class diagram below visualizes these
definitions.</p></div>
<div class="imageblock" id="img-configure-parameters" style="text-align:center;">
<div class="content">
<img src="configure-parameter.png" alt="configure-parameter.png" />
</div>
<div class="title">Figure 45. System and Application Classes</div>
</div>
<div class="paragraph"><p></p></div>
<div class="paragraph"><p>In NX, these definitions imply that instances of the class of <code>Person</code>
have the properties <code>name</code> and <code>birthday</code> as <em>non-positional object
parameters</em>.  Furthermore it implies that instances of <code>Student</code> will
have the configure parameters of <code>Person</code> augmented with the object
parameters from <code>Student</code> (namely <code>matnr</code> and <code>oncampus</code>).  Based on
these configure parameters, we can create a <code>Person</code> named <code>Bob</code> and a
<code>Student</code> named <code>Susan</code> with the matriculation number <code>4711</code> (see line
23 and 24 in &lt;&lt;xmp-object-parameters,
instance variables <code>name</code>, <code>matnr</code> and <code>oncampus</code> (the latter is
initialized with the default value).</p></div>
<div class="sect3">
<h4 id="_configure_parameters_available_for_all_nx_objects">4.3.1. Configure Parameters available for all NX Objects</h4>
<div class="paragraph"><p>The configure parameters are not limited to the application defined
properties, also NX provides some predefined definitions. Since
<code>Person</code> is a subclass of <code>nx::Object</code> also the configure parameters of
<code>nx::Object</code> are inherited. In the introductory stack example, we used
<code>-mixins</code> applied to an object to denote per-object mixins (see
<a href="#xmp-using-class-safety">Listing 8</a>). Since <code>mixins</code>
is defined as a parameter on <code>nx::Object</code> it can be used as an object
parameter <code>-mixins</code> for all objects in NX. To put it in other words,
every object can be configured to have per-object mixins. If we would
remove this definition, this feature would be removed as well.</p></div>
<div class="paragraph"><p>As shown in the introductory examples, every object can be configured
via a scripted initialization block (the optional scripted block
specified at object creation as last argument; see
<a href="#xmp-object-stack">Listing 5</a> or
<a href="#xmp-object-integer-stack">Listing 12</a>). The
scripted block and its meaning are as well defined by the means of
configure parameters. However, this configure parameter is positional (last
argument) and optional (it can be omitted). The following listing shows
the configure parameters of <code>Person p1</code> and <code>Student s1</code>.</p></div>
<div class="paragraph" id="xmp-object-parameter-list"><div class="title">Listing 46: Computed Actual Configure Parameter</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'>Configure parameters <span class='nx-keyword'>for</span> Person p1:
   Command:
      p1 <span class='nx-keyword'>info</span> <span class='nx-keyword'>lookup</span> syntax <span class='nx-keyword'>configure</span>
   Result:
      -name <span class='nx-placeholder'>/value/</span> ?-birthday <span class='nx-placeholder'>/value/</span>? ?-object-mixins /mixinreg .../<span class='nx-placeholder'>?
      ?</span>-class /<span class='nx-keyword'>class</span>/? ?-object-filters /filterreg .../? ?/__initblock/?

Configure parameter <span class='nx-keyword'>for</span> Student s1:
   Command:
      s1 <span class='nx-keyword'>info</span> <span class='nx-keyword'>lookup</span> syntax <span class='nx-keyword'>configure</span>
   Result:
      ?-oncampus <span class='nx-placeholder'>/boolean/</span>? -matnr <span class='nx-placeholder'>/value/</span> -name <span class='nx-placeholder'>/value/</span>
      ?-birthday <span class='nx-placeholder'>/value/</span>? ?-object-mixins /mixinreg .../? ?-class /<span class='nx-keyword'>class</span>/<span class='nx-placeholder'>?
      ?</span>-object-filters /filterreg .../? ?/__initblock/?</pre></div></div>
<div class="paragraph"><p>The given parameter show, how (a) objects can be configured
at runtime or (b) how new instances can be configured
at creation time via the <code>new</code> or <code>create</code> methods.
Introspection can be used to obtain the configuration
parameters from an object via
<code>p1 info lookup parameters configure</code>
(returning the configure parameters currently applicable for
<code>configure</code> or <code>cget</code>) or from a class
<code>Person info lookup parameters create</code> on a class
(returning the configure parameters applicable when an object
of this class is created)</p></div>
<div class="paragraph"><p>The listed configure parameter types <code>mixinreg</code> and
<code>filterreg</code> are for converting definitions of filters and mixins.  The
last value <code>__initblock</code> says that the content of this variable
will be executed in the context of the object being created (before
the constructor <code>init</code> is called).  More about the configure parameter
types later.</p></div>
</div>
<div class="sect3">
<h4 id="_configure_parameters_available_for_all_nx_classes">4.3.2. Configure Parameters available for all NX Classes</h4>
<div class="paragraph"><p>Since classes are certain kind of objects, classes are parameterized
in the same way as objects. A typical parameter for a class definition
is the relation of the class to its superclass.In our example, we have
specified, that <code>Student</code> has <code>Person</code> as superclass via the
non-positional configure parameter <code>-superclass</code>. If no superclass is
specified for a class, the default superclass is
<code>nx::Object</code>. Therefore <code>nx::Object</code> is the default value for the
parameter <code>superclass</code>.</p></div>
<div class="paragraph"><p>Another frequently used parameter for classes is <code>-mixins</code> to denote
per-class mixins (see e.g. the introductory Stack example in
<a href="#xmp-class-safestack">Listing 10</a>), which is defined in
the same way.</p></div>
<div class="paragraph"><p>Since <code>Student</code> is an instance of the meta-class <code>nx::Class</code> it
inherits the configure parameters from <code>nx::Class</code> (see class diagram
<a href="#img-configure-parameters">Figure 45</a>).
Therefore, one can use e.g. <code>-superclass</code> in the definition of classes.</p></div>
<div class="paragraph"><p>Since <code>nx::Class</code> is a subclass of <code>nx::Object</code>, the meta-class
<code>nx::Class</code> inherits the parameter definitions from the most general
class <code>nx::Object</code>. Therefore, every class might as well be configured
with a scripted initialization block the same way as objects can be
configured. We used actually this scripted initialization block in
most examples for defining the methods of the class. The following
listing shows (simplified) the parameters applicable for <code>Class
Student</code>.</p></div>
<div class="paragraph" id="xmp-class-parameter-list"><div class="title">Listing 47: Parameters for Classes</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'>Configure parameter <span class='nx-keyword'>for</span> <span class='nx-keyword'>class</span> <span class='nx-keyword'>nx::Class</span>
   Command:
      <span class='nx-keyword'>nx::Class</span> <span class='nx-keyword'>info</span> <span class='nx-keyword'>lookup</span> syntax <span class='nx-keyword'>configure</span>
   Result:
      ?-superclass /<span class='nx-keyword'>class</span> .../? ?-mixins /mixinreg .../<span class='nx-placeholder'>?
      ?</span>-filters /filterreg .../? ?-object-mixins /mixinreg .../<span class='nx-placeholder'>?
      ?</span>-class /<span class='nx-keyword'>class</span>/? ?-object-filters /filterreg .../? ?/__initblock/?</pre></div></div>
</div>
<div class="sect3">
<h4 id="_user_defined_parameter_types">4.3.3. User defined Parameter Types</h4>
<div class="paragraph"><p>More detailed definition of the configure parameter types comes here.</p></div>
</div>
<div class="sect3">
<h4 id="_slot_classes_and_slot_objects">4.3.4. Slot Classes and Slot Objects</h4>
<div class="paragraph"><p>In one of the previous sections, we defined scripted (application
defined) checker methods on a class named <code>nx::Slot</code>.  In general NX
offers the possibility to define value checkers not only for all
usages of parameters but as well differently for method parameters or
configure parameters</p></div>
<div class="imageblock" id="img-slots" style="text-align:center;">
<div class="content">
<img src="slots.png" alt="slots.png" />
</div>
<div class="title">Figure 48. Slot Classes and Objects</div>
</div>
<div class="paragraph"><p></p></div>
</div>
<div class="sect3">
<h4 id="_attribute_slots">4.3.5. Attribute Slots</h4>
<div class="paragraph"><p>Still Missing</p></div>
<div class="ulist"><ul>
<li>
<p>
return value checking
</p>
</li>
<li>
<p>
switch
</p>
</li>
<li>
<p>
initcmd &#8230;
</p>
</li>
<li>
<p>
subst rules
</p>
</li>
<li>
<p>
converter
</p>
</li>
<li>
<p>
incremental slots
</p>
</li>
</ul></div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_miscellaneous">5. Miscellaneous</h2>
<div class="sectionbody">
<div class="paragraph"><p>&#8230;</p></div>
<div class="sect2">
<h3 id="_profiling">5.1. Profiling</h3>
<div class="paragraph"><p>&#8230;</p></div>
</div>
<div class="sect2">
<h3 id="_unknown_handlers">5.2. Unknown Handlers</h3>
<div class="paragraph"><p>NX provides two kinds of unknown handlers:</p></div>
<div class="ulist"><ul>
<li>
<p>
Unknown handlers for methods
</p>
</li>
<li>
<p>
Unknown handlers for objects and classes
</p>
</li>
</ul></div>
<div class="sect3">
<h4 id="_unknown_handlers_for_methods">5.2.1. Unknown Handlers for Methods</h4>
<div class="paragraph"><p>Object and classes might be equipped
with a method <code>unknown</code> which is called in cases, where an unknown
method is called. The method unknown receives as first argument the
called method followed by the provided arguments</p></div>
<div class="paragraph" id="xmp-unknown-method"><div class="title">Listing 49: Unknown Method Handler</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'>::nx::Object <span class='nx-keyword'>create</span> o {
  <span class='nx-keyword'>:object</span> <span class='nx-keyword'>method</span> <span class='nx-keyword'>unknown</span> {called_method args} {
    <span class='nx-keyword'>puts</span> <span class='nx-string'>"Unknown method '$called_method' called"</span>
  }
}

<span class='nx-comment'># Invoke an unknown method for object o:
</span>o foo 1 2 3

<span class='nx-comment'># Output will be: "Unknown method 'foo' called"</span></pre></div></div>
<div class="paragraph"><p>Without any provision of an unknown method handler, an error will be
raised, when an unknown method is called.</p></div>
</div>
<div class="sect3">
<h4 id="_unknown_handlers_for_objects_and_classes">5.2.2. Unknown Handlers for Objects and Classes</h4>
<div class="paragraph"><p>The next scripting framework provides in addition to unknown method
handlers also a means to dynamically create objects and classes, when
these are referenced. This happens e.g. when superclasses, mixins, or
parent objects are referenced. This mechanism can be used to implement
e.g. lazy loading of these classes.  Nsf allows one to register multiple
unknown handlers, each identified by a key (a unique name, different
from the keys of other unknown handlers).</p></div>
<div class="paragraph" id="xmp-unknown-class"><div class="title">Listing 50: Unknown Class Handler</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'>::nx::Class <span class='nx-keyword'>public</span> <span class='nx-keyword'>object</span> <span class='nx-keyword'>method</span> __unknown {name} {
  <span class='nx-comment'># A very simple unknown handler, showing just how
</span>  <span class='nx-comment'># the mechanism works.
</span>  <span class='nx-keyword'>puts</span> <span class='nx-string'>"***** __unknown called with &lt;$name&gt;"</span>
  ::nx::Class <span class='nx-keyword'>create</span> <span class='nx-variable'>$name</span>
}

<span class='nx-comment'># Register an unknown handler as a method of ::nx::Class
</span>::nsf::object::unknown::add nx {::nx::Class __unknown}

::nx::Object <span class='nx-keyword'>create</span> o {
  <span class='nx-comment'># The class M is unknown at this point
</span>
  <span class='nx-keyword'>:object</span> mixins add M
  <span class='nx-comment'># The line above has triggered the unknown class handler,
</span>  <span class='nx-comment'># class M is now defined
</span>
  <span class='nx-keyword'>puts</span> [<span class='nx-keyword'>:info</span> <span class='nx-keyword'>object</span> mixins]
  <span class='nx-comment'># The output will be:
</span>  <span class='nx-comment'>#     ***** __unknown called with &lt;::M&gt;
</span>  <span class='nx-comment'>#     ::M
</span>}</pre></div></div>
<div class="paragraph"><p>The Next Scripting Framework allows one to add, query, delete and list unknown handlers.</p></div>
<div class="paragraph" id="xmp-unknown-registration"><div class="title">Listing 51: Unknown Handler registration</div><p></p></div>
<div class="listingblock">
<div class="content"><style type='text/css'>
.nx             {color: #000000; font-weight: normal; font-style: normal; padding-left: 10px}
table.nx        {border-collapse: collapse; border-spacing: 3px;}
.nx-linenr      {border-right: 1px solid #DDDDDD;padding-right: 5px; color: #2B547D;font-style: italic;}
.nx-string      {color: #779977; font-weight: normal; font-style: italic;}
.nx-comment     {color: #717ab3; font-weight: normal; font-style: italic;}
.nx-keyword     {color: #7f0055; font-weight: normal; font-style: normal;}
.nx-placeholder {color: #AF663F; font-weight: normal; font-style: italic;}
.nx-variable    {color: #AF663F; font-weight: normal; font-style: normal;}
</style>
<pre class='nx'><span class='nx-comment'># Interface for unknown handlers:
</span><span class='nx-comment'># nsf::object::unknown::add /key/ /handler/
</span><span class='nx-comment'># nsf::object::unknown::get /key/
</span><span class='nx-comment'># nsf::object::unknown::delete /key/
</span><span class='nx-comment'># nsf::object::unknown::keys</span></pre></div></div>
<div class="ulist bibliography"><div class="title">References</div><ul>
<li>
<p>
<a id="Zdun"></a> U. Zdun, M. Strembeck, G. Neumann:
  Object-Based and Class-Based Composition of Transitive Mixins,
  Information and Software Technology, 49(8) 2007 .
</p>
</li>
<li>
<p>
<a id="Neumann and Zdun 1999a"></a> G. Neumann and U. Zdun: Filters as a
   language support for design patterns in object-oriented scripting
   languages. In Proceedings of COOTS&#8217;99, 5th Conference on
   Object-Oriented Technologies and Systems, San Diego, May 1999.
</p>
</li>
<li>
<p>
<a id="Neumann and Zdun 1999b"></a> G. Neumann and U. Zdun: Implementing
   object-specific design patterns using per-object mixins. In Proc. of
   NOSA`99, Second Nordic Workshop on Software Architecture, Ronneby,
   Sweden, August 1999.
</p>
</li>
<li>
<p>
<a id="Neumann and Zdun 1999c"></a> G. Neumann and U. Zdun: Enhancing
   object-based system composition through per-object mixins. In
   Proceedings of Asia-Pacific Software Engineering Conference (APSEC),
   Takamatsu, Japan, December 1999.
</p>
</li>
<li>
<p>
<a id="Neumann and Zdun 2000a"></a> G. Neumann and U. Zdun: XOTCL, an
   object-oriented scripting language. In Proceedings of Tcl2k: The
   7th USENIX Tcl/Tk Conference, Austin, Texas, February 2000.
</p>
</li>
<li>
<p>
<a id="Neumann and Zdun 2000b"></a> G. Neumann and U. Zdun: Towards the Usage
   of Dynamic Object Aggregations as a Form of Composition In:
   Proceedings of Symposium of Applied Computing (SAC&#8217;00), Como,
   Italy, Mar 19-21, 2000.
</p>
</li>
<li>
<p>
<a id="Neumann and Sobernig 2009"></a> G. Neumann, S. Sobernig: XOTcl 2.0 - A
   Ten-Year Retrospective and Outlook, in: Proceedings of the Sixteenth
   Annual Tcl/Tk Conference, Portland, Oregon, October, 2009.
</p>
</li>
<li>
<p>
<a id="Ousterhout 1990"></a> J. K. Ousterhout: Tcl: An embeddable command
   language. In Proc. of the 1990 Winter USENIX Conference, January 1990.
</p>
</li>
<li>
<p>
<a id="Ousterhout 1998"></a> J. K. Ousterhout: Scripting: Higher Level
   Programming for the 21st Century, IEEE Computer 31(3), March 1998.
</p>
</li>
<li>
<p>
<a id="Wetherall and Lindblad 1995"></a> D. Wetherall and C. J. Lindblad: Extending Tcl for
   Dynamic Object-Oriented Programming. Proc. of the Tcl/Tk Workshop '95,
   July 1995.
</p>
</li>
</ul></div>
</div>
</div>
</div>
</div>
</div>
<div id="footnotes"><hr /></div>
<div id="footer">
<div id="footer-text">
Version 2.2.0<br />
Last updated 2018-09-09 20:22:49 CEST
</div>
</div>
</body>
</html>