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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>Expressions</TITLE>
<SCRIPT type="text/javascript" src="../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="6"><!-- Empty --></A>
<H2>6 Expressions</H2>
<P>In this chapter, all valid Erlang expressions are listed.
When writing Erlang programs, it is also allowed to use macro-
and record expressions. However, these expressions are expanded
during compilation and are in that sense not true Erlang
expressions. Macro- and record expressions are covered in
separate chapters: <A HREF="macros.html">Macros</A> and
<A HREF="records.html">Records</A>.<A NAME="6.1"><!-- Empty --></A>
<H3>6.1 Expression Evaluation</H3>
<P>All subexpressions are evaluated before an expression itself is
evaluated, unless explicitly stated otherwise. For example,
consider the expression:
<PRE>
Expr1 + Expr2
</PRE>
<P><CODE>Expr1</CODE> and <CODE>Expr2</CODE>, which are also expressions, are
evaluated first - in any order - before the addition is
performed.
<P>Many of the operators can only be applied to arguments of a
certain type. For example, arithmetic operators can only be
applied to numbers. An argument of the wrong type will cause
a <CODE>badarg</CODE> run-time error.<A NAME="term"><!-- Empty --></A><A NAME="6.2"><!-- Empty --></A>
<H3>6.2 Terms</H3>
<P>The simplest form of expression is a term, that is an integer,
float, atom, string, list or tuple.
The return value is the term itself.
<A NAME="6.3"><!-- Empty --></A>
<H3>6.3 Variables</H3>
<P>A variable is an expression. If a variable is bound to a value,
the return value is this value. Unbound variables are only
allowed in patterns.
<P>Variables start with an uppercase letter or underscore (_)
and may contain alphanumeric characters, underscore and @.
Examples:
<PRE>
X
Name1
PhoneNumber
Phone_number
_
_Height
</PRE>
<P>Variables are bound to values using
<A HREF="patterns.html">pattern matching</A>. Erlang
uses <STRONG>single assignment</STRONG>, a variable can only be bound
once.
<P>The <STRONG>anonymous variable</STRONG> is denoted by underscore (_) and
can be used when a variable is required but its value can be
ignored. Example:
<PRE>
[H|_] = [1,2,3]
</PRE>
<P>Variables starting with underscore (_), for example
<CODE>_Height</CODE>, are normal variables, not anonymous. They are
however ignored by the compiler in the sense that they will not
generate any warnings for unused variables. Example: The following
code
<PRE>
member(_, []) ->
[].
</PRE>
<P>can be rewritten to be more readable:
<PRE>
member(Elem, []) ->
[].
</PRE>
<P>This will however cause a warning for an unused variable
<CODE>Elem</CODE>, if the code is compiled with the flag
<CODE>warn_unused_vars</CODE> set. Instead, the code can be rewritten
to:
<PRE>
member(_Elem, []) ->
[].
</PRE>
<P>Note that since variables starting with an underscore are
not anonymous, this will match:
<PRE>
{_,_} = {1,2}
</PRE>
<P>But this will fail:
<PRE>
{_N,_N} = {1,2}
</PRE>
<P>The scope for a variable is its function clause.
Variables bound in a branch of an <CODE>if</CODE>, <CODE>case</CODE>,
or <CODE>receive</CODE> expression must be bound in all branches
to have a value outside the expression, otherwise they
will be regarded as 'unsafe' outside the expression.
<P>For the <CODE>try</CODE> expression introduced in
Erlang 5.4/OTP-R10B, variable scoping is limited so that
variables bound in the expression are always 'unsafe' outside
the expression. This will be improved.<A NAME="pattern"><!-- Empty --></A><A NAME="6.4"><!-- Empty --></A>
<H3>6.4 Patterns</H3>
<P>A pattern has the same structure as a term but may contain
unbound variables. Example:
<PRE>
Name1
[H|T]
{error,Reason}
</PRE>
<P>Patterns are allowed in clause heads, <CODE>case</CODE> and
<CODE>receive</CODE> expressions, and match expressions.<A NAME="6.4.1"><!-- Empty --></A>
<H4>6.4.1 Match Operator = in Patterns</H4>
<P>If <CODE>Pattern1</CODE> and <CODE>Pattern2</CODE> are valid patterns,
then the following is also a valid pattern:
<PRE>
Pattern1 = Pattern2
</PRE>
<P>When matched against a term, both <CODE>Pattern1</CODE> and
<CODE>Pattern2</CODE> will be matched against the term. The idea
behind this feature is to avoid reconstruction of terms.
Example:
<PRE>
f({connect,From,To,Number,Options}, To) ->
Signal = {connect,From,To,Number,Options},
...;
f(Signal, To) ->
ignore.
</PRE>
<P>can instead be written as
<PRE>
f({connect,_,To,_,_} = Signal, To) ->
...;
f(Signal, To) ->
ignore.
</PRE>
<A NAME="6.4.2"><!-- Empty --></A>
<H4>6.4.2 String Prefix in Patterns</H4>
<P>When matching strings, the following is a valid pattern:
<PRE>
f("prefix" ++ Str) -> ...
</PRE>
<P>This is syntactic sugar for the equivalent, but harder to
read
<PRE>
f([$p,$r,$e,$f,$i,$x | Str]) -> ...
</PRE>
<A NAME="6.4.3"><!-- Empty --></A>
<H4>6.4.3 Expressions in Patterns</H4>
<P>An arithmetic expression can be used within a pattern, if
it uses only numeric or bitwise operators, and if its value
can be evaluated to a constant at compile-time. Example:
<PRE>
case {Value, Result} of
{?THRESHOLD+1, ok} -> ...
</PRE>
<P>This feature was added in Erlang 5.0/OTP R7.<A NAME="6.5"><!-- Empty --></A>
<H3>6.5 Match</H3>
<PRE>
Expr1 = Expr2
</PRE>
<P>Matches <CODE>Expr1</CODE>, a pattern, against <CODE>Expr2</CODE>.
If the matching succeeds, any unbound variable in the pattern
becomes bound and the value of <CODE>Expr2</CODE> is returned.
<P>If the matching fails, a <CODE>badmatch</CODE> run-time error will
occur.
<P>Examples:
<PRE>
1> <STRONG>{A, B} = {answer, 42}.</STRONG>
{answer,42}
2> <STRONG>A.</STRONG>
answer
3> <STRONG>{C, D} = [1, 2].</STRONG>
** exited: {{badmatch,[1,2]},[{erl_eval,expr,3}]} **
</PRE>
<A NAME="calls"><!-- Empty --></A><A NAME="6.6"><!-- Empty --></A>
<H3>6.6 Function Calls</H3>
<PRE>
ExprF(Expr1,...,ExprN)
ExprM:ExprF(Expr1,...,ExprN)
</PRE>
<P><CODE>ExprM</CODE> should evalutate to a module name and <CODE>ExprF</CODE>
to a function name or a fun.
<P>When including the module name, the function is said to be
called by using the <STRONG>fully qualified function name</STRONG>.
This is often referred to as a <STRONG>remote</STRONG> or <STRONG>external
function call</STRONG>. Example:
<PRE>
lists:keysearch(Name, 1, List)
</PRE>
<P>The module name can be omitted, if <CODE>ExprF</CODE> evaluates to
the name of a local function, an imported function, or an
auto-imported BIF. Then the function is said to be called by
using the <STRONG>implicitly qualified function name</STRONG>.
Examples:
<PRE>
handle(Msg, State)
spawn(m, init, [])
</PRE>
<P>To avoid possible ambiguities, the fully qualified function
name must be used when calling a function with the same name as
a BIF, and the compiler does not allow defining a function with
the same name as an imported function.
<P>Note that when calling a local function, there is a difference
between using the implicitly or fully qualified function name, as
the latter always refer to the latest version of the module. See
<A HREF="code.html">Compilation and Code Loading</A>.
<P>If <CODE>ExprF</CODE> evaluates to a fun, only the format
<CODE>ExprF(Expr1,...,ExprN)</CODE> is correct. Example:
<PRE>
Fun1 = fun(X) -> X+1 end
Fun1(3)
=> 4
Fun2 = {lists,append}
Fun2([1,2],[3,4])
=> [1,2,3,4]
</PRE>
<P>See also the chapter about
<A HREF="functions.html#eval">Function Evaluation</A>.
<A NAME="6.7"><!-- Empty --></A>
<H3>6.7 If</H3>
<PRE>
if
GuardSeq1 ->
Body1;
...;
GuardSeqN ->
BodyN
end
</PRE>
<P>The branches of an <CODE>if</CODE>-expression are scanned sequentially
until a guard sequence <CODE>GuardSeq</CODE> which evaluates to true is
found. Then the corresponding <CODE>Body</CODE> (sequence of expressions
separated by ',') is evaluated.
<P>The return value of <CODE>Body</CODE> is the return value of
the <CODE>if</CODE> expression.
<P>If no guard sequence is true, an <CODE>if_clause</CODE> run-time error
will occur. If necessary, the guard expression <CODE>true</CODE> can be
used in the last branch, as that guard sequence is always true.
<P>Example:
<PRE>
is_greater_than(X, Y) ->
if
X>Y ->
true;
true -> % works as an 'else' branch
false
end
</PRE>
<A NAME="case"><!-- Empty --></A><A NAME="6.8"><!-- Empty --></A>
<H3>6.8 Case</H3>
<PRE>
case Expr of
Pattern1 [when GuardSeq1] ->
Body1;
...;
PatternN [when GuardSeqN] ->
BodyN
end
</PRE>
<P>The expression <CODE>Expr</CODE> is evaluated and the patterns
<CODE>Pattern</CODE> are sequentially matched against the result. If a
match succeeds and the optional guard sequence <CODE>GuardSeq</CODE> is
true, the corresponding <CODE>Body</CODE> is evaluated.
<P>The return value of <CODE>Body</CODE> is the return value of
the <CODE>case</CODE> expression.
<P>If there is no matching pattern with a true guard sequence,
a <CODE>case_clause</CODE> run-time error will occur.
<P>Example:
<PRE>
is_valid_signal(Signal) ->
case Signal of
{signal, _What, _From, _To} ->
true;
{signal, _What, _To} ->
true;
_Else ->
false
end.
</PRE>
<A NAME="send"><!-- Empty --></A><A NAME="6.9"><!-- Empty --></A>
<H3>6.9 Send</H3>
<PRE>
Expr1 ! Expr2
</PRE>
<P>Sends the value of <CODE>Expr2</CODE> as a message to the process
specified by <CODE>Expr1</CODE>. The value of <CODE>Expr2</CODE> is also
the return value of the expression.
<P><CODE>Expr1</CODE> must evaluate to a pid, a registered name (atom) or
a tuple <CODE>{Name,Node}</CODE>, where <CODE>Name</CODE> is an atom and
<CODE>Node</CODE> a node name, also an atom.
<P>
<UL>
<LI>
If <CODE>Expr1</CODE> evaluates to a name, but this name is not
registered, a <CODE>badarg</CODE> run-time error will occur.
</LI>
<LI>
Sending a message to a pid never fails, even if the pid
identifies a non-existing process.
</LI>
<LI>
Distributed message sending, that is if <CODE>Expr1</CODE>
evaluates to a tuple <CODE>{Name,Node}</CODE> (or a pid located at
another node), also never fails.
</LI>
</UL>
<A NAME="receive"><!-- Empty --></A><A NAME="6.10"><!-- Empty --></A>
<H3>6.10 Receive</H3>
<PRE>
receive
Pattern1 [when GuardSeq1] ->
Body1;
...;
PatternN [when GuardSeqN] ->
BodyN
end
</PRE>
<P>Receives messages sent to the process using the send operator
(!). The patterns <CODE>Pattern</CODE> are sequentially matched
against the first message in time order in the mailbox, then
the second, and so on. If a match succeeds and the optional
guard sequence <CODE>GuardSeq</CODE> is true, the corresponding
<CODE>Body</CODE> is evaluated. The matching message is consumed, that
is removed from the mailbox, while any other messages in
the mailbox remain unchanged.
<P>The return value of <CODE>Body</CODE> is the return value of
the <CODE>receive</CODE> expression.
<P><CODE>receive</CODE> never fails. Execution is suspended, possibly
indefinitely, until a message arrives that does match one of
the patterns and with a true guard sequence.
<P>Example:
<PRE>
wait_for_onhook() ->
receive
onhook ->
disconnect(),
idle();
{connect, B} ->
B ! {busy, self()},
wait_for_onhook()
end.
</PRE>
<P>It is possible to augment the <CODE>receive</CODE> expression with a
timeout:
<PRE>
receive
Pattern1 [when GuardSeq1] ->
Body1;
...;
PatternN [when GuardSeqN] ->
BodyN
after
ExprT ->
BodyT
end
</PRE>
<P><CODE>ExprT</CODE> should evaluate to an integer. The highest allowed
value is 16#ffffffff, that is, the value must fit in 32 bits.
<CODE>receive..after</CODE> works exactly as <CODE>receive</CODE>, except
that if no matching message has arrived within <CODE>ExprT</CODE>
milliseconds, then <CODE>BodyT</CODE> is evaluated instead and its
return value becomes the return value of the <CODE>receive..after</CODE>
expression.
<P>Example:
<PRE>
wait_for_onhook() ->
receive
onhook ->
disconnect(),
idle();
{connect, B} ->
B ! {busy, self()},
wait_for_onhook()
after
60000 ->
disconnect(),
error()
end.
</PRE>
<P>It is legal to use a <CODE>receive..after</CODE> expression with no
branches:
<PRE>
receive
after
ExprT ->
BodyT
end
</PRE>
<P>This construction will not consume any messages, only suspend
execution in the process for <CODE>ExprT</CODE> milliseconds and can be
used to implement simple timers.
<P>Example:
<PRE>
timer() ->
spawn(m, timer, [self()]).
timer(Pid) ->
receive
after
5000 ->
Pid ! timeout
end.
</PRE>
<P>There are two special cases for the timeout value <CODE>ExprT</CODE>:
<P>
<DL>
<DT>
<CODE>infinity</CODE>
</DT>
<DD>
The process should wait indefinitely for a matching message
-- this is the same as not using a timeout. Can be
useful for timeout values that are calculated at run-time.
</DD>
<DT>
0
</DT>
<DD>
If there is no matching message in the mailbox, the timeout
will occur immediately.
</DD>
</DL>
<A NAME="6.11"><!-- Empty --></A>
<H3>6.11 Term Comparisons</H3>
<PRE>
Expr1 <STRONG>op</STRONG> Expr2
</PRE>
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
<CAPTION ALIGN=BOTTOM><EM>Term Comparison Operators.</EM></CAPTION>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<STRONG>op</STRONG>
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<STRONG>Description</STRONG>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
==
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
equal to
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
/=
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
not equal to
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
=<
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
less than or equal to
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
less than
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
>=
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
greater than or equal to
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
>
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
greater than
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
=:=
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
exactly equal to
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
=/=
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
exactly not equal to
</TD>
</TR>
</TABLE>
</CENTER>
<P>The arguments may be of different data types. The following
order is defined:
<PRE>
number < atom < reference < fun < port < pid < tuple < list < binary
</PRE>
<P>Lists are compared element by element. Tuples are ordered by
size, two tuples with the same size are compared element by
element.
<P>All comparison operators except =:= and =/= are of type coerce:
When comparing an integer and a float, the integer is first
converted to a float. In the case of =:= and =/=, there is no type
conversion.
<P>Returns the Boolean value of the expression, <CODE>true</CODE> or
<CODE>false</CODE>.
<P>Examples:
<PRE>
1> <STRONG>1==1.0.</STRONG>
true
2> <STRONG>1=:=1.0.</STRONG>
false
3> <STRONG>1 > a.</STRONG>
false
</PRE>
<A NAME="6.12"><!-- Empty --></A>
<H3>6.12 Arithmetic Expressions</H3>
<PRE>
<STRONG>op</STRONG> Expr
Expr1 <STRONG>op</STRONG> Expr2
</PRE>
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
<CAPTION ALIGN=BOTTOM><EM>Arithmetic Operators.</EM></CAPTION>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<STRONG>op</STRONG>
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<STRONG>Description</STRONG>
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<STRONG>Argument type</STRONG>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
+
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
unary +
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
number
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
-
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
unary -
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
number
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
+
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
number
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
-
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
number
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
*
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
number
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
/
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
floating point division
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
number
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
bnot
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
unary bitwise not
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
integer
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
div
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
integer division
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
integer
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
rem
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
integer remainder of X/Y
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
integer
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
band
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
bitwise and
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
integer
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
bor
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
bitwise or
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
integer
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
bxor
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
arithmetic bitwise xor
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
integer
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
bsl
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
arithmetic bitshift left
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
integer
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
bsr
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
bitshift right
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
integer
</TD>
</TR>
</TABLE>
</CENTER>
<P>Examples:
<PRE>
1> <STRONG>+1.</STRONG>
1
2> <STRONG>-1.</STRONG>
-1
3> <STRONG>1+1.</STRONG>
2
4> <STRONG>4/2.</STRONG>
2.00000
5> <STRONG>5 div 2.</STRONG>
2
6> <STRONG>5 rem 2.</STRONG>
1
7> <STRONG>2#10 band 2#01.</STRONG>
0
8> <STRONG>2#10 bor 2#01.</STRONG>
3
</PRE>
<A NAME="6.13"><!-- Empty --></A>
<H3>6.13 Boolean Expressions</H3>
<PRE>
<STRONG>op</STRONG> Expr
Expr1 <STRONG>op</STRONG> Expr2
</PRE>
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
<CAPTION ALIGN=BOTTOM><EM>Logical Operators.</EM></CAPTION>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<STRONG>op</STRONG>
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<STRONG>Description</STRONG>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
not
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
unary logical not
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
and
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
logical and
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
or
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
logical or
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
xor
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
logical xor
</TD>
</TR>
</TABLE>
</CENTER>
<P>Examples:
<PRE>
1> <STRONG>not true.</STRONG>
false
2> <STRONG>true and false.</STRONG>
false
3> <STRONG>true xor false.</STRONG>
true
</PRE>
<A NAME="6.14"><!-- Empty --></A>
<H3>6.14 Short-Circuit Boolean Expressions</H3>
<PRE>
Expr1 orelse Expr2
Expr1 andalso Expr2
</PRE>
<P>Boolean expressions where <CODE>Expr2</CODE> is evaluated only if
necessary. That is, <CODE>Expr2</CODE> is evaluated only if <CODE>Expr1</CODE>
evaluates to <CODE>false</CODE> in an <CODE>orelse</CODE> expression, or only
if <CODE>Expr1</CODE> evaluates to <CODE>true</CODE> in an <CODE>andalso</CODE>
expression. Returns the Boolean value of the expression, that is
<CODE>true</CODE> or <CODE>false</CODE>.
<P>As of Erlang 5.5/OTP R11B, short-circuit boolean expressions are
allowed in guards.
In guards, however, evaluation is always short-circuited
since guard tests are known to be free of side effects.
<P>Example 1:
<PRE>
case A >= -1.0 andalso math:sqrt(A+1) > B of
</PRE>
<P>This will work even if <CODE>A</CODE> is less than <CODE>-1.0</CODE>,
since in that case, <CODE>math:sqrt/1</CODE> is never evaluated.
<P>Example 2:
<PRE>
OnlyOne = is_atom(L) orelse
(is_list(L) andalso length(L) == 1),
</PRE>
<P>This feature was added in Erlang 5.1/OTP R8.<A NAME="6.15"><!-- Empty --></A>
<H3>6.15 List Operations</H3>
<PRE>
Expr1 ++ Expr2
Expr1 -- Expr2
</PRE>
<P>The list concatenation operator <CODE>++</CODE> appends its second
argument to its first and returns the resulting list.
<P>The list subtraction operator <CODE>--</CODE> produces a list which
is a copy of the first argument, subjected to the following
procedure: for each element in the second argument, the first
occurrence of this element (if any) is removed.
<P>Example:
<PRE>
1> <STRONG>[1,2,3]++[4,5].</STRONG>
[1,2,3,4,5]
2> <STRONG>[1,2,3,2,1,2]--[2,1,2].</STRONG>
[3,1,2]
</PRE>
<A NAME="bit_syntax"><!-- Empty --></A><A NAME="6.16"><!-- Empty --></A>
<H3>6.16 Bit Syntax Expressions</H3>
<PRE>
<<>>
<<E1,...,En>>
</PRE>
<P>Each element <CODE>Ei</CODE> specifies a <STRONG>segment</STRONG> of
the binary. Each element <CODE>Ei</CODE> is a value, followed by an
optional <STRONG>size expression</STRONG> and an optional <STRONG>type
specifier list</STRONG>.
<PRE>
Ei = Value |
Value:Size |
Value/TypeSpecifierList |
Value:Size/TypeSpecifierList
</PRE>
<P>Used in a binary construction, <CODE>Value</CODE> is an expression
which should evaluate to an integer, float or binary.
If the expression is something else than a single literal or
variable, it should be enclosed in parenthesis.
<P>Used in a binary matching, <CODE>Value</CODE> must be a variable, or
an integer, float or string.
<P>Note that, for example, using a string literal as in
<CODE><<"abc">></CODE> is syntactic sugar for <CODE><<$a,$b,$c>></CODE>.
<P>Used in a binary construction, <CODE>Size</CODE> is an expression
similar to <CODE>Value</CODE>, which should evaluate to an integer.
<P>Used in a binary matching, <CODE>Size</CODE> must be an integer, or
a variable bound to an integer.
<P>The value of <CODE>Size</CODE> specifies the size of the segment in
units (see below). The default value depends on the type (see
below). For <CODE>integer</CODE> it is 8, for <CODE>float</CODE> it is 64,
for <CODE>binary</CODE> it is all of the binary. In matching, this
default value is only valid for the very last element. All other
binary elements in the matching must have a size specification.
<P><CODE>TypeSpecifierList</CODE> is a list of type specifiers, in any
order, separated by hyphens (-). Default values are used for any
omitted type specifiers.
<P>
<DL>
<DT>
<CODE>Type</CODE> = <CODE>integer</CODE> | <CODE>float</CODE> |
<CODE>binary</CODE>
</DT>
<DD>
The default is <CODE>integer</CODE>.
</DD>
<DT>
<CODE>Signedness</CODE> = <CODE>signed</CODE> | <CODE>unsigned</CODE>
</DT>
<DD>
Only matters for matching. The default is <CODE>unsigned</CODE>.
</DD>
<DT>
<CODE>Endianness</CODE> = <CODE>big</CODE> | <CODE>little</CODE> |
<CODE>native</CODE>
</DT>
<DD>
Native-endian means that the endian will be resolved at load
time to be either big-endian or little-endian, depending on
what is native for the CPU that the Erlang machine is run on.
The default is <CODE>big</CODE>.
</DD>
<DT>
<CODE>Unit</CODE> = <CODE>unit:IntegerLiteral</CODE>
</DT>
<DD>
The allowed range is 1..256. Defaults to 1 for <CODE>integer</CODE>
and <CODE>float</CODE>, and to 8 for <CODE>binary</CODE>.
</DD>
</DL>
<P>The value of <CODE>Size</CODE> multiplied with the unit gives
the number of bits for the segment. Each segment can consist of
zero or more bits, but the total number of bits must be divisible
by 8, or a <CODE>badarg</CODE> run-time error will occur. Also, a
segment of type <CODE>binary</CODE> must have a size evenly divisble
by 8.
<P>Examples:
<PRE>
1> <STRONG>Bin1 = <<1,17,42>>.</STRONG>
<<1,17,42>>
2> <STRONG>Bin2 = <<"abc">>.</STRONG>
<<97,98,99>>
3> <STRONG>Bin3 = <<1,17,42:16>>.</STRONG>
<<1,17,0,42>>
4> <STRONG><<A,B,C:16>> = <<1,17,42:16>>.</STRONG>
<<1,17,0,42>>
5> <STRONG>C.</STRONG>
42
6> <STRONG><<D:16,E,F>> = <<1,17,42:16>>.</STRONG>
<<1,17,0,42>>
7> <STRONG>D.</STRONG>
273
8> <STRONG>F.</STRONG>
42
9> <STRONG><<G,H/binary>> = <<1,17,42:16>>.</STRONG>
<<1,17,0,42>>
10> <STRONG>H.</STRONG>
<<17,0,42>>
</PRE>
<P>Note that binary patterns cannot be nested.
<P>Note also that "<CODE>B=<<1>></CODE>" is interpreted as
"<CODE>B =< <1>></CODE>" which is a syntax error. The correct way is
to write a space after '=': "<CODE>B= <<1>></CODE>.
<P>More examples can be found in <STRONG>Programming Examples</STRONG>.<A NAME="funs"><!-- Empty --></A><A NAME="6.17"><!-- Empty --></A>
<H3>6.17 Fun Expressions</H3>
<PRE>
fun
(Pattern11,...,Pattern1N) [when GuardSeq1] ->
Body1;
...;
(PatternK1,...,PatternKN) [when GuardSeqK] ->
BodyK
end
</PRE>
<P>A fun expression begins with the keyword <CODE>fun</CODE> and ends
with the keyword <CODE>end</CODE>. Between them should be a function
declaration, similar to a
<A HREF="functions.html#syntax">regular function
declaration</A>, except that no function name is
specified.
<P>Variables in a fun head shadow variables in the
function clause surrounding the fun expression, and
variables bound in a fun body are local to the fun body.
<P>The return value of the expression is the resulting fun.
<P>Examples:
<PRE>
1> <STRONG>Fun1 = fun (X) -> X+1 end.</STRONG>
#Fun<erl_eval.6.39074546>
2> <STRONG>Fun1(2).</STRONG>
3
3> <STRONG>Fun2 = fun (X) when X>=5 -> gt; (X) -> lt end.</STRONG>
#Fun<erl_eval.6.39074546>
4> <STRONG>Fun2(7).</STRONG>
gt
</PRE>
<P>The following fun expressions are also allowed:
<PRE>
fun Name/Arity
fun Module:Name/Arity
</PRE>
<P>In <CODE>Name/Arity</CODE>, <CODE>Name</CODE> is an atom and <CODE>Arity</CODE> is an integer.
<CODE>Name/Arity</CODE> must specify an existing local function. The expression is
syntactic sugar for:
<PRE>
fun (Arg1,...,ArgN) -> Name(Arg1,...,ArgN) end
</PRE>
<P>In <CODE>Module:Name/Arity</CODE>, <CODE>Module</CODE> and <CODE>Name</CODE> are atoms
and <CODE>Arity</CODE> is an integer.
A fun defined in this way will refer to the function <CODE>Name</CODE>
with arity <CODE>Arity</CODE> in the <STRONG>latest</STRONG> version of module <CODE>Module</CODE>.
<P>When applied to a number N of arguments, a tuple
<CODE>{Module,FunctionName}</CODE> is interpreted as a fun, referring
to the function <CODE>FunctionName</CODE> with arity N in the module
<CODE>Module</CODE>. The function must be exported.
<STRONG>This usage is deprecated.</STRONG>
See <A HREF="#calls">Function Calls</A> for an example.
<P>More examples can be found in <STRONG>Programming Examples</STRONG>.<A NAME="catch"><!-- Empty --></A><A NAME="6.18"><!-- Empty --></A>
<H3>6.18 Catch and Throw</H3>
<PRE>
catch Expr
</PRE>
<P>Returns the value of <CODE>Expr</CODE> unless an exception
occurs during the evaluation. In that case, the exception is
caught. For exceptions of class <CODE>error</CODE>,
that is run-time errors: <CODE>{'EXIT',{Reason,Stack}}</CODE>
is returned. For exceptions of class <CODE>exit</CODE>, that is
the code called <CODE>exit(Term)</CODE>: <CODE>{'EXIT',Term}</CODE> is returned.
For exceptions of class <CODE>throw</CODE>, that is
the code called <CODE>throw(Term)</CODE>: <CODE>Term</CODE> is returned.
<P><CODE>Reason</CODE> depends on the type of error that occurred, and
<CODE>Stack</CODE> is the stack of recent function calls, see
<A HREF="errors.html#exit_reasons">Errors and Error
Handling</A>.
<P>Examples:
<P>
<PRE>
1> <STRONG>catch 1+2.</STRONG>
3
2> <STRONG>catch 1+a.</STRONG>
{'EXIT',{badarith,[...]}}
</PRE>
<P>Note that <CODE>catch</CODE> has low precedence and catch
subexpressions often needs to be enclosed in a block
expression or in parenthesis:
<PRE>
3> <STRONG>A = catch 1+2.</STRONG>
** 1: syntax error before: 'catch' **
4> <STRONG>A = (catch 1+2).</STRONG>
3
</PRE>
<P>The BIF <CODE>throw(Any)</CODE> can be used for non-local return from
a function. It must be evaluated within a <CODE>catch</CODE>, which will
return the value <CODE>Any</CODE>. Example:
<PRE>
5> <STRONG>catch throw(hello).</STRONG>
hello
</PRE>
<P>If <CODE>throw/1</CODE> is not evaluated within a catch, a
<CODE>nocatch</CODE> run-time error will occur.<A NAME="try"><!-- Empty --></A><A NAME="6.19"><!-- Empty --></A>
<H3>6.19 Try</H3>
<PRE>
try Expr
catch
[Class1:]ExceptionPattern1 [when ExceptionGuardSeq1] ->
ExceptionBody1;
[ClassN:]ExceptionPatternN [when ExceptionGuardSeqN] ->
ExceptionBodyN
end
</PRE>
<P>This is an enhancement of <A HREF="#catch">catch</A>
that appeared in Erlang 5.4/OTP-R10B. It gives the possibility
do distinguish between different exception classes, and
to choose to handle only the desired ones, passing the others
on to an enclosing <CODE>try</CODE> or <CODE>catch</CODE> or to default
error handling.
<P>Note that althought the keyword <CODE>catch</CODE> is used in the
<CODE>try</CODE> expression there is not a <CODE>catch</CODE> expression
whithin the <CODE>try</CODE> expression.
<P>Returns the value of <CODE>Expr</CODE> unless an exception occurs
during the evaluation. In that case the exception is caught and the
patterns <CODE>ExceptionPattern</CODE> with the right exception
class <CODE>Class</CODE> are sequentially matched against the caught
exception. An omitted <CODE>Class</CODE> is shorthand for <CODE>throw</CODE>.
If a match succeeds and the optional guard
sequence <CODE>ExceptionGuardSeq</CODE> is true, the corresponding
<CODE>ExceptionBody</CODE> is evaluated to become the return value.
<P>If an exception occurs during evaluation of <CODE>Expr</CODE> but
there is no matching <CODE>ExceptionPattern</CODE> of the right
<CODE>Class</CODE> with a true guard sequence, the exception is
passed on as if <CODE>Expr</CODE> had not been enclosed in a
<CODE>try</CODE> expression.
<P>If an exception occurs during evaluation of
<CODE>ExceptionBody</CODE> it is not caught.
<P>The <CODE>try</CODE> expression can have an <CODE>of</CODE>
section:
<PRE>
try Expr of
Pattern1 [when GuardSeq1] ->
Body1;
...;
PatternN [when GuardSeqN] ->
BodyN
catch
[Class1:]ExceptionPattern1 [when ExceptionGuardSeq1] ->
ExceptionBody1;
...;
[ClassN:]ExceptionPatternN [when ExceptionGuardSeqN] ->
ExceptionBodyN
end
</PRE>
<P>If the evaluation of <CODE>Expr</CODE> succedes without an
exception, the value is then matched against the
patterns <CODE>Pattern</CODE> in the same way as for a
<A HREF="#case">case</A> expression to
produce the return value, except that if the matching fails,
a <CODE>try_clause</CODE> run-time error will occur.
<P>If an exception occurs during evaluation of
<CODE>Body</CODE> it is not caught.
<P>The <CODE>try</CODE> expression can also be augumented with an
<CODE>after</CODE> section, intended to be used for cleanup
with side effects:
<PRE>
try Expr of
Pattern1 [when GuardSeq1] ->
Body1;
...;
PatternN [when GuardSeqN] ->
BodyN
catch
[Class1:]ExceptionPattern1 [when ExceptionGuardSeq1] ->
ExceptionBody1;
...;
[ClassN:]ExceptionPatternN [when ExceptionGuardSeqN] ->
ExceptionBodyN
after
AfterBody
end
</PRE>
<P>The <CODE>AfterBody</CODE> is evaluated after either
<CODE>Body</CODE> or <CODE>ExceptionBody</CODE> no matter
which one. The evaluated value of the <CODE>AfterBody</CODE> is
lost; the return value of the <CODE>try</CODE> expression is
the same with an <CODE>after</CODE> section as without.
<P>Even if an exception occurs during evaluation of
<CODE>Body</CODE> or <CODE>ExceptionBody</CODE>, the <CODE>AfterBody</CODE>
is evaluated. In this case the exception is caught
and passed on after the <CODE>AfterBody</CODE> has been
evaluated, so the exception from the <CODE>try</CODE> expression
is the same with an <CODE>after</CODE> section as without.
<P>If an exception occurs during evaluation of
<CODE>AfterBody</CODE> itself it is not caught, so if
the <CODE>AfterBody</CODE> is evaluated due to an exception
in <CODE>Expr</CODE>, <CODE>Body</CODE> or <CODE>ExceptionBody</CODE>,
that exception is lost and masked by the new exception.
<P>The <CODE>of</CODE>, <CODE>catch</CODE> and <CODE>after</CODE> sections,
are all optional in the <CODE>try</CODE> expression, as long as
there is at least a <CODE>catch</CODE> or an <CODE>after</CODE> section,
so the following are also valid <CODE>try</CODE> expressions:
<PRE>
try Expr of
Pattern when GuardSeq ->
Body
after
AfterBody
end
try Expr
catch
ExpressionPattern ->
ExpressionBody
after
AfterBody
end
try Expr after AfterBody end
</PRE>
<P>Example of using <CODE>after</CODE>, this code will close the file
even in the event of exceptions in <CODE>file:read/2</CODE>
or in <CODE>binary_to_term/1</CODE>, and exceptions will be the
same as without the <CODE>try</CODE>...<CODE>after</CODE>...<CODE>end</CODE>
expression:
<PRE>
termize_file(Name) ->
{ok,F} = file:open(Name, [read,binary]),
try
{ok,Bin} = file:read(F, 1024*1024),
binary_to_term(Bin)
after
file:close(F)
end.
</PRE>
<P>Example: Using <CODE>try</CODE> to emulate <CODE>catch Expr</CODE>
<PRE>
try Expr
catch
throw:Term -> Term;
exit:Reason -> {'EXIT',Reason}
error:Reason -> {'EXIT',{Reason,erlang:get_stacktrace()}}
end
</PRE>
<A NAME="6.20"><!-- Empty --></A>
<H3>6.20 Parenthesized Expressions</H3>
<PRE>
(Expr)
</PRE>
<P>Parenthesized expressions are useful to override
<A HREF="#prec">operator precedences</A>,
for example in arithmethic expressions:
<PRE>
1> <STRONG>1 + 2 * 3.</STRONG>
7
2> <STRONG>(1 + 2) * 3.</STRONG>
9
</PRE>
<A NAME="6.21"><!-- Empty --></A>
<H3>6.21 Block Expressions</H3>
<PRE>
begin
Expr1,
...,
ExprN
end
</PRE>
<P>Block expressions provide a way to group a sequence of
expressions, similar to a clause body. The return value is
the value of the last expression <CODE>ExprN</CODE>.<A NAME="lcs"><!-- Empty --></A><A NAME="6.22"><!-- Empty --></A>
<H3>6.22 List Comprehensions</H3>
<P>List comprehensions are a feature of many modern functional
programming languages. Subject to certain rules, they provide a
succinct notation for generating elements in a list.
<P>List comprehensions are analogous to set comprehensions in
Zermelo-Frankel set theory and are called ZF expressions in
Miranda. They are analogous to the <CODE>setof</CODE> and
<CODE>findall</CODE> predicates in Prolog.
<P>List comprehensions are written with the following syntax:
<PRE>
[Expr || Qualifier1,...,QualifierN]
</PRE>
<P><CODE>Expr</CODE> is an arbitrary expression, and each
<CODE>Qualifier</CODE> is either a generator or a filter.
<P>
<UL>
<LI>
A <STRONG>generator</STRONG> is written as:<BR>
<CODE>Pattern <- ListExpr</CODE>.<BR>
<CODE>ListExpr</CODE> must be an expression which evaluates to a
list of terms.
</LI>
<LI>
A <STRONG>filter</STRONG> is an expression which evaluates to
<CODE>true</CODE> or <CODE>false</CODE>.
</LI>
</UL>
<P>The variables in the generator patterns shadow variables in
the function clause surrounding the list comprehensions.
<P>A list comprehension returns a list, where the elements are
the result of evaluating <CODE>Expr</CODE> for each combination of
generator list elements for which all filters are true.
<P>
<P>Example:
<PRE>
1> <STRONG>[X*2 || X <- [1,2,3]].</STRONG>
[2,4,6]
</PRE>
<P>More examples can be found in <STRONG>Programming Examples</STRONG>.<A NAME="guards"><!-- Empty --></A><A NAME="6.23"><!-- Empty --></A>
<H3>6.23 Guard Sequences</H3>
<P>A <STRONG>guard sequence</STRONG> is a sequence of guards, separated
by semicolon (;). The guard sequence is true if at least one of
the guards is true.<BR>
<CODE>Guard1;...;GuardK</CODE>
<P>A <STRONG>guard</STRONG> is a sequence of guard expressions, separated
by comma (,). The guard is true if all guard expressions
evaluate to <CODE>true</CODE>.<BR>
<CODE>GuardExpr1,...,GuardExprN</CODE>
<P>The set of valid <STRONG>guard expressions</STRONG> (sometimes called
guard tests) is a subset of the set of valid Erlang expressions.
The reason for restricting the set of valid expressions is that
evaluation of a guard expression must be guaranteed to be free
of side effects. Valid guard expressions are:
<P>
<UL>
<LI>
the atom <CODE>true</CODE>,
</LI>
<LI>
other constants (terms and bound variables), all regarded
as false,
</LI>
<LI>
calls to the BIFs specified below,
</LI>
<LI>
term comparisons,
</LI>
<LI>
arithmetic expressions,
</LI>
<LI>
boolean expressions, and
</LI>
<LI>
short-circuit boolean expressions.
</LI>
</UL>
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
<CAPTION ALIGN=BOTTOM><EM>Type Test BIFs.</EM></CAPTION>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_atom/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_binary/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_constant/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_float/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_function/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_function/2</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_integer/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_list/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_number/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_pid/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_port/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_reference/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_tuple/1</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_record/2</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>is_record/3</CODE>
</TD>
</TR>
</TABLE>
</CENTER>
<P>Note that each type test BIF has an older equivalent, without
the <CODE>is_</CODE> prefix. These old BIFs are retained for backwards
compatibility only and should not be used in new code. They are
also only allowed at top level. For example, they are not allowed
in boolean expressions in guards.
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
<CAPTION ALIGN=BOTTOM><EM>Other BIFs Allowed in Guard Expressions.</EM></CAPTION>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>abs(Number)</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>element(N, Tuple)</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>float(Term)</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>hd(List)</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>length(List)</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>node()</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>node(Pid|Ref|Port)</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>round(Number)</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>self()</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>size(Tuple|Binary)</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>tl(List)</CODE>
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
<CODE>trunc(Number)</CODE>
</TD>
</TR>
</TABLE>
</CENTER>
<A NAME="prec"><!-- Empty --></A><A NAME="6.24"><!-- Empty --></A>
<H3>6.24 Operator Precedence</H3>
<P>Operator precedence in falling priority:
<P>
<CENTER>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
<CAPTION ALIGN=BOTTOM><EM>Operator Precedence.</EM></CAPTION>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
:
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
#
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
Unary + - bnot not
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
/ * div rem band and
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
Left associative
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
+ - bor bxor bsl bsr or xor
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
Left associative
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
++ --
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
Right associative
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
== /= =< < >= > =:= =/=
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
-
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
andalso
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
orelse
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
= !
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
Right associative
</TD>
</TR>
<TR>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
catch
</TD>
<TD ALIGN="LEFT" VALIGN="MIDDLE">
</TD>
</TR>
</TABLE>
</CENTER>
<P>When evaluating an expression, the operator with the highest
priority is evaluated first. Operators with the same priority
are evaluated according to their associativity. Example:
The left associative arithmethic operators are evaluated left to
right:
<PRE>
<STRONG>6 + 5 * 4 - 3 / 2</STRONG> evaluates to
<STRONG>6 + 20 - 1.5</STRONG> evaluates to
<STRONG>26 - 1.5</STRONG> evaluates to
<STRONG>24.5</STRONG>
</PRE>
<CENTER>
<HR>
<SMALL>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|