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
|
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="hevea 2.32">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<link rel="stylesheet" type="text/css" href="manual.css">
<title>Chapter 3 Objects in OCaml</title>
</head>
<body>
<a href="moduleexamples.html"><img src="previous_motif.svg" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.svg" alt="Up"></a>
<a href="lablexamples.html"><img src="next_motif.svg" alt="Next"></a>
<hr>
<h1 class="chapter" id="sec26">Chapter 3 Objects in OCaml</h1>
<ul>
<li><a href="objectexamples.html#s%3Aclasses-and-objects">3.1 Classes and objects</a>
</li><li><a href="objectexamples.html#s%3Aimmediate-objects">3.2 Immediate objects</a>
</li><li><a href="objectexamples.html#s%3Areference-to-self">3.3 Reference to self</a>
</li><li><a href="objectexamples.html#s%3Ainitializers">3.4 Initializers</a>
</li><li><a href="objectexamples.html#s%3Avirtual-methods">3.5 Virtual methods</a>
</li><li><a href="objectexamples.html#s%3Aprivate-methods">3.6 Private methods</a>
</li><li><a href="objectexamples.html#s%3Aclass-interfaces">3.7 Class interfaces</a>
</li><li><a href="objectexamples.html#s%3Ainheritance">3.8 Inheritance</a>
</li><li><a href="objectexamples.html#s%3Amultiple-inheritance">3.9 Multiple inheritance</a>
</li><li><a href="objectexamples.html#s%3Aparameterized-classes">3.10 Parameterized classes</a>
</li><li><a href="objectexamples.html#s%3Apolymorphic-methods">3.11 Polymorphic methods</a>
</li><li><a href="objectexamples.html#s%3Ausing-coercions">3.12 Using coercions</a>
</li><li><a href="objectexamples.html#s%3Afunctional-objects">3.13 Functional objects</a>
</li><li><a href="objectexamples.html#s%3Acloning-objects">3.14 Cloning objects</a>
</li><li><a href="objectexamples.html#s%3Arecursive-classes">3.15 Recursive classes</a>
</li><li><a href="objectexamples.html#s%3Abinary-methods">3.16 Binary methods</a>
</li><li><a href="objectexamples.html#s%3Afriends">3.17 Friends</a>
</li></ul>
<p>
<a id="c:objectexamples"></a>
</p><p>
<span class="c009">(Chapter written by Jérôme Vouillon, Didier Rémy and Jacques Garrigue)</span></p><p><br>
<br>
</p><p>This chapter gives an overview of the object-oriented features of
OCaml.</p><p>Note that the relationship between object, class and type in OCaml is
different than in mainstream object-oriented languages such as Java and
C++, so you shouldn’t assume that similar keywords mean the same thing.
Object-oriented features are used much less frequently in OCaml than
in those languages. OCaml has alternatives that are often more appropriate,
such as modules and functors. Indeed, many OCaml programs do not use objects
at all.</p>
<h2 class="section" id="s:classes-and-objects"><a class="section-anchor" href="#s:classes-and-objects" aria-hidden="true"></a>3.1 Classes and objects</h2>
<p>The class <span class="c003">point</span> below defines one instance variable <span class="c003">x</span> and two methods
<span class="c003">get_x</span> and <span class="c003">move</span>. The initial value of the instance variable is <span class="c003">0</span>.
The variable <span class="c003">x</span> is declared mutable, so the method <span class="c003">move</span> can change
its value.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> point =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = 0
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> point :
<span class="ocamlkeyword">object</span> <span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int <span class="ocamlkeyword">method</span> get_x : int <span class="ocamlkeyword">method</span> move : int -> unit <span class="ocamlkeyword">end</span></div></div>
</div><p>We now create a new point <span class="c003">p</span>, instance of the <span class="c003">point</span> class.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> point;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> p : point = <obj></div></div>
</div><p>
Note that the type of <span class="c003">p</span> is <span class="c003">point</span>. This is an abbreviation
automatically defined by the class definition above. It stands for the
object type <span class="c003"><get_x : int; move : int -> unit></span>, listing the methods
of class <span class="c003">point</span> along with their types.</p><p>We now invoke some methods of <span class="c003">p</span>:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> p#get_x;;</div>
<div class="pre caml-output ok">- : int = 0</div></div>
<div class="ocaml">
<div class="pre caml-input"> p#move 3;;</div>
<div class="pre caml-output ok">- : unit = ()</div></div>
<div class="ocaml">
<div class="pre caml-input"> p#get_x;;</div>
<div class="pre caml-output ok">- : int = 3</div></div>
</div><p>The evaluation of the body of a class only takes place at object
creation time. Therefore, in the following example, the instance
variable <span class="c003">x</span> is initialized to different values for two different
objects.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> x0 = <span class="ocamlkeyword">ref</span> 0;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> x0 : int <span class="ocamlkeyword">ref</span> = {contents = 0}</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> point =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = incr x0; !x0
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> point :
<span class="ocamlkeyword">object</span> <span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int <span class="ocamlkeyword">method</span> get_x : int <span class="ocamlkeyword">method</span> move : int -> unit <span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">new</span> point#get_x;;</div>
<div class="pre caml-output ok">- : int = 1</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">new</span> point#get_x;;</div>
<div class="pre caml-output ok">- : int = 2</div></div>
</div><p>The class <span class="c003">point</span> can also be abstracted over the initial values of
the <span class="c003">x</span> coordinate.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> point = <span class="ocamlkeyword">fun</span> x_init ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = x_init
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> point :
int ->
<span class="ocamlkeyword">object</span> <span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int <span class="ocamlkeyword">method</span> get_x : int <span class="ocamlkeyword">method</span> move : int -> unit <span class="ocamlkeyword">end</span></div></div>
</div><p>
Like in function definitions, the definition above can be
abbreviated as:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> point x_init =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = x_init
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> point :
int ->
<span class="ocamlkeyword">object</span> <span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int <span class="ocamlkeyword">method</span> get_x : int <span class="ocamlkeyword">method</span> move : int -> unit <span class="ocamlkeyword">end</span></div></div>
</div><p>
An instance of the class <span class="c003">point</span> is now a function that expects an
initial parameter to create a point object:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">new</span> point;;</div>
<div class="pre caml-output ok">- : int -> point = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> point 7;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> p : point = <obj></div></div>
</div><p>
The parameter <span class="c003">x_init</span> is, of course, visible in the whole body of the
definition, including methods. For instance, the method <span class="c003">get_offset</span>
in the class below returns the position of the object relative to its
initial position.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> point x_init =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = x_init
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> get_offset = x - x_init
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> point :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> get_offset : int
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
</div><p>
Expressions can be evaluated and bound before defining the object body
of the class. This is useful to enforce invariants. For instance,
points can be automatically adjusted to the nearest point on a grid,
as follows:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> adjusted_point x_init =
<span class="ocamlkeyword">let</span> origin = (x_init / 10) * 10 <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = origin
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> get_offset = x - origin
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> adjusted_point :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> get_offset : int
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
</div><p>
(One could also raise an exception if the <span class="c003">x_init</span> coordinate is not
on the grid.) In fact, the same effect could here be obtained by
calling the definition of class <span class="c003">point</span> with the value of the
<span class="c003">origin</span>.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> adjusted_point x_init = point ((x_init / 10) * 10);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> adjusted_point : int -> point</div></div>
</div><p>
An alternate solution would have been to define the adjustment in
a special allocation function:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> new_adjusted_point x_init = <span class="ocamlkeyword">new</span> point ((x_init / 10) * 10);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> new_adjusted_point : int -> point = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
However, the former pattern is generally more appropriate, since
the code for adjustment is part of the definition of the class and will be
inherited.</p><p>This ability provides class constructors as can be found in other
languages. Several constructors can be defined this way to build objects of
the same class but with different initialization patterns; an
alternative is to use initializers, as described below in
section <a href="#s%3Ainitializers">3.4</a>.</p>
<h2 class="section" id="s:immediate-objects"><a class="section-anchor" href="#s:immediate-objects" aria-hidden="true"></a>3.2 Immediate objects</h2>
<p>There is another, more direct way to create an object: create it
without going through a class.</p><p>The syntax is exactly the same as for class expressions, but the
result is a single object rather than a class. All the constructs
described in the rest of this section also apply to immediate objects.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = 0
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> p : < get_x : int; move : int -> unit > = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> p#get_x;;</div>
<div class="pre caml-output ok">- : int = 0</div></div>
<div class="ocaml">
<div class="pre caml-input"> p#move 3;;</div>
<div class="pre caml-output ok">- : unit = ()</div></div>
<div class="ocaml">
<div class="pre caml-input"> p#get_x;;</div>
<div class="pre caml-output ok">- : int = 3</div></div>
</div><p>Unlike classes, which cannot be defined inside an expression,
immediate objects can appear anywhere, using variables from their
environment.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> minmax x y =
<span class="ocamlkeyword">if</span> x < y <span class="ocamlkeyword">then</span> <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> min = x <span class="ocamlkeyword">method</span> max = y <span class="ocamlkeyword">end</span>
<span class="ocamlkeyword">else</span> <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> min = y <span class="ocamlkeyword">method</span> max = x <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> minmax : 'a -> 'a -> < max : 'a; min : 'a > = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>Immediate objects have two weaknesses compared to classes: their types
are not abbreviated, and you cannot inherit from them. But these two
weaknesses can be advantages in some situations, as we will see
in sections <a href="#s%3Areference-to-self">3.3</a> and <a href="#s%3Aparameterized-classes">3.10</a>.</p>
<h2 class="section" id="s:reference-to-self"><a class="section-anchor" href="#s:reference-to-self" aria-hidden="true"></a>3.3 Reference to self</h2>
<p>A method or an initializer can invoke methods on self (that is,
the current object). For that, self must be explicitly bound, here to
the variable <span class="c003">s</span> (<span class="c003">s</span> could be any identifier, even though we will
often choose the name <span class="c003">self</span>.)
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> printable_point x_init =
<span class="ocamlkeyword">object</span> (s)
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = x_init
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">method</span> print = print_int s#get_x
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> printable_point :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">method</span> print : unit
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> printable_point 7;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> p : printable_point = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> p#print;;</div>
<div class="pre caml-output ok">7- : unit = ()</div></div>
</div><p>
Dynamically, the variable <span class="c003">s</span> is bound at the invocation of a method. In
particular, when the class <span class="c003">printable_point</span> is inherited, the variable
<span class="c003">s</span> will be correctly bound to the object of the subclass.</p><p>A common problem with self is that, as its type may be extended in
subclasses, you cannot fix it in advance. Here is a simple example.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> ints = <span class="ocamlkeyword">ref</span> [];;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> ints : '_weak1 list <span class="ocamlkeyword">ref</span> = {contents = []}</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> my_int =
<span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">method</span> n = 1
<span class="ocamlkeyword">method</span> register = ints := <span class="ocamlhighlight">self</span> :: !ints
<span class="ocamlkeyword">end</span> ;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: This expression has type < n : int; register : 'a; .. >
but an expression was expected of type 'weak1
Self type cannot escape its class</div></div>
</div><p>
You can ignore the first two lines of the error message. What matters
is the last one: putting self into an external reference would make it
impossible to extend it through inheritance.
We will see in section <a href="#s%3Ausing-coercions">3.12</a> a workaround to this
problem.
Note however that, since immediate objects are not extensible, the
problem does not occur with them.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> my_int =
<span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">method</span> n = 1
<span class="ocamlkeyword">method</span> register = ints := self :: !ints
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> my_int : < n : int; register : unit > = <obj></div></div>
</div>
<h2 class="section" id="s:initializers"><a class="section-anchor" href="#s:initializers" aria-hidden="true"></a>3.4 Initializers</h2>
<p>Let-bindings within class definitions are evaluated before the object
is constructed. It is also possible to evaluate an expression
immediately after the object has been built. Such code is written as
an anonymous hidden method called an initializer. Therefore, it can
access self and the instance variables.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> printable_point x_init =
<span class="ocamlkeyword">let</span> origin = (x_init / 10) * 10 <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = origin
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">method</span> print = print_int self#get_x
<span class="ocamlkeyword">initializer</span> print_string <span class="ocamlstring">"new point at "</span>; self#print; print_newline ()
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> printable_point :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">method</span> print : unit
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> printable_point 17;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">new</span> point at 10
<span class="ocamlkeyword">val</span> p : printable_point = <obj></div></div>
</div><p>
Initializers cannot be overridden. On the contrary, all initializers are
evaluated sequentially.
Initializers are particularly useful to enforce invariants.
Another example can be seen in section <a href="advexamples.html#s%3Aextended-bank-accounts">6.1</a>.</p>
<h2 class="section" id="s:virtual-methods"><a class="section-anchor" href="#s:virtual-methods" aria-hidden="true"></a>3.5 Virtual methods</h2>
<p>It is possible to declare a method without actually defining it, using
the keyword <span class="c003">virtual</span>. This method will be provided later in
subclasses. A class containing virtual methods must be flagged
<span class="c003">virtual</span>, and cannot be instantiated (that is, no object of this class
can be created). It still defines type abbreviations (treating virtual methods
as other methods.)
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> <span class="ocamlkeyword">virtual</span> abstract_point x_init =
<span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">method</span> <span class="ocamlkeyword">virtual</span> get_x : int
<span class="ocamlkeyword">method</span> get_offset = self#get_x - x_init
<span class="ocamlkeyword">method</span> <span class="ocamlkeyword">virtual</span> move : int -> unit
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> <span class="ocamlkeyword">virtual</span> abstract_point :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">method</span> get_offset : int
<span class="ocamlkeyword">method</span> <span class="ocamlkeyword">virtual</span> get_x : int
<span class="ocamlkeyword">method</span> <span class="ocamlkeyword">virtual</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> point x_init =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">inherit</span> abstract_point x_init
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = x_init
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> point :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> get_offset : int
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
</div><p>Instance variables can also be declared as virtual, with the same effect
as with methods.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> <span class="ocamlkeyword">virtual</span> abstract_point2 =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> <span class="ocamlkeyword">virtual</span> x : int
<span class="ocamlkeyword">method</span> move d = x <- x + d
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> <span class="ocamlkeyword">virtual</span> abstract_point2 :
<span class="ocamlkeyword">object</span> <span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> <span class="ocamlkeyword">virtual</span> x : int <span class="ocamlkeyword">method</span> move : int -> unit <span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> point2 x_init =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">inherit</span> abstract_point2
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = x_init
<span class="ocamlkeyword">method</span> get_offset = x - x_init
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> point2 :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> get_offset : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
</div>
<h2 class="section" id="s:private-methods"><a class="section-anchor" href="#s:private-methods" aria-hidden="true"></a>3.6 Private methods</h2>
<p>Private methods are methods that do not appear in object interfaces.
They can only be invoked from other methods of the same object.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> restricted_point x_init =
<span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = x_init
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> <span class="ocamlkeyword">private</span> move d = x <- x + d
<span class="ocamlkeyword">method</span> bump = self#move 1
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> restricted_point :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> bump : unit
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> <span class="ocamlkeyword">private</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> restricted_point 0;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> p : restricted_point = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlhighlight">p</span>#move 10 ;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: This expression has type restricted_point
It has no method move</div></div>
<div class="ocaml">
<div class="pre caml-input"> p#bump;;</div>
<div class="pre caml-output ok">- : unit = ()</div></div>
</div><p>
Note that this is not the same thing as private and protected methods
in Java or C++, which can be called from other objects of the same
class. This is a direct consequence of the independence between types
and classes in OCaml: two unrelated classes may produce
objects of the same type, and there is no way at the type level to
ensure that an object comes from a specific class. However a possible
encoding of friend methods is given in section <a href="#s%3Afriends">3.17</a>.</p><p>Private methods are inherited (they are by default visible in subclasses),
unless they are hidden by signature matching, as described below.</p><p>Private methods can be made public in a subclass.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> point_again x =
<span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">inherit</span> restricted_point x
<span class="ocamlkeyword">method</span> <span class="ocamlkeyword">virtual</span> move : _
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> point_again :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> bump : unit
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
</div><p>
The annotation <span class="c003">virtual</span> here is only used to mention a method without
providing its definition. Since we didn’t add the <span class="c003">private</span>
annotation, this makes the method public, keeping the original
definition.</p><p>An alternative definition is
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> point_again x =
<span class="ocamlkeyword">object</span> (self : < move : _; ..> )
<span class="ocamlkeyword">inherit</span> restricted_point x
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> point_again :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> bump : unit
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
</div><p>
The constraint on self’s type is requiring a public <span class="c003">move</span> method, and
this is sufficient to override <span class="c003">private</span>.</p><p>One could think that a private method should remain private in a subclass.
However, since the method is visible in a subclass, it is always possible
to pick its code and define a method of the same name that runs that
code, so yet another (heavier) solution would be:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> point_again x =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">inherit</span> restricted_point x <span class="ocamlkeyword">as</span> super
<span class="ocamlkeyword">method</span> move = super#move
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> point_again :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> bump : unit
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
</div><p>Of course, private methods can also be virtual. Then, the keywords must
appear in this order <span class="c003">method private virtual</span>.</p>
<h2 class="section" id="s:class-interfaces"><a class="section-anchor" href="#s:class-interfaces" aria-hidden="true"></a>3.7 Class interfaces</h2>
<p>Class interfaces are inferred from class definitions. They may also
be defined directly and used to restrict the type of a class. Like class
declarations, they also define a new type abbreviation.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> restricted_point_type =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> bump : unit
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> restricted_point_type =
<span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> bump : unit <span class="ocamlkeyword">method</span> get_x : int <span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">fun</span> (x : restricted_point_type) -> x;;</div>
<div class="pre caml-output ok">- : restricted_point_type -> restricted_point_type = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
In addition to program documentation, class interfaces can be used to
constrain the type of a class. Both concrete instance variables and concrete
private methods can be hidden by a class type constraint. Public
methods and virtual members, however, cannot.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> restricted_point' x = (restricted_point x : restricted_point_type);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> restricted_point' : int -> restricted_point_type</div></div>
</div><p>
Or, equivalently:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> restricted_point' = (restricted_point : int -> restricted_point_type);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> restricted_point' : int -> restricted_point_type</div></div>
</div><p>
The interface of a class can also be specified in a module
signature, and used to restrict the inferred signature of a module.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">module</span> <span class="ocamlkeyword">type</span> POINT = <span class="ocamlkeyword">sig</span>
<span class="ocamlkeyword">class</span> restricted_point' : int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> bump : unit
<span class="ocamlkeyword">end</span>
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">module</span> <span class="ocamlkeyword">type</span> POINT =
<span class="ocamlkeyword">sig</span>
<span class="ocamlkeyword">class</span> restricted_point' :
int -> <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> bump : unit <span class="ocamlkeyword">method</span> get_x : int <span class="ocamlkeyword">end</span>
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">module</span> Point : POINT = <span class="ocamlkeyword">struct</span>
<span class="ocamlkeyword">class</span> restricted_point' = restricted_point
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">module</span> Point : POINT</div></div>
</div>
<h2 class="section" id="s:inheritance"><a class="section-anchor" href="#s:inheritance" aria-hidden="true"></a>3.8 Inheritance</h2>
<p>We illustrate inheritance by defining a class of colored points that
inherits from the class of points. This class has all instance
variables and all methods of class <span class="c003">point</span>, plus a new instance
variable <span class="c003">c</span> and a new method <span class="c003">color</span>.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> colored_point x (c : string) =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">inherit</span> point x
<span class="ocamlkeyword">val</span> c = c
<span class="ocamlkeyword">method</span> color = c
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> colored_point :
int ->
string ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> c : string
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> color : string
<span class="ocamlkeyword">method</span> get_offset : int
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p' = <span class="ocamlkeyword">new</span> colored_point 5 <span class="ocamlstring">"red"</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> p' : colored_point = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> p'#get_x, p'#color;;</div>
<div class="pre caml-output ok">- : int * string = (5, <span class="ocamlstring">"red"</span>)</div></div>
</div><p>
A point and a colored point have incompatible types, since a point has
no method <span class="c003">color</span>. However, the function <span class="c003">get_x</span> below is a generic
function applying method <span class="c003">get_x</span> to any object <span class="c003">p</span> that has this
method (and possibly some others, which are represented by an ellipsis
in the type). Thus, it applies to both points and colored points.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> get_succ_x p = p#get_x + 1;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> get_succ_x : < get_x : int; .. > -> int = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> get_succ_x p + get_succ_x p';;</div>
<div class="pre caml-output ok">- : int = 8</div></div>
</div><p>
Methods need not be declared previously, as shown by the example:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> set_x p = p#set_x;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> set_x : < set_x : 'a; .. > -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> incr p = set_x p (get_succ_x p);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> incr : < get_x : int; set_x : int -> 'a; .. > -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
</div>
<h2 class="section" id="s:multiple-inheritance"><a class="section-anchor" href="#s:multiple-inheritance" aria-hidden="true"></a>3.9 Multiple inheritance</h2>
<p>Multiple inheritance is allowed. Only the last definition of a method
is kept: the redefinition in a subclass of a method that was visible in
the parent class overrides the definition in the parent class.
Previous definitions of a method can be reused by binding the related
ancestor. Below, <span class="c003">super</span> is bound to the ancestor <span class="c003">printable_point</span>.
The name <span class="c003">super</span> is a pseudo value identifier that can only be used to
invoke a super-class method, as in <span class="c003">super#print</span>.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> printable_colored_point y c =
<span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">val</span> c = c
<span class="ocamlkeyword">method</span> color = c
<span class="ocamlkeyword">inherit</span> printable_point y <span class="ocamlkeyword">as</span> super
<span class="ocamlkeyword">method</span>! print =
print_string <span class="ocamlstring">"("</span>;
super#print;
print_string <span class="ocamlstring">", "</span>;
print_string (self#color);
print_string <span class="ocamlstring">")"</span>
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> printable_colored_point :
int ->
string ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> c : string
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> color : string
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">method</span> print : unit
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p' = <span class="ocamlkeyword">new</span> printable_colored_point 17 <span class="ocamlstring">"red"</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">new</span> point at (10, red)
<span class="ocamlkeyword">val</span> p' : printable_colored_point = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> p'#print;;</div>
<div class="pre caml-output ok">(10, red)- : unit = ()</div></div>
</div><p>
A private method that has been hidden in the parent class is no longer
visible, and is thus not overridden. Since initializers are treated as
private methods, all initializers along the class hierarchy are evaluated,
in the order they are introduced.</p><p>Note that for clarity’s sake, the method <span class="c003">print</span> is explicitly marked as
overriding another definition by annotating the <span class="c003">method</span> keyword with
an exclamation mark <span class="c003">!</span>. If the method <span class="c003">print</span> were not overriding the
<span class="c003">print</span> method of <span class="c003">printable_point</span>, the compiler would raise an error:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">object</span>
<span class="ocamlhighlight">method! m = ()</span>
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: The method `m' has no previous definition</div></div>
</div><p>This explicit overriding annotation also works
for <span class="c003">val</span> and <span class="c003">inherit</span>:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> another_printable_colored_point y c c' =
<span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">inherit</span> printable_point y
<span class="ocamlkeyword">inherit</span>! printable_colored_point y c
<span class="ocamlkeyword">val</span>! c = c'
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> another_printable_colored_point :
int ->
string ->
string ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> c : string
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> color : string
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">method</span> print : unit
<span class="ocamlkeyword">end</span></div></div>
</div>
<h2 class="section" id="s:parameterized-classes"><a class="section-anchor" href="#s:parameterized-classes" aria-hidden="true"></a>3.10 Parameterized classes</h2>
<p>Reference cells can be implemented as objects.
The naive definition fails to typecheck:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlhighlight">class oref x_init =
object
val mutable x = x_init
method get = x
method set y = x <- y
end</span>;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: Some type variables are unbound in this type:
class oref :
'a ->
object
val mutable x : 'a
method get : 'a
method set : 'a -> unit
end
The method get has type 'a where 'a is unbound</div></div>
</div><p>
The reason is that at least one of the methods has a polymorphic type
(here, the type of the value stored in the reference cell), thus
either the class should be parametric, or the method type should be
constrained to a monomorphic type. A monomorphic instance of the class could
be defined by:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> oref (x_init:int) =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = x_init
<span class="ocamlkeyword">method</span> get = x
<span class="ocamlkeyword">method</span> set y = x <- y
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> oref :
int ->
<span class="ocamlkeyword">object</span> <span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int <span class="ocamlkeyword">method</span> get : int <span class="ocamlkeyword">method</span> set : int -> unit <span class="ocamlkeyword">end</span></div></div>
</div><p>
Note that since immediate objects do not define a class type, they have
no such restriction.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> new_oref x_init =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = x_init
<span class="ocamlkeyword">method</span> get = x
<span class="ocamlkeyword">method</span> set y = x <- y
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> new_oref : 'a -> < get : 'a; set : 'a -> unit > = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
On the other hand, a class for polymorphic references must explicitly
list the type parameters in its declaration. Class type parameters are
listed between <span class="c003">[</span> and <span class="c003">]</span>. The type parameters must also be
bound somewhere in the class body by a type constraint.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> ['a] oref x_init =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = (x_init : 'a)
<span class="ocamlkeyword">method</span> get = x
<span class="ocamlkeyword">method</span> set y = x <- y
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> ['a] oref :
'a -> <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : 'a <span class="ocamlkeyword">method</span> get : 'a <span class="ocamlkeyword">method</span> set : 'a -> unit <span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> r = <span class="ocamlkeyword">new</span> oref 1 <span class="ocamlkeyword">in</span> r#set 2; (r#get);;</div>
<div class="pre caml-output ok">- : int = 2</div></div>
</div><p>
The type parameter in the declaration may actually be constrained in the
body of the class definition. In the class type, the actual value of
the type parameter is displayed in the <span class="c003">constraint</span> clause.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> ['a] oref_succ (x_init:'a) =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x = x_init + 1
<span class="ocamlkeyword">method</span> get = x
<span class="ocamlkeyword">method</span> set y = x <- y
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> ['a] oref_succ :
'a ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">constraint</span> 'a = int
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> get : int
<span class="ocamlkeyword">method</span> set : int -> unit
<span class="ocamlkeyword">end</span></div></div>
</div><p>
Let us consider a more complex example: define a circle, whose center
may be any kind of point. We put an additional type
constraint in method <span class="c003">move</span>, since no free variables must remain
unaccounted for by the class type parameters.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> ['a] circle (c : 'a) =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> center = c
<span class="ocamlkeyword">method</span> center = center
<span class="ocamlkeyword">method</span> set_center c = center <- c
<span class="ocamlkeyword">method</span> move = (center#move : int -> unit)
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> ['a] circle :
'a ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">constraint</span> 'a = < move : int -> unit; .. >
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> center : 'a
<span class="ocamlkeyword">method</span> center : 'a
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">method</span> set_center : 'a -> unit
<span class="ocamlkeyword">end</span></div></div>
</div><p>
An alternate definition of <span class="c003">circle</span>, using a <span class="c003">constraint</span> clause in
the class definition, is shown below. The type <span class="c003">#point</span> used below in
the <span class="c003">constraint</span> clause is an abbreviation produced by the definition
of class <span class="c003">point</span>. This abbreviation unifies with the type of any
object belonging to a subclass of class <span class="c003">point</span>. It actually expands to
<span class="c003">< get_x : int; move : int -> unit; .. ></span>. This leads to the following
alternate definition of <span class="c003">circle</span>, which has slightly stronger
constraints on its argument, as we now expect <span class="c003">center</span> to have a
method <span class="c003">get_x</span>.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> ['a] circle (c : 'a) =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">constraint</span> 'a = #point
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> center = c
<span class="ocamlkeyword">method</span> center = center
<span class="ocamlkeyword">method</span> set_center c = center <- c
<span class="ocamlkeyword">method</span> move = center#move
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> ['a] circle :
'a ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">constraint</span> 'a = #point
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> center : 'a
<span class="ocamlkeyword">method</span> center : 'a
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">method</span> set_center : 'a -> unit
<span class="ocamlkeyword">end</span></div></div>
</div><p>
The class <span class="c003">colored_circle</span> is a specialized version of class
<span class="c003">circle</span> that requires the type of the center to unify with
<span class="c003">#colored_point</span>, and adds a method <span class="c003">color</span>. Note that when specializing a
parameterized class, the instance of type parameter must always be
explicitly given. It is again written between <span class="c003">[</span> and <span class="c003">]</span>.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> ['a] colored_circle c =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">constraint</span> 'a = #colored_point
<span class="ocamlkeyword">inherit</span> ['a] circle c
<span class="ocamlkeyword">method</span> color = center#color
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> ['a] colored_circle :
'a ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">constraint</span> 'a = #colored_point
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> center : 'a
<span class="ocamlkeyword">method</span> center : 'a
<span class="ocamlkeyword">method</span> color : string
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">method</span> set_center : 'a -> unit
<span class="ocamlkeyword">end</span></div></div>
</div>
<h2 class="section" id="s:polymorphic-methods"><a class="section-anchor" href="#s:polymorphic-methods" aria-hidden="true"></a>3.11 Polymorphic methods</h2>
<p>While parameterized classes may be polymorphic in their contents, they
are not enough to allow polymorphism of method use.</p><p>A classical example is defining an iterator.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> List.fold_left;;</div>
<div class="pre caml-output ok">- : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> ['a] intlist (l : int list) =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">method</span> empty = (l = [])
<span class="ocamlkeyword">method</span> fold f (accu : 'a) = List.fold_left f accu l
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> ['a] intlist :
int list ->
<span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> empty : bool <span class="ocamlkeyword">method</span> fold : ('a -> int -> 'a) -> 'a -> 'a <span class="ocamlkeyword">end</span></div></div>
</div><p>
At first look, we seem to have a polymorphic iterator, however this
does not work in practice.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> l = <span class="ocamlkeyword">new</span> intlist [1; 2; 3];;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> l : '_weak2 intlist = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> l#fold (<span class="ocamlkeyword">fun</span> x y -> x+y) 0;;</div>
<div class="pre caml-output ok">- : int = 6</div></div>
<div class="ocaml">
<div class="pre caml-input"> l;;</div>
<div class="pre caml-output ok">- : int intlist = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> l#fold (<span class="ocamlkeyword">fun</span> s x -> <span class="ocamlhighlight">s</span> ^ Int.to_string x ^ <span class="ocamlstring">" "</span>) <span class="ocamlstring">""</span> ;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: This expression has type int but an expression was expected of type
string</div></div>
</div><p>
Our iterator works, as shows its first use for summation. However,
since objects themselves are not polymorphic (only their constructors
are), using the <span class="c003">fold</span> method fixes its type for this individual object.
Our next attempt to use it as a string iterator fails.</p><p>The problem here is that quantification was wrongly located: it is
not the class we want to be polymorphic, but the <span class="c003">fold</span> method.
This can be achieved by giving an explicitly polymorphic type in the
method definition.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> intlist (l : int list) =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">method</span> empty = (l = [])
<span class="ocamlkeyword">method</span> fold : 'a. ('a -> int -> 'a) -> 'a -> 'a =
<span class="ocamlkeyword">fun</span> f accu -> List.fold_left f accu l
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> intlist :
int list ->
<span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> empty : bool <span class="ocamlkeyword">method</span> fold : ('a -> int -> 'a) -> 'a -> 'a <span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> l = <span class="ocamlkeyword">new</span> intlist [1; 2; 3];;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> l : intlist = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> l#fold (<span class="ocamlkeyword">fun</span> x y -> x+y) 0;;</div>
<div class="pre caml-output ok">- : int = 6</div></div>
<div class="ocaml">
<div class="pre caml-input"> l#fold (<span class="ocamlkeyword">fun</span> s x -> s ^ Int.to_string x ^ <span class="ocamlstring">" "</span>) <span class="ocamlstring">""</span>;;</div>
<div class="pre caml-output ok">- : string = <span class="ocamlstring">"1 2 3 "</span></div></div>
</div><p>
As you can see in the class type shown by the compiler, while
polymorphic method types must be fully explicit in class definitions
(appearing immediately after the method name), quantified type
variables can be left implicit in class descriptions. Why require types
to be explicit? The problem is that <span class="c003">(int -> int -> int) -> int -> int</span> would also be a valid type for <span class="c003">fold</span>, and it happens to be
incompatible with the polymorphic type we gave (automatic
instantiation only works for toplevel types variables, not for inner
quantifiers, where it becomes an undecidable problem.) So the compiler
cannot choose between those two types, and must be helped.</p><p>However, the type can be completely omitted in the class definition if
it is already known, through inheritance or type constraints on self.
Here is an example of method overriding.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> intlist_rev l =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">inherit</span> intlist l
<span class="ocamlkeyword">method</span>! fold f accu = List.fold_left f accu (List.rev l)
<span class="ocamlkeyword">end</span>;;</div></div>
</div><p>
The following idiom separates description and definition.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> ['a] iterator =
<span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> fold : ('b -> 'a -> 'b) -> 'b -> 'b <span class="ocamlkeyword">end</span>;;</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> intlist' l =
<span class="ocamlkeyword">object</span> (self : int #iterator)
<span class="ocamlkeyword">method</span> empty = (l = [])
<span class="ocamlkeyword">method</span> fold f accu = List.fold_left f accu l
<span class="ocamlkeyword">end</span>;;</div></div>
</div><p>
Note here the <span class="c003">(self : int #iterator)</span> idiom, which ensures that this
object implements the interface <span class="c003">iterator</span>.</p><p>Polymorphic methods are called in exactly the same way as normal
methods, but you should be aware of some limitations of type
inference. Namely, a polymorphic method can only be called if its
type is known at the call site. Otherwise, the method will be assumed
to be monomorphic, and given an incompatible type.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> sum lst = lst#fold (<span class="ocamlkeyword">fun</span> x y -> x+y) 0;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> sum : < fold : (int -> int -> int) -> int -> 'a; .. > -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> sum <span class="ocamlhighlight">l</span> ;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: This expression has type intlist
but an expression was expected of type
< fold : (int -> int -> int) -> int -> 'a; .. >
Types for method fold are incompatible</div></div>
</div><p>
The workaround is easy: you should put a type constraint on the
parameter.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> sum (lst : _ #iterator) = lst#fold (<span class="ocamlkeyword">fun</span> x y -> x+y) 0;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> sum : int #iterator -> int = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
Of course the constraint may also be an explicit method type.
Only occurrences of quantified variables are required.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> sum lst =
(lst : < fold : 'a. ('a -> _ -> 'a) -> 'a -> 'a; .. >)#fold (+) 0;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> sum : < fold : 'a. ('a -> int -> 'a) -> 'a -> 'a; .. > -> int = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>Another use of polymorphic methods is to allow some form of implicit
subtyping in method arguments. We have already seen in
section <a href="#s%3Ainheritance">3.8</a> how some functions may be polymorphic in the
class of their argument. This can be extended to methods.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> point0 = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> get_x : int <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> point0 = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> get_x : int <span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> distance_point x =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">inherit</span> point x
<span class="ocamlkeyword">method</span> distance : 'a. (#point0 <span class="ocamlkeyword">as</span> 'a) -> int =
<span class="ocamlkeyword">fun</span> other -> abs (other#get_x - x)
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> distance_point :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : int
<span class="ocamlkeyword">method</span> distance : #point0 -> int
<span class="ocamlkeyword">method</span> get_offset : int
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> unit
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> distance_point 3 <span class="ocamlkeyword">in</span>
(p#distance (<span class="ocamlkeyword">new</span> point 8), p#distance (<span class="ocamlkeyword">new</span> colored_point 1 <span class="ocamlstring">"blue"</span>));;</div>
<div class="pre caml-output ok">- : int * int = (5, 2)</div></div>
</div><p>
Note here the special syntax <span class="c003">(#point0 as 'a)</span> we have to use to
quantify the extensible part of <span class="c003">#point0</span>. As for the variable binder,
it can be omitted in class specifications. If you want polymorphism
inside object field it must be quantified independently.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> multi_poly =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">method</span> m1 : 'a. (< n1 : 'b. 'b -> 'b; .. > <span class="ocamlkeyword">as</span> 'a) -> _ =
<span class="ocamlkeyword">fun</span> o -> o#n1 <span class="ocamlkeyword">true</span>, o#n1 <span class="ocamlstring">"hello"</span>
<span class="ocamlkeyword">method</span> m2 : 'a 'b. (< n2 : 'b -> bool; .. > <span class="ocamlkeyword">as</span> 'a) -> 'b -> _ =
<span class="ocamlkeyword">fun</span> o x -> o#n2 x
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> multi_poly :
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">method</span> m1 : < n1 : 'b. 'b -> 'b; .. > -> bool * string
<span class="ocamlkeyword">method</span> m2 : < n2 : 'b -> bool; .. > -> 'b -> bool
<span class="ocamlkeyword">end</span></div></div>
</div><p>
In method <span class="c003">m1</span>, <span class="c003">o</span> must be an object with at least a method <span class="c003">n1</span>,
itself polymorphic. In method <span class="c003">m2</span>, the argument of <span class="c003">n2</span> and <span class="c003">x</span> must
have the same type, which is quantified at the same level as <span class="c003">'a</span>.</p>
<h2 class="section" id="s:using-coercions"><a class="section-anchor" href="#s:using-coercions" aria-hidden="true"></a>3.12 Using coercions</h2>
<p>Subtyping is never implicit. There are, however, two ways to perform
subtyping. The most general construction is fully explicit: both the
domain and the codomain of the type coercion must be given.</p><p>We have seen that points and colored points have incompatible types.
For instance, they cannot be mixed in the same list. However, a
colored point can be coerced to a point, hiding its <span class="c003">color</span> method:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> colored_point_to_point cp = (cp : colored_point :> point);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> colored_point_to_point : colored_point -> point = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> point 3 <span class="ocamlkeyword">and</span> q = <span class="ocamlkeyword">new</span> colored_point 4 <span class="ocamlstring">"blue"</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> p : point = <obj>
<span class="ocamlkeyword">val</span> q : colored_point = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> l = [p; (colored_point_to_point q)];;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> l : point list = [<obj>; <obj>]</div></div>
</div><p>
An object of type <span class="c003">t</span> can be seen as an object of type <span class="c003">t'</span>
only if <span class="c003">t</span> is a subtype of <span class="c003">t'</span>. For instance, a point cannot be
seen as a colored point.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlhighlight">(p : point :> colored_point)</span>;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: Type point = < get_offset : int; get_x : int; move : int -> unit >
is not a subtype of
colored_point =
< color : string; get_offset : int; get_x : int;
move : int -> unit >
The first object type has no method color</div></div>
</div><p>
Indeed, narrowing coercions without runtime checks would be unsafe.
Runtime type checks might raise exceptions, and they would require
the presence of type information at runtime, which is not the case in
the OCaml system.
For these reasons, there is no such operation available in the language.</p><p>Be aware that subtyping and inheritance are not related. Inheritance is a
syntactic relation between classes while subtyping is a semantic relation
between types. For instance, the class of colored points could have been
defined directly, without inheriting from the class of points; the type of
colored points would remain unchanged and thus still be a subtype of
points.
</p><p>The domain of a coercion can often be omitted. For instance, one can
define:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> to_point cp = (cp :> point);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> to_point : #point -> point = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
In this case, the function <span class="c003">colored_point_to_point</span> is an instance of the
function <span class="c003">to_point</span>. This is not always true, however. The fully
explicit coercion is more precise and is sometimes unavoidable.
Consider, for example, the following class:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> c0 = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> m = {< >} <span class="ocamlkeyword">method</span> n = 0 <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> c0 : <span class="ocamlkeyword">object</span> ('a) <span class="ocamlkeyword">method</span> m : 'a <span class="ocamlkeyword">method</span> n : int <span class="ocamlkeyword">end</span></div></div>
</div><p>
The object type <span class="c003">c0</span> is an abbreviation for <span class="c003"><m : 'a; n : int> as 'a</span>.
Consider now the type declaration:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> c1 = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> m : c1 <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> c1 = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> m : c1 <span class="ocamlkeyword">end</span></div></div>
</div><p>
The object type <span class="c003">c1</span> is an abbreviation for the type <span class="c003"><m : 'a> as 'a</span>.
The coercion from an object of type <span class="c003">c0</span> to an object of type <span class="c003">c1</span> is
correct:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">fun</span> (x:c0) -> (x : c0 :> c1);;</div>
<div class="pre caml-output ok">- : c0 -> c1 = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
However, the domain of the coercion cannot always be omitted.
In that case, the solution is to use the explicit form.
Sometimes, a change in the class-type definition can also solve the problem
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> c2 = <span class="ocamlkeyword">object</span> ('a) <span class="ocamlkeyword">method</span> m : 'a <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> c2 = <span class="ocamlkeyword">object</span> ('a) <span class="ocamlkeyword">method</span> m : 'a <span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">fun</span> (x:c0) -> (x :> c2);;</div>
<div class="pre caml-output ok">- : c0 -> c2 = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
While class types <span class="c003">c1</span> and <span class="c003">c2</span> are different, both object types
<span class="c003">c1</span> and <span class="c003">c2</span> expand to the same object type (same method names and types).
Yet, when the domain of a coercion is left implicit and its co-domain
is an abbreviation of a known class type, then the class type, rather
than the object type, is used to derive the coercion function. This
allows leaving the domain implicit in most cases when coercing form a
subclass to its superclass.
The type of a coercion can always be seen as below:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> to_c1 x = (x :> c1);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> to_c1 : < m : #c1; .. > -> c1 = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> to_c2 x = (x :> c2);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> to_c2 : #c2 -> c2 = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
Note the difference between these two coercions: in the case of <span class="c003">to_c2</span>,
the type
<span class="c003">#c2 = < m : 'a; .. > as 'a</span> is polymorphically recursive (according
to the explicit recursion in the class type of <span class="c003">c2</span>); hence the
success of applying this coercion to an object of class <span class="c003">c0</span>.
On the other hand, in the first case, <span class="c003">c1</span> was only expanded and
unrolled twice to obtain <span class="c003">< m : < m : c1; .. >; .. ></span> (remember <span class="c003">#c1 = < m : c1; .. ></span>), without introducing recursion.
You may also note that the type of <span class="c003">to_c2</span> is <span class="c003">#c2 -> c2</span> while
the type of <span class="c003">to_c1</span> is more general than <span class="c003">#c1 -> c1</span>. This is not always true,
since there are class types for which some instances of <span class="c003">#c</span> are not subtypes
of <span class="c003">c</span>, as explained in section <a href="#s%3Abinary-methods">3.16</a>. Yet, for
parameterless classes the coercion <span class="c003">(_ :> c)</span> is always more general than
<span class="c003">(_ : #c :> c)</span>.
</p><p>A common problem may occur when one tries to define a coercion to a
class <span class="c003">c</span> while defining class <span class="c003">c</span>. The problem is due to the type
abbreviation not being completely defined yet, and so its subtypes are not
clearly known. Then, a coercion <span class="c003">(_ :> c)</span> or <span class="c003">(_ : #c :> c)</span> is taken to be
the identity function, as in
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">function</span> x -> (x :> 'a);;</div>
<div class="pre caml-output ok">- : 'a -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
As a consequence, if the coercion is applied to <span class="c003">self</span>, as in the
following example, the type of <span class="c003">self</span> is unified with the closed type
<span class="c003">c</span> (a closed object type is an object type without ellipsis). This
would constrain the type of self be closed and is thus rejected.
Indeed, the type of self cannot be closed: this would prevent any
further extension of the class. Therefore, a type error is generated
when the unification of this type with another type would result in a
closed object type.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> c = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> m = 1 <span class="ocamlkeyword">end</span>
<span class="ocamlkeyword">and</span> d = <span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">inherit</span> c
<span class="ocamlkeyword">method</span> n = 2
<span class="ocamlkeyword">method</span> as_c = (<span class="ocamlhighlight">self</span> :> c)
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: This expression cannot be coerced to type c = < m : int >; it has type
< as_c : c; m : int; n : int; .. >
but is here used with type c
Self type cannot escape its class</div></div>
</div><p>
However, the most common instance of this problem, coercing self to
its current class, is detected as a special case by the type checker,
and properly typed.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> c = <span class="ocamlkeyword">object</span> (self) <span class="ocamlkeyword">method</span> m = (self :> c) <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> c : <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> m : c <span class="ocamlkeyword">end</span></div></div>
</div><p>
This allows the following idiom, keeping a list of all objects
belonging to a class or its subclasses:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> all_c = <span class="ocamlkeyword">ref</span> [];;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> all_c : '_weak3 list <span class="ocamlkeyword">ref</span> = {contents = []}</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> c (m : int) =
<span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">method</span> m = m
<span class="ocamlkeyword">initializer</span> all_c := (self :> c) :: !all_c
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> c : int -> <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> m : int <span class="ocamlkeyword">end</span></div></div>
</div><p>
This idiom can in turn be used to retrieve an object whose type has
been weakened:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> lookup_obj obj = <span class="ocamlkeyword">function</span> [] -> raise Not_found
| obj' :: l ->
<span class="ocamlkeyword">if</span> (obj :> < >) = (obj' :> < >) <span class="ocamlkeyword">then</span> obj' <span class="ocamlkeyword">else</span> lookup_obj obj l ;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> lookup_obj : < .. > -> (< .. > <span class="ocamlkeyword">as</span> 'a) list -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> lookup_c obj = lookup_obj obj !all_c;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> lookup_c : < .. > -> < m : int > = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
The type <span class="c003">< m : int ></span> we see here is just the expansion of <span class="c003">c</span>, due
to the use of a reference; we have succeeded in getting back an object
of type <span class="c003">c</span>.</p><p><br>
The previous coercion problem can often be avoided by first
defining the abbreviation, using a class type:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> c' = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> m : int <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> <span class="ocamlkeyword">type</span> c' = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> m : int <span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> c : c' = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> m = 1 <span class="ocamlkeyword">end</span>
<span class="ocamlkeyword">and</span> d = <span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">inherit</span> c
<span class="ocamlkeyword">method</span> n = 2
<span class="ocamlkeyword">method</span> as_c = (self :> c')
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> c : c'
<span class="ocamlkeyword">and</span> d : <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> as_c : c' <span class="ocamlkeyword">method</span> m : int <span class="ocamlkeyword">method</span> n : int <span class="ocamlkeyword">end</span></div></div>
</div><p>
It is also possible to use a virtual class. Inheriting from this class
simultaneously forces all methods of <span class="c003">c</span> to have the same
type as the methods of <span class="c003">c'</span>.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> <span class="ocamlkeyword">virtual</span> c' = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> <span class="ocamlkeyword">virtual</span> m : int <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> <span class="ocamlkeyword">virtual</span> c' : <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> <span class="ocamlkeyword">virtual</span> m : int <span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> c = <span class="ocamlkeyword">object</span> (self) <span class="ocamlkeyword">inherit</span> c' <span class="ocamlkeyword">method</span> m = 1 <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> c : <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">method</span> m : int <span class="ocamlkeyword">end</span></div></div>
</div><p>
One could think of defining the type abbreviation directly:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> c' = <m : int>;;</div></div>
</div><p>
However, the abbreviation <span class="c003">#c'</span> cannot be defined directly in a similar way.
It can only be defined by a class or a class-type definition.
This is because a <span class="c003">#</span>-abbreviation carries an implicit anonymous
variable <span class="c003">..</span> that cannot be explicitly named.
The closer you get to it is:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> 'a c'_class = 'a <span class="ocamlkeyword">constraint</span> 'a = < m : int; .. >;;</div></div>
</div><p>
with an extra type variable capturing the open object type.</p>
<h2 class="section" id="s:functional-objects"><a class="section-anchor" href="#s:functional-objects" aria-hidden="true"></a>3.13 Functional objects</h2>
<p>It is possible to write a version of class <span class="c003">point</span> without assignments
on the instance variables. The override construct <span class="c003">{< ... >}</span> returns a copy of
“self” (that is, the current object), possibly changing the value of
some instance variables.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> functional_point y =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> x = y
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> move d = {< x = x + d >}
<span class="ocamlkeyword">method</span> move_to x = {< x >}
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> functional_point :
int ->
<span class="ocamlkeyword">object</span> ('a)
<span class="ocamlkeyword">val</span> x : int
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> 'a
<span class="ocamlkeyword">method</span> move_to : int -> 'a
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> functional_point 7;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> p : functional_point = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> p#get_x;;</div>
<div class="pre caml-output ok">- : int = 7</div></div>
<div class="ocaml">
<div class="pre caml-input"> (p#move 3)#get_x;;</div>
<div class="pre caml-output ok">- : int = 10</div></div>
<div class="ocaml">
<div class="pre caml-input"> (p#move_to 15)#get_x;;</div>
<div class="pre caml-output ok">- : int = 15</div></div>
<div class="ocaml">
<div class="pre caml-input"> p#get_x;;</div>
<div class="pre caml-output ok">- : int = 7</div></div>
</div><p>
As with records, the form <span class="c003">{< x >}</span> is an elided version of
<span class="c003">{< x = x >}</span> which avoids the repetition of the instance variable name.
Note that the type abbreviation <span class="c003">functional_point</span> is recursive, which can
be seen in the class type of <span class="c003">functional_point</span>: the type of self is <span class="c003">'a</span>
and <span class="c003">'a</span> appears inside the type of the method <span class="c003">move</span>.</p><p>The above definition of <span class="c003">functional_point</span> is not equivalent
to the following:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> bad_functional_point y =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> x = y
<span class="ocamlkeyword">method</span> get_x = x
<span class="ocamlkeyword">method</span> move d = <span class="ocamlkeyword">new</span> bad_functional_point (x+d)
<span class="ocamlkeyword">method</span> move_to x = <span class="ocamlkeyword">new</span> bad_functional_point x
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> bad_functional_point :
int ->
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> x : int
<span class="ocamlkeyword">method</span> get_x : int
<span class="ocamlkeyword">method</span> move : int -> bad_functional_point
<span class="ocamlkeyword">method</span> move_to : int -> bad_functional_point
<span class="ocamlkeyword">end</span></div></div>
</div><p>
While objects of either class will behave the same, objects of their
subclasses will be different. In a subclass of <span class="c003">bad_functional_point</span>,
the method <span class="c003">move</span> will
keep returning an object of the parent class. On the contrary, in a
subclass of <span class="c003">functional_point</span>, the method <span class="c003">move</span> will return an
object of the subclass.</p><p>Functional update is often used in conjunction with binary methods
as illustrated in section <a href="advexamples.html#ss%3Astring-as-class">6.2.1</a>.</p>
<h2 class="section" id="s:cloning-objects"><a class="section-anchor" href="#s:cloning-objects" aria-hidden="true"></a>3.14 Cloning objects</h2>
<p>Objects can also be cloned, whether they are functional or imperative.
The library function <span class="c003">Oo.copy</span> makes a shallow copy of an object. That is,
it returns a new object that has the same methods and instance
variables as its argument. The
instance variables are copied but their contents are shared.
Assigning a new value to an instance variable of the copy (using a method
call) will not affect instance variables of the original, and conversely.
A deeper assignment (for example if the instance variable is a reference cell)
will of course affect both the original and the copy.</p><p>The type of <span class="c003">Oo.copy</span> is the following:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> Oo.copy;;</div>
<div class="pre caml-output ok">- : (< .. > <span class="ocamlkeyword">as</span> 'a) -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
The keyword <span class="c003">as</span> in that type binds the type variable <span class="c003">'a</span> to
the object type <span class="c003">< .. ></span>. Therefore, <span class="c003">Oo.copy</span> takes an object with
any methods (represented by the ellipsis), and returns an object of
the same type. The type of <span class="c003">Oo.copy</span> is different from type <span class="c003">< .. > -> < .. ></span> as each ellipsis represents a different set of methods.
Ellipsis actually behaves as a type variable.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> point 5;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> p : point = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> q = Oo.copy p;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> q : point = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> q#move 7; (p#get_x, q#get_x);;</div>
<div class="pre caml-output ok">- : int * int = (5, 12)</div></div>
</div><p>
In fact, <span class="c003">Oo.copy p</span> will behave as <span class="c003">p#copy</span> assuming that a public
method <span class="c003">copy</span> with body <span class="c003">{< >}</span> has been defined in the class of <span class="c003">p</span>.</p><p>Objects can be compared using the generic comparison functions <span class="c003">=</span> and <span class="c003"><></span>.
Two objects are equal if and only if they are physically equal. In
particular, an object and its copy are not equal.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> q = Oo.copy p;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> q : point = <obj></div></div>
<div class="ocaml">
<div class="pre caml-input"> p = q, p = p;;</div>
<div class="pre caml-output ok">- : bool * bool = (<span class="ocamlkeyword">false</span>, <span class="ocamlkeyword">true</span>)</div></div>
</div><p>
Other generic comparisons such as (<span class="c003"><</span>, <span class="c003"><=</span>, ...) can also be used on
objects. The
relation <span class="c003"><</span> defines an unspecified but strict ordering on objects. The
ordering relationship between two objects is fixed once for all after the
two objects have been created and it is not affected by mutation of fields.</p><p>Cloning and override have a non empty intersection.
They are interchangeable when used within an object and without
overriding any field:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> copy =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">method</span> copy = {< >}
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> copy : <span class="ocamlkeyword">object</span> ('a) <span class="ocamlkeyword">method</span> copy : 'a <span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> copy =
<span class="ocamlkeyword">object</span> (self)
<span class="ocamlkeyword">method</span> copy = Oo.copy self
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> copy : <span class="ocamlkeyword">object</span> ('a) <span class="ocamlkeyword">method</span> copy : 'a <span class="ocamlkeyword">end</span></div></div>
</div><p>
Only the override can be used to actually override fields, and
only the <span class="c003">Oo.copy</span> primitive can be used externally.</p><p>Cloning can also be used to provide facilities for saving and
restoring the state of objects.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> backup =
<span class="ocamlkeyword">object</span> (self : 'mytype)
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> copy = None
<span class="ocamlkeyword">method</span> save = copy <- Some {< copy = None >}
<span class="ocamlkeyword">method</span> restore = <span class="ocamlkeyword">match</span> copy <span class="ocamlkeyword">with</span> Some x -> x | None -> self
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> backup :
<span class="ocamlkeyword">object</span> ('a)
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> copy : 'a option
<span class="ocamlkeyword">method</span> restore : 'a
<span class="ocamlkeyword">method</span> save : unit
<span class="ocamlkeyword">end</span></div></div>
</div><p>
The above definition will only backup one level.
The backup facility can be added to any class by using multiple inheritance.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> ['a] backup_ref x = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">inherit</span> ['a] oref x <span class="ocamlkeyword">inherit</span> backup <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> ['a] backup_ref :
'a ->
<span class="ocamlkeyword">object</span> ('b)
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> copy : 'b option
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : 'a
<span class="ocamlkeyword">method</span> get : 'a
<span class="ocamlkeyword">method</span> restore : 'b
<span class="ocamlkeyword">method</span> save : unit
<span class="ocamlkeyword">method</span> set : 'a -> unit
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> get p n = <span class="ocamlkeyword">if</span> n = 0 <span class="ocamlkeyword">then</span> p # get <span class="ocamlkeyword">else</span> get (p # restore) (n-1);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> get : (< get : 'b; restore : 'a; .. > <span class="ocamlkeyword">as</span> 'a) -> int -> 'b = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> backup_ref 0 <span class="ocamlkeyword">in</span>
p # save; p # set 1; p # save; p # set 2;
[get p 0; get p 1; get p 2; get p 3; get p 4];;</div>
<div class="pre caml-output ok">- : int list = [2; 1; 1; 1; 1]</div></div>
</div><p>
We can define a variant of backup that retains all copies. (We also
add a method <span class="c003">clear</span> to manually erase all copies.)
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> backup =
<span class="ocamlkeyword">object</span> (self : 'mytype)
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> copy = None
<span class="ocamlkeyword">method</span> save = copy <- Some {< >}
<span class="ocamlkeyword">method</span> restore = <span class="ocamlkeyword">match</span> copy <span class="ocamlkeyword">with</span> Some x -> x | None -> self
<span class="ocamlkeyword">method</span> clear = copy <- None
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> backup :
<span class="ocamlkeyword">object</span> ('a)
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> copy : 'a option
<span class="ocamlkeyword">method</span> clear : unit
<span class="ocamlkeyword">method</span> restore : 'a
<span class="ocamlkeyword">method</span> save : unit
<span class="ocamlkeyword">end</span></div></div>
</div><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> ['a] backup_ref x = <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">inherit</span> ['a] oref x <span class="ocamlkeyword">inherit</span> backup <span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> ['a] backup_ref :
'a ->
<span class="ocamlkeyword">object</span> ('b)
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> copy : 'b option
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> x : 'a
<span class="ocamlkeyword">method</span> clear : unit
<span class="ocamlkeyword">method</span> get : 'a
<span class="ocamlkeyword">method</span> restore : 'b
<span class="ocamlkeyword">method</span> save : unit
<span class="ocamlkeyword">method</span> set : 'a -> unit
<span class="ocamlkeyword">end</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> p = <span class="ocamlkeyword">new</span> backup_ref 0 <span class="ocamlkeyword">in</span>
p # save; p # set 1; p # save; p # set 2;
[get p 0; get p 1; get p 2; get p 3; get p 4];;</div>
<div class="pre caml-output ok">- : int list = [2; 1; 0; 0; 0]</div></div>
</div>
<h2 class="section" id="s:recursive-classes"><a class="section-anchor" href="#s:recursive-classes" aria-hidden="true"></a>3.15 Recursive classes</h2>
<p>Recursive classes can be used to define objects whose types are
mutually recursive.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> window =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> top_widget = (None : widget option)
<span class="ocamlkeyword">method</span> top_widget = top_widget
<span class="ocamlkeyword">end</span>
<span class="ocamlkeyword">and</span> widget (w : window) =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> window = w
<span class="ocamlkeyword">method</span> window = window
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> window :
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">val</span> <span class="ocamlkeyword">mutable</span> top_widget : widget option
<span class="ocamlkeyword">method</span> top_widget : widget option
<span class="ocamlkeyword">end</span>
<span class="ocamlkeyword">and</span> widget : window -> <span class="ocamlkeyword">object</span> <span class="ocamlkeyword">val</span> window : window <span class="ocamlkeyword">method</span> window : window <span class="ocamlkeyword">end</span></div></div>
</div><p>
Although their types are mutually recursive, the classes <span class="c003">widget</span> and
<span class="c003">window</span> are themselves independent.</p>
<h2 class="section" id="s:binary-methods"><a class="section-anchor" href="#s:binary-methods" aria-hidden="true"></a>3.16 Binary methods</h2>
<p>A binary method is a method which takes an argument of the same type
as self. The class <span class="c003">comparable</span> below is a template for classes with a
binary method <span class="c003">leq</span> of type <span class="c003">'a -> bool</span> where the type variable <span class="c003">'a</span>
is bound to the type of self. Therefore, <span class="c003">#comparable</span> expands to <span class="c003">< leq : 'a -> bool; .. > as 'a</span>. We see here that the binder <span class="c003">as</span> also
allows writing recursive types.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> <span class="ocamlkeyword">virtual</span> comparable =
<span class="ocamlkeyword">object</span> (_ : 'a)
<span class="ocamlkeyword">method</span> <span class="ocamlkeyword">virtual</span> leq : 'a -> bool
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> <span class="ocamlkeyword">virtual</span> comparable : <span class="ocamlkeyword">object</span> ('a) <span class="ocamlkeyword">method</span> <span class="ocamlkeyword">virtual</span> leq : 'a -> bool <span class="ocamlkeyword">end</span></div></div>
</div><p>
We then define a subclass <span class="c003">money</span> of <span class="c003">comparable</span>. The class <span class="c003">money</span>
simply wraps floats as comparable objects. We will extend it below with
more operations. We have to use a type constraint on the class parameter <span class="c003">x</span>
because the primitive <span class="c003"><=</span> is a polymorphic function in
OCaml. The <span class="c003">inherit</span> clause ensures that the type of objects
of this class is an instance of <span class="c003">#comparable</span>.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> money (x : float) =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">inherit</span> comparable
<span class="ocamlkeyword">val</span> repr = x
<span class="ocamlkeyword">method</span> value = repr
<span class="ocamlkeyword">method</span> leq p = repr <= p#value
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> money :
float ->
<span class="ocamlkeyword">object</span> ('a)
<span class="ocamlkeyword">val</span> repr : float
<span class="ocamlkeyword">method</span> leq : 'a -> bool
<span class="ocamlkeyword">method</span> value : float
<span class="ocamlkeyword">end</span></div></div>
</div><p>
Note that the type <span class="c003">money</span> is not a subtype of type
<span class="c003">comparable</span>, as the self type appears in contravariant position
in the type of method <span class="c003">leq</span>.
Indeed, an object <span class="c003">m</span> of class <span class="c003">money</span> has a method <span class="c003">leq</span>
that expects an argument of type <span class="c003">money</span> since it accesses
its <span class="c003">value</span> method. Considering <span class="c003">m</span> of type <span class="c003">comparable</span> would allow a
call to method <span class="c003">leq</span> on <span class="c003">m</span> with an argument that does not have a method
<span class="c003">value</span>, which would be an error.</p><p>Similarly, the type <span class="c003">money2</span> below is not a subtype of type <span class="c003">money</span>.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> money2 x =
<span class="ocamlkeyword">object</span>
<span class="ocamlkeyword">inherit</span> money x
<span class="ocamlkeyword">method</span> times k = {< repr = k *. repr >}
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> money2 :
float ->
<span class="ocamlkeyword">object</span> ('a)
<span class="ocamlkeyword">val</span> repr : float
<span class="ocamlkeyword">method</span> leq : 'a -> bool
<span class="ocamlkeyword">method</span> times : float -> 'a
<span class="ocamlkeyword">method</span> value : float
<span class="ocamlkeyword">end</span></div></div>
</div><p>
It is however possible to define functions that manipulate objects of
type either <span class="c003">money</span> or <span class="c003">money2</span>: the function <span class="c003">min</span>
will return the minimum of any two objects whose type unifies with
<span class="c003">#comparable</span>. The type of <span class="c003">min</span> is not the same as <span class="c003">#comparable -> #comparable -> #comparable</span>, as the abbreviation <span class="c003">#comparable</span> hides a
type variable (an ellipsis). Each occurrence of this abbreviation
generates a new variable.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> min (x : #comparable) y =
<span class="ocamlkeyword">if</span> x#leq y <span class="ocamlkeyword">then</span> x <span class="ocamlkeyword">else</span> y;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> min : (#comparable <span class="ocamlkeyword">as</span> 'a) -> 'a -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
This function can be applied to objects of type <span class="c003">money</span>
or <span class="c003">money2</span>.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> (min (<span class="ocamlkeyword">new</span> money 1.3) (<span class="ocamlkeyword">new</span> money 3.1))#value;;</div>
<div class="pre caml-output ok">- : float = 1.3</div></div>
<div class="ocaml">
<div class="pre caml-input"> (min (<span class="ocamlkeyword">new</span> money2 5.0) (<span class="ocamlkeyword">new</span> money2 3.14))#value;;</div>
<div class="pre caml-output ok">- : float = 3.14</div></div>
</div><p>More examples of binary methods can be found in
sections <a href="advexamples.html#ss%3Astring-as-class">6.2.1</a> and <a href="advexamples.html#ss%3Aset-as-class">6.2.3</a>.</p><p>Note the use of override for method <span class="c003">times</span>.
Writing <span class="c003">new money2 (k *. repr)</span> instead of <span class="c003">{< repr = k *. repr >}</span>
would not behave well with inheritance: in a subclass <span class="c003">money3</span> of <span class="c003">money2</span>
the <span class="c003">times</span> method would return an object of class <span class="c003">money2</span> but not of class
<span class="c003">money3</span> as would be expected.</p><p>The class <span class="c003">money</span> could naturally carry another binary method. Here is a
direct definition:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> money x =
<span class="ocamlkeyword">object</span> (self : 'a)
<span class="ocamlkeyword">val</span> repr = x
<span class="ocamlkeyword">method</span> value = repr
<span class="ocamlkeyword">method</span> print = print_float repr
<span class="ocamlkeyword">method</span> times k = {< repr = k *. x >}
<span class="ocamlkeyword">method</span> leq (p : 'a) = repr <= p#value
<span class="ocamlkeyword">method</span> plus (p : 'a) = {< repr = x +. p#value >}
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> money :
float ->
<span class="ocamlkeyword">object</span> ('a)
<span class="ocamlkeyword">val</span> repr : float
<span class="ocamlkeyword">method</span> leq : 'a -> bool
<span class="ocamlkeyword">method</span> plus : 'a -> 'a
<span class="ocamlkeyword">method</span> print : unit
<span class="ocamlkeyword">method</span> times : float -> 'a
<span class="ocamlkeyword">method</span> value : float
<span class="ocamlkeyword">end</span></div></div>
</div>
<h2 class="section" id="s:friends"><a class="section-anchor" href="#s:friends" aria-hidden="true"></a>3.17 Friends</h2>
<p>The above class <span class="c003">money</span> reveals a problem that often occurs with binary
methods. In order to interact with other objects of the same class, the
representation of <span class="c003">money</span> objects must be revealed, using a method such as
<span class="c003">value</span>. If we remove all binary methods (here <span class="c003">plus</span> and <span class="c003">leq</span>),
the representation can easily be hidden inside objects by removing the method
<span class="c003">value</span> as well. However, this is not possible as soon as some binary
method requires access to the representation of objects of the same
class (other than self).
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">class</span> safe_money x =
<span class="ocamlkeyword">object</span> (self : 'a)
<span class="ocamlkeyword">val</span> repr = x
<span class="ocamlkeyword">method</span> print = print_float repr
<span class="ocamlkeyword">method</span> times k = {< repr = k *. x >}
<span class="ocamlkeyword">end</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">class</span> safe_money :
float ->
<span class="ocamlkeyword">object</span> ('a)
<span class="ocamlkeyword">val</span> repr : float
<span class="ocamlkeyword">method</span> print : unit
<span class="ocamlkeyword">method</span> times : float -> 'a
<span class="ocamlkeyword">end</span></div></div>
</div><p>
Here, the representation of the object is known only to a particular object.
To make it available to other objects of the same class, we are forced to
make it available to the whole world. However we can easily restrict the
visibility of the representation using the module system.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">module</span> <span class="ocamlkeyword">type</span> MONEY =
<span class="ocamlkeyword">sig</span>
<span class="ocamlkeyword">type</span> t
<span class="ocamlkeyword">class</span> c : float ->
<span class="ocamlkeyword">object</span> ('a)
<span class="ocamlkeyword">val</span> repr : t
<span class="ocamlkeyword">method</span> value : t
<span class="ocamlkeyword">method</span> print : unit
<span class="ocamlkeyword">method</span> times : float -> 'a
<span class="ocamlkeyword">method</span> leq : 'a -> bool
<span class="ocamlkeyword">method</span> plus : 'a -> 'a
<span class="ocamlkeyword">end</span>
<span class="ocamlkeyword">end</span>;;</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">module</span> Euro : MONEY =
<span class="ocamlkeyword">struct</span>
<span class="ocamlkeyword">type</span> t = float
<span class="ocamlkeyword">class</span> c x =
<span class="ocamlkeyword">object</span> (self : 'a)
<span class="ocamlkeyword">val</span> repr = x
<span class="ocamlkeyword">method</span> value = repr
<span class="ocamlkeyword">method</span> print = print_float repr
<span class="ocamlkeyword">method</span> times k = {< repr = k *. x >}
<span class="ocamlkeyword">method</span> leq (p : 'a) = repr <= p#value
<span class="ocamlkeyword">method</span> plus (p : 'a) = {< repr = x +. p#value >}
<span class="ocamlkeyword">end</span>
<span class="ocamlkeyword">end</span>;;</div></div>
</div><p>
Another example of friend functions may be found in section <a href="advexamples.html#ss%3Aset-as-class">6.2.3</a>.
These examples occur when a group of objects (here
objects of the same class) and functions should see each others internal
representation, while their representation should be hidden from the
outside. The solution is always to define all friends in the same module,
give access to the representation and use a signature constraint to make the
representation abstract outside the module.</p>
<hr>
<a href="moduleexamples.html"><img src="previous_motif.svg" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.svg" alt="Up"></a>
<a href="lablexamples.html"><img src="next_motif.svg" alt="Next"></a>
</body>
</html>
|