diff options
author | Li Jin <dragon-fly@qq.com> | 2023-10-22 00:55:02 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-10-22 00:55:02 +0800 |
commit | 652a8839f885b73fff57942a9db8b26e9cb5233b (patch) | |
tree | 0b24bb5610823a831ec0addbba37cb6200203b27 /doc | |
parent | f61a4a1d9a1b979b8a0c2e8a9c194a284f42220f (diff) | |
download | yuescript-652a8839f885b73fff57942a9db8b26e9cb5233b.tar.gz yuescript-652a8839f885b73fff57942a9db8b26e9cb5233b.tar.bz2 yuescript-652a8839f885b73fff57942a9db8b26e9cb5233b.zip |
fixing issues from #152.
Diffstat (limited to 'doc')
-rwxr-xr-x | doc/docs/doc/README.md | 180 | ||||
-rwxr-xr-x | doc/docs/zh/doc/README.md | 182 |
2 files changed, 203 insertions, 159 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index c60330a..6662752 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
@@ -389,6 +389,80 @@ tb::func! if tb != nil | |||
389 | </pre> | 389 | </pre> |
390 | </YueDisplay> | 390 | </YueDisplay> |
391 | 391 | ||
392 | ### Chaining Comparisons | ||
393 | |||
394 | Comparisons can be arbitrarily chained: | ||
395 | |||
396 | ```moonscript | ||
397 | print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 | ||
398 | -- output: true | ||
399 | |||
400 | a = 5 | ||
401 | print 1 <= a <= 10 | ||
402 | -- output: true | ||
403 | ``` | ||
404 | <YueDisplay> | ||
405 | <pre> | ||
406 | print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 | ||
407 | -- output: true | ||
408 | |||
409 | a = 5 | ||
410 | print 1 <= a <= 10 | ||
411 | -- output: true | ||
412 | </pre> | ||
413 | </YueDisplay> | ||
414 | |||
415 | Note the evaluation behavior of chained comparisons: | ||
416 | |||
417 | ```moonscript | ||
418 | v = (x)-> | ||
419 | print x | ||
420 | x | ||
421 | |||
422 | print v(1) < v(2) <= v(3) | ||
423 | --[[ | ||
424 | output: | ||
425 | 2 | ||
426 | 1 | ||
427 | 3 | ||
428 | true | ||
429 | ]] | ||
430 | |||
431 | print v(1) > v(2) <= v(3) | ||
432 | --[[ | ||
433 | output: | ||
434 | 2 | ||
435 | 1 | ||
436 | false | ||
437 | ]] | ||
438 | ``` | ||
439 | <YueDisplay> | ||
440 | <pre> | ||
441 | v = (x)-> | ||
442 | print x | ||
443 | x | ||
444 | |||
445 | print v(1) < v(2) <= v(3) | ||
446 | --[[ | ||
447 | output: | ||
448 | 2 | ||
449 | 1 | ||
450 | 3 | ||
451 | true | ||
452 | ]] | ||
453 | |||
454 | print v(1) > v(2) <= v(3) | ||
455 | --[[ | ||
456 | output: | ||
457 | 2 | ||
458 | 1 | ||
459 | false | ||
460 | ]] | ||
461 | </pre> | ||
462 | </YueDisplay> | ||
463 | |||
464 | The middle expression is only evaluated once, rather than twice as it would be if the expression were written as `v(1) < v(2) and v(2) <= v(3)`. However, the order of evaluations in a chained comparison is undefined. It is strongly recommended not to use expressions with side effects (such as printing) in chained comparisons. If side effects are required, the short-circuit `and` operator should be used explicitly. | ||
465 | |||
392 | ### Table Appending | 466 | ### Table Appending |
393 | The **[] =** operator is used to append values to tables. | 467 | The **[] =** operator is used to append values to tables. |
394 | 468 | ||
@@ -984,17 +1058,17 @@ Typically when you see a table literal, {1,2,3}, it is on the right hand side of | |||
984 | This is best explained with examples. Here is how you would unpack the first two values from a table: | 1058 | This is best explained with examples. Here is how you would unpack the first two values from a table: |
985 | 1059 | ||
986 | ```moonscript | 1060 | ```moonscript |
987 | thing = {1, 2} | 1061 | thing = [1, 2] |
988 | 1062 | ||
989 | {a, b} = thing | 1063 | [a, b] = thing |
990 | print a, b | 1064 | print a, b |
991 | ``` | 1065 | ``` |
992 | <YueDisplay> | 1066 | <YueDisplay> |
993 | 1067 | ||
994 | <pre> | 1068 | <pre> |
995 | thing = {1, 2} | 1069 | thing = [1, 2] |
996 | 1070 | ||
997 | {a, b} = thing | 1071 | [a, b] = thing |
998 | print a, b | 1072 | print a, b |
999 | </pre> | 1073 | </pre> |
1000 | </YueDisplay> | 1074 | </YueDisplay> |
@@ -1921,6 +1995,19 @@ t = { | |||
1921 | </pre> | 1995 | </pre> |
1922 | </YueDisplay> | 1996 | </YueDisplay> |
1923 | 1997 | ||
1998 | Lua tables have both an array part and a hash part, but sometimes you want to make a semantic distinction between array and hash usage when writing Lua tables. Then you can write Lua table with **[ ]** instead of **{ }** to represent an array table and writing any key value pair in a list table won't be allowed. | ||
1999 | |||
2000 | ```moonscript | ||
2001 | some_values = [ 1, 2, 3, 4 ] | ||
2002 | list_with_one_element = [ 1, ] | ||
2003 | ``` | ||
2004 | <YueDisplay> | ||
2005 | <pre> | ||
2006 | some_values = [ 1, 2, 3, 4 ] | ||
2007 | list_with_one_element = [ 1, ] | ||
2008 | </pre> | ||
2009 | </YueDisplay> | ||
2010 | |||
1924 | ## Comprehensions | 2011 | ## Comprehensions |
1925 | 2012 | ||
1926 | Comprehensions provide a convenient syntax for constructing a new table by iterating over some existing object and applying an expression to its values. There are two kinds of comprehensions: list comprehensions and table comprehensions. They both produce Lua tables; list comprehensions accumulate values into an array-like table, and table comprehensions let you set both the key and the value on each iteration. | 2013 | Comprehensions provide a convenient syntax for constructing a new table by iterating over some existing object and applying an expression to its values. There are two kinds of comprehensions: list comprehensions and table comprehensions. They both produce Lua tables; list comprehensions accumulate values into an array-like table, and table comprehensions let you set both the key and the value on each iteration. |
@@ -1930,12 +2017,12 @@ Comprehensions provide a convenient syntax for constructing a new table by itera | |||
1930 | The following creates a copy of the items table but with all the values doubled. | 2017 | The following creates a copy of the items table but with all the values doubled. |
1931 | 2018 | ||
1932 | ```moonscript | 2019 | ```moonscript |
1933 | items = { 1, 2, 3, 4 } | 2020 | items = [ 1, 2, 3, 4 ] |
1934 | doubled = [item * 2 for i, item in ipairs items] | 2021 | doubled = [item * 2 for i, item in ipairs items] |
1935 | ``` | 2022 | ``` |
1936 | <YueDisplay> | 2023 | <YueDisplay> |
1937 | <pre> | 2024 | <pre> |
1938 | items = { 1, 2, 3, 4 } | 2025 | items = [ 1, 2, 3, 4 ] |
1939 | doubled = [item * 2 for i, item in ipairs items] | 2026 | doubled = [item * 2 for i, item in ipairs items] |
1940 | </pre> | 2027 | </pre> |
1941 | </YueDisplay> | 2028 | </YueDisplay> |
@@ -1969,18 +2056,18 @@ The for and when clauses can be chained as much as desired. The only requirement | |||
1969 | Using multiple for clauses is the same as using nested loops: | 2056 | Using multiple for clauses is the same as using nested loops: |
1970 | 2057 | ||
1971 | ```moonscript | 2058 | ```moonscript |
1972 | x_coords = {4, 5, 6, 7} | 2059 | x_coords = [4, 5, 6, 7] |
1973 | y_coords = {9, 2, 3} | 2060 | y_coords = [9, 2, 3] |
1974 | 2061 | ||
1975 | points = [{x, y} for x in *x_coords \ | 2062 | points = [ [x, y] for x in *x_coords \ |
1976 | for y in *y_coords] | 2063 | for y in *y_coords] |
1977 | ``` | 2064 | ``` |
1978 | <YueDisplay> | 2065 | <YueDisplay> |
1979 | <pre> | 2066 | <pre> |
1980 | x_coords = {4, 5, 6, 7} | 2067 | x_coords = [4, 5, 6, 7] |
1981 | y_coords = {9, 2, 3} | 2068 | y_coords = [9, 2, 3] |
1982 | 2069 | ||
1983 | points = [{x, y} for x in *x_coords \ | 2070 | points = [ [x, y] for x in *x_coords \ |
1984 | for y in *y_coords] | 2071 | for y in *y_coords] |
1985 | </pre> | 2072 | </pre> |
1986 | </YueDisplay> | 2073 | </YueDisplay> |
@@ -2035,12 +2122,12 @@ no_color = {k, v for k, v in pairs thing when k != "color"} | |||
2035 | The **\*** operator is also supported. Here we create a square root look up table for a few numbers. | 2122 | The **\*** operator is also supported. Here we create a square root look up table for a few numbers. |
2036 | 2123 | ||
2037 | ```moonscript | 2124 | ```moonscript |
2038 | numbers = {1, 2, 3, 4} | 2125 | numbers = [1, 2, 3, 4] |
2039 | sqrts = {i, math.sqrt i for i in *numbers} | 2126 | sqrts = {i, math.sqrt i for i in *numbers} |
2040 | ``` | 2127 | ``` |
2041 | <YueDisplay> | 2128 | <YueDisplay> |
2042 | <pre> | 2129 | <pre> |
2043 | numbers = {1, 2, 3, 4} | 2130 | numbers = [1, 2, 3, 4] |
2044 | sqrts = {i, math.sqrt i for i in *numbers} | 2131 | sqrts = {i, math.sqrt i for i in *numbers} |
2045 | </pre> | 2132 | </pre> |
2046 | </YueDisplay> | 2133 | </YueDisplay> |
@@ -2405,15 +2492,6 @@ You can write range checking code with an `in-expression`. | |||
2405 | ```moonscript | 2492 | ```moonscript |
2406 | a = 5 | 2493 | a = 5 |
2407 | 2494 | ||
2408 | if a in [1, 10] | ||
2409 | print "a is in range from 1 to 10" | ||
2410 | |||
2411 | if a not in [1, 10] | ||
2412 | print "a is not in range from 1 to 10" | ||
2413 | |||
2414 | if a in (0, 11) | ||
2415 | print "a is between 0 and 11 with open intervals" | ||
2416 | |||
2417 | if a in {1, 3, 5, 7} | 2495 | if a in {1, 3, 5, 7} |
2418 | print "checking equality with discrete values" | 2496 | print "checking equality with discrete values" |
2419 | 2497 | ||
@@ -2424,15 +2502,6 @@ if a in list | |||
2424 | <pre> | 2502 | <pre> |
2425 | a = 5 | 2503 | a = 5 |
2426 | 2504 | ||
2427 | if a in [1, 10] | ||
2428 | print "a is in range from 1 to 10" | ||
2429 | |||
2430 | if a not in [1, 10] | ||
2431 | print "a is not in range from 1 to 10" | ||
2432 | |||
2433 | if a in (0, 11) | ||
2434 | print "a is between 0 and 11 with open intervals" | ||
2435 | |||
2436 | if a in {1, 3, 5, 7} | 2505 | if a in {1, 3, 5, 7} |
2437 | print "checking equality with discrete values" | 2506 | print "checking equality with discrete values" |
2438 | 2507 | ||
@@ -2649,53 +2718,6 @@ switch item | |||
2649 | </pre> | 2718 | </pre> |
2650 | </YueDisplay> | 2719 | </YueDisplay> |
2651 | 2720 | ||
2652 | ### Range Matching | ||
2653 | |||
2654 | You can do range matching in a switch when clause using `In` expressions. | ||
2655 | |||
2656 | ```moonscript | ||
2657 | value = 5 | ||
2658 | |||
2659 | switch item | ||
2660 | -- range checking with closed interval | ||
2661 | when in [1, 3] | ||
2662 | print "1 <= value <= 3" | ||
2663 | |||
2664 | -- range checking with open and closed interval | ||
2665 | when in (6, 8] | ||
2666 | print "6 < value <= 8" | ||
2667 | |||
2668 | -- not in range checking | ||
2669 | when not in [1, 10) | ||
2670 | print "not (1 <= value < 10)" | ||
2671 | |||
2672 | -- checking discrete values | ||
2673 | when in {11, 21, 99} | ||
2674 | print "value is 11, 21 or 99" | ||
2675 | ``` | ||
2676 | <YueDisplay> | ||
2677 | <pre> | ||
2678 | value = 5 | ||
2679 | |||
2680 | switch item | ||
2681 | -- range checking with closed interval | ||
2682 | when in [1, 3] | ||
2683 | print "1 <= value <= 3" | ||
2684 | |||
2685 | -- range checking with open and closed interval | ||
2686 | when in (6, 8] | ||
2687 | print "6 < value <= 8" | ||
2688 | |||
2689 | -- not in range checking | ||
2690 | when not in [1, 10) | ||
2691 | print "not (1 <= value < 10)" | ||
2692 | |||
2693 | -- checking discrete values | ||
2694 | when in {11, 21, 99} | ||
2695 | print "value is 11, 21 or 99" | ||
2696 | </pre> | ||
2697 | </YueDisplay> | ||
2698 | |||
2699 | ## Object Oriented Programming | 2721 | ## Object Oriented Programming |
2700 | 2722 | ||
2701 | In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the Yuescript code at first, then look into the Lua code if you wish to know the implementation details. | 2723 | In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the Yuescript code at first, then look into the Lua code if you wish to know the implementation details. |
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index 1d234a0..8a236c1 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md | |||
@@ -386,6 +386,80 @@ tb::func! if tb != nil | |||
386 | </pre> | 386 | </pre> |
387 | </YueDisplay> | 387 | </YueDisplay> |
388 | 388 | ||
389 | ### 链式比较 | ||
390 | |||
391 | 您可以在月之脚本中进行比较表达式的链式书写: | ||
392 | |||
393 | ```moonscript | ||
394 | print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 | ||
395 | -- 输出:true | ||
396 | |||
397 | a = 5 | ||
398 | print 1 <= a <= 10 | ||
399 | -- 输出:true | ||
400 | ``` | ||
401 | <YueDisplay> | ||
402 | <pre> | ||
403 | print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 | ||
404 | -- 输出:true | ||
405 | |||
406 | a = 5 | ||
407 | print 1 <= a <= 10 | ||
408 | -- 输出:true | ||
409 | </pre> | ||
410 | </YueDisplay> | ||
411 | |||
412 | 可以注意一下链式比较表达式的求值行为: | ||
413 | |||
414 | ```moonscript | ||
415 | v = (x)-> | ||
416 | print x | ||
417 | x | ||
418 | |||
419 | print v(1) < v(2) <= v(3) | ||
420 | --[[ | ||
421 | 输出: | ||
422 | 2 | ||
423 | 1 | ||
424 | 3 | ||
425 | true | ||
426 | ]] | ||
427 | |||
428 | print v(1) > v(2) <= v(3) | ||
429 | --[[ | ||
430 | 输出: | ||
431 | 2 | ||
432 | 1 | ||
433 | false | ||
434 | ]] | ||
435 | ``` | ||
436 | <YueDisplay> | ||
437 | <pre> | ||
438 | v = (x)-> | ||
439 | print x | ||
440 | x | ||
441 | |||
442 | print v(1) < v(2) <= v(3) | ||
443 | --[[ | ||
444 | 输出: | ||
445 | 2 | ||
446 | 1 | ||
447 | 3 | ||
448 | true | ||
449 | ]] | ||
450 | |||
451 | print v(1) > v(2) <= v(3) | ||
452 | --[[ | ||
453 | 输出: | ||
454 | 2 | ||
455 | 1 | ||
456 | false | ||
457 | ]] | ||
458 | </pre> | ||
459 | </YueDisplay> | ||
460 | |||
461 | 在上面的例子里,中间的表达式`v(2)`仅被计算一次,如果把表达式写成`v(1) < v(2) and v(2) <= v(3)`的方式,中间的`v(2)`才会被计算两次。在链式比较中,求值的顺序往往是未定义的。所以强烈建议不要在链式比较中使用具有副作用(比如做打印操作)的表达式。如果需要使用有副作用的函数,应明确使用短路 `and` 运算符来做连接。 | ||
462 | |||
389 | ### 表追加 | 463 | ### 表追加 |
390 | 464 | ||
391 | **[] =** 操作符用于向Lua表的最后插入值。 | 465 | **[] =** 操作符用于向Lua表的最后插入值。 |
@@ -982,17 +1056,17 @@ do | |||
982 | 最好是通过示例来解释。以下是如何从表格中解包前两个值的方法: | 1056 | 最好是通过示例来解释。以下是如何从表格中解包前两个值的方法: |
983 | 1057 | ||
984 | ```moonscript | 1058 | ```moonscript |
985 | thing = {1, 2} | 1059 | thing = [1, 2] |
986 | 1060 | ||
987 | {a, b} = thing | 1061 | [a, b] = thing |
988 | print a, b | 1062 | print a, b |
989 | ``` | 1063 | ``` |
990 | <YueDisplay> | 1064 | <YueDisplay> |
991 | 1065 | ||
992 | <pre> | 1066 | <pre> |
993 | thing = {1, 2} | 1067 | thing = [1, 2] |
994 | 1068 | ||
995 | {a, b} = thing | 1069 | [a, b] = thing |
996 | print a, b | 1070 | print a, b |
997 | </pre> | 1071 | </pre> |
998 | </YueDisplay> | 1072 | </YueDisplay> |
@@ -1881,6 +1955,19 @@ t = { | |||
1881 | </pre> | 1955 | </pre> |
1882 | </YueDisplay> | 1956 | </YueDisplay> |
1883 | 1957 | ||
1958 | Lua的表同时具有数组部分和哈希部分,但有时候你会希望在书写Lua表时,对Lua表做数组和哈希不同用法的语义区分。然后你可以用 **[ ]** 而不是 **{ }** 来编写表示数组的 Lua 表,并且不允许在数组 Lua 表中写入任何键值对。 | ||
1959 | |||
1960 | ```moonscript | ||
1961 | some_values = [ 1, 2, 3, 4 ] | ||
1962 | list_with_one_element = [ 1, ] | ||
1963 | ``` | ||
1964 | <YueDisplay> | ||
1965 | <pre> | ||
1966 | some_values = [ 1, 2, 3, 4 ] | ||
1967 | list_with_one_element = [ 1, ] | ||
1968 | </pre> | ||
1969 | </YueDisplay> | ||
1970 | |||
1884 | ## 推导式 | 1971 | ## 推导式 |
1885 | 1972 | ||
1886 | 推导式为我们提供了一种便捷的语法,通过遍历现有对象并对其值应用表达式来构造出新的表格。月之脚本有两种推导式:列表推导式和表格推导式。它们最终都是产生Lua表格;列表推导式将值累积到类似数组的表格中,而表格推导式允许您在每次遍历时设置新表格的键和值。 | 1973 | 推导式为我们提供了一种便捷的语法,通过遍历现有对象并对其值应用表达式来构造出新的表格。月之脚本有两种推导式:列表推导式和表格推导式。它们最终都是产生Lua表格;列表推导式将值累积到类似数组的表格中,而表格推导式允许您在每次遍历时设置新表格的键和值。 |
@@ -1890,12 +1977,12 @@ t = { | |||
1890 | 以下操作创建了一个items表的副本,但所有包含的值都翻倍了。 | 1977 | 以下操作创建了一个items表的副本,但所有包含的值都翻倍了。 |
1891 | 1978 | ||
1892 | ```moonscript | 1979 | ```moonscript |
1893 | items = { 1, 2, 3, 4 } | 1980 | items = [ 1, 2, 3, 4 ] |
1894 | doubled = [item * 2 for i, item in ipairs items] | 1981 | doubled = [item * 2 for i, item in ipairs items] |
1895 | ``` | 1982 | ``` |
1896 | <YueDisplay> | 1983 | <YueDisplay> |
1897 | <pre> | 1984 | <pre> |
1898 | items = { 1, 2, 3, 4 } | 1985 | items = [ 1, 2, 3, 4 ] |
1899 | doubled = [item * 2 for i, item in ipairs items] | 1986 | doubled = [item * 2 for i, item in ipairs items] |
1900 | </pre> | 1987 | </pre> |
1901 | </YueDisplay> | 1988 | </YueDisplay> |
@@ -1929,18 +2016,18 @@ for和when子句可以根据需要进行链式操作。唯一的要求是推导 | |||
1929 | 使用多个for子句与使用多重循环的效果相同: | 2016 | 使用多个for子句与使用多重循环的效果相同: |
1930 | 2017 | ||
1931 | ```moonscript | 2018 | ```moonscript |
1932 | x_coords = {4, 5, 6, 7} | 2019 | x_coords = [4, 5, 6, 7] |
1933 | y_coords = {9, 2, 3} | 2020 | y_coords = [9, 2, 3] |
1934 | 2021 | ||
1935 | points = [{x, y} for x in *x_coords \ | 2022 | points = [ [x, y] for x in *x_coords \ |
1936 | for y in *y_coords] | 2023 | for y in *y_coords] |
1937 | ``` | 2024 | ``` |
1938 | <YueDisplay> | 2025 | <YueDisplay> |
1939 | <pre> | 2026 | <pre> |
1940 | x_coords = {4, 5, 6, 7} | 2027 | x_coords = [4, 5, 6, 7] |
1941 | y_coords = {9, 2, 3} | 2028 | y_coords = [9, 2, 3] |
1942 | 2029 | ||
1943 | points = [{x, y} for x in *x_coords \ | 2030 | points = [ [x, y] for x in *x_coords \ |
1944 | for y in *y_coords] | 2031 | for y in *y_coords] |
1945 | </pre> | 2032 | </pre> |
1946 | </YueDisplay> | 2033 | </YueDisplay> |
@@ -1995,12 +2082,12 @@ no_color = {k, v for k, v in pairs thing when k != "color"} | |||
1995 | **\***操作符在表格推导式中能使用。在下面的例子里,我们为几个数字创建了一个平方根查找表。 | 2082 | **\***操作符在表格推导式中能使用。在下面的例子里,我们为几个数字创建了一个平方根查找表。 |
1996 | 2083 | ||
1997 | ```moonscript | 2084 | ```moonscript |
1998 | numbers = {1, 2, 3, 4} | 2085 | numbers = [1, 2, 3, 4] |
1999 | sqrts = {i, math.sqrt i for i in *numbers} | 2086 | sqrts = {i, math.sqrt i for i in *numbers} |
2000 | ``` | 2087 | ``` |
2001 | <YueDisplay> | 2088 | <YueDisplay> |
2002 | <pre> | 2089 | <pre> |
2003 | numbers = {1, 2, 3, 4} | 2090 | numbers = [1, 2, 3, 4] |
2004 | sqrts = {i, math.sqrt i for i in *numbers} | 2091 | sqrts = {i, math.sqrt i for i in *numbers} |
2005 | </pre> | 2092 | </pre> |
2006 | </YueDisplay> | 2093 | </YueDisplay> |
@@ -2361,20 +2448,11 @@ print "你真幸运!" unless math.random! > 0.1 | |||
2361 | 2448 | ||
2362 | ### 范围表达式 | 2449 | ### 范围表达式 |
2363 | 2450 | ||
2364 | 您可以使用范围表达式来编写进行围检查的代码。 | 2451 | 您可以使用范围表达式来编写进行范围检查的代码。 |
2365 | 2452 | ||
2366 | ```moonscript | 2453 | ```moonscript |
2367 | a = 5 | 2454 | a = 5 |
2368 | 2455 | ||
2369 | if a in [1, 10] | ||
2370 | print "a在1到10的范围内" | ||
2371 | |||
2372 | if a not in [1, 10] | ||
2373 | print "a不在1到10的范围内" | ||
2374 | |||
2375 | if a in (0, 11) | ||
2376 | print "a在0到11的开放区间内" | ||
2377 | |||
2378 | if a in {1, 3, 5, 7} | 2456 | if a in {1, 3, 5, 7} |
2379 | print "检查离散值的相等性" | 2457 | print "检查离散值的相等性" |
2380 | 2458 | ||
@@ -2385,15 +2463,6 @@ if a in list | |||
2385 | <pre> | 2463 | <pre> |
2386 | a = 5 | 2464 | a = 5 |
2387 | 2465 | ||
2388 | if a in [1, 10] | ||
2389 | print "a在1到10的范围内" | ||
2390 | |||
2391 | if a not in [1, 10] | ||
2392 | print "a不在1到10的范围内" | ||
2393 | |||
2394 | if a in (0, 11) | ||
2395 | print "a在0到11的开放区间内" | ||
2396 | |||
2397 | if a in {1, 3, 5, 7} | 2466 | if a in {1, 3, 5, 7} |
2398 | print "检查离散值的相等性" | 2467 | print "检查离散值的相等性" |
2399 | 2468 | ||
@@ -2610,53 +2679,6 @@ switch item | |||
2610 | </pre> | 2679 | </pre> |
2611 | </YueDisplay> | 2680 | </YueDisplay> |
2612 | 2681 | ||
2613 | ### 范围匹配 | ||
2614 | |||
2615 | 使用`in`范围匹配表达式,你可以在switch的when子句中进行范围匹配的检查处理。 | ||
2616 | |||
2617 | ```moonscript | ||
2618 | value = 5 | ||
2619 | |||
2620 | switch item | ||
2621 | -- 使用闭区间进行范围检查 | ||
2622 | when in [1, 3] | ||
2623 | print "1 <= value <= 3" | ||
2624 | |||
2625 | -- 使用开闭区间进行范围检查 | ||
2626 | when in (6, 8] | ||
2627 | print "6 < value <= 8" | ||
2628 | |||
2629 | -- 检查不在范围内 | ||
2630 | when not in [1, 10) | ||
2631 | print "不是 (1 <= value < 10)" | ||
2632 | |||
2633 | -- 检查离散值 | ||
2634 | when in {11, 21, 99} | ||
2635 | print "值是 11, 21 或 99" | ||
2636 | ``` | ||
2637 | <YueDisplay> | ||
2638 | <pre> | ||
2639 | value = 5 | ||
2640 | |||
2641 | switch item | ||
2642 | -- 使用闭区间进行范围检查 | ||
2643 | when in [1, 3] | ||
2644 | print "1 <= value <= 3" | ||
2645 | |||
2646 | -- 使用开闭区间进行范围检查 | ||
2647 | when in (6, 8] | ||
2648 | print "6 < value <= 8" | ||
2649 | |||
2650 | -- 检查不在范围内 | ||
2651 | when not in [1, 10) | ||
2652 | print "不是 (1 <= value < 10)" | ||
2653 | |||
2654 | -- 检查离散值 | ||
2655 | when in {11, 21, 99} | ||
2656 | print "值是 11, 21 或 99" | ||
2657 | </pre> | ||
2658 | </YueDisplay> | ||
2659 | |||
2660 | ## 面向对象编程 | 2682 | ## 面向对象编程 |
2661 | 2683 | ||
2662 | 在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果您想知道关于面向对象功能的实现细节,再查看Lua代码。 | 2684 | 在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果您想知道关于面向对象功能的实现细节,再查看Lua代码。 |