diff options
author | Li Jin <dragon-fly@qq.com> | 2025-05-26 11:07:38 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2025-05-26 11:07:38 +0800 |
commit | a91135ce512f907ed085d9aac147d8fcad356406 (patch) | |
tree | e53408fe0a88ef71ea33d14bcb0b6eeb3a344810 /doc/docs | |
parent | 4ba4c90e711c6204aa40e38347c5a5a076d9370e (diff) | |
download | yuescript-a91135ce512f907ed085d9aac147d8fcad356406.tar.gz yuescript-a91135ce512f907ed085d9aac147d8fcad356406.tar.bz2 yuescript-a91135ce512f907ed085d9aac147d8fcad356406.zip |
Added assignment expression for switch syntax.
Diffstat (limited to 'doc/docs')
-rwxr-xr-x | doc/docs/doc/README.md | 16 | ||||
-rwxr-xr-x | doc/docs/zh/doc/README.md | 16 |
2 files changed, 14 insertions, 18 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index 58b37c0..7e9b454 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
@@ -2696,28 +2696,26 @@ reader\parse_line! until reader\eof! | |||
2696 | 2696 | ||
2697 | ## Switch | 2697 | ## Switch |
2698 | 2698 | ||
2699 | The switch statement is shorthand for writing a series of if statements that check against the same value. Note that the value is only evaluated once. Like if statements, switches can have an else block to handle no matches. Comparison is done with the == operator. | 2699 | The switch statement is shorthand for writing a series of if statements that check against the same value. Note that the value is only evaluated once. Like if statements, switches can have an else block to handle no matches. Comparison is done with the == operator. In switch statement, you can also use assignment expression to store temporary variable value. |
2700 | 2700 | ||
2701 | ```moonscript | 2701 | ```moonscript |
2702 | name = "Dan" | 2702 | switch name := "Dan" |
2703 | switch name | ||
2704 | when "Robert" | 2703 | when "Robert" |
2705 | print "You are Robert" | 2704 | print "You are Robert" |
2706 | when "Dan", "Daniel" | 2705 | when "Dan", "Daniel" |
2707 | print "Your name, it's Dan" | 2706 | print "Your name, it's Dan" |
2708 | else | 2707 | else |
2709 | print "I don't know about your name" | 2708 | print "I don't know about you with name #{name}" |
2710 | ``` | 2709 | ``` |
2711 | <YueDisplay> | 2710 | <YueDisplay> |
2712 | <pre> | 2711 | <pre> |
2713 | name = "Dan" | 2712 | switch name := "Dan" |
2714 | switch name | ||
2715 | when "Robert" | 2713 | when "Robert" |
2716 | print "You are Robert" | 2714 | print "You are Robert" |
2717 | when "Dan", "Daniel" | 2715 | when "Dan", "Daniel" |
2718 | print "Your name, it's Dan" | 2716 | print "Your name, it's Dan" |
2719 | else | 2717 | else |
2720 | print "I don't know about your name" | 2718 | print "I don't know about you with name #{name}" |
2721 | </pre> | 2719 | </pre> |
2722 | </YueDisplay> | 2720 | </YueDisplay> |
2723 | 2721 | ||
@@ -3528,13 +3526,13 @@ In this usage, with can be seen as a special form of the K combinator. | |||
3528 | The expression in the with statement can also be an assignment, if you want to give a name to the expression. | 3526 | The expression in the with statement can also be an assignment, if you want to give a name to the expression. |
3529 | 3527 | ||
3530 | ```moonscript | 3528 | ```moonscript |
3531 | with str = "Hello" | 3529 | with str := "Hello" |
3532 | print "original:", str | 3530 | print "original:", str |
3533 | print "upper:", \upper! | 3531 | print "upper:", \upper! |
3534 | ``` | 3532 | ``` |
3535 | <YueDisplay> | 3533 | <YueDisplay> |
3536 | <pre> | 3534 | <pre> |
3537 | with str = "Hello" | 3535 | with str := "Hello" |
3538 | print "original:", str | 3536 | print "original:", str |
3539 | print "upper:", \upper! | 3537 | print "upper:", \upper! |
3540 | </pre> | 3538 | </pre> |
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index 1dd59a7..da90fa6 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md | |||
@@ -2655,28 +2655,26 @@ reader\parse_line! until reader\eof! | |||
2655 | 2655 | ||
2656 | ## switch 语句 | 2656 | ## switch 语句 |
2657 | 2657 | ||
2658 | switch语句是为了简化检查一系列相同值的if语句而提供的简写语法。要注意用于比较检查的目标值只会计算一次。和if语句一样,switch语句在最后可以接一个else代码块来处理没有匹配的情况。在生成的Lua代码中,进行比较是使用==操作符完成的。 | 2658 | switch语句是为了简化检查一系列相同值的if语句而提供的简写语法。要注意用于比较检查的目标值只会计算一次。和if语句一样,switch语句在最后可以接一个else代码块来处理没有匹配的情况。在生成的Lua代码中,进行比较是使用==操作符完成的。switch语句中也可以使用赋值表达式来储存临时变量值。 |
2659 | 2659 | ||
2660 | ```moonscript | 2660 | ```moonscript |
2661 | name = "Dan" | 2661 | switch name := "Dan" |
2662 | switch name | ||
2663 | when "Robert" | 2662 | when "Robert" |
2664 | print "你是Robert" | 2663 | print "你是Robert" |
2665 | when "Dan", "Daniel" | 2664 | when "Dan", "Daniel" |
2666 | print "你的名字是Dan" | 2665 | print "你的名字是Dan" |
2667 | else | 2666 | else |
2668 | print "我不知道你的名字" | 2667 | print "我不认识你,你的名字是#{name}" |
2669 | ``` | 2668 | ``` |
2670 | <YueDisplay> | 2669 | <YueDisplay> |
2671 | <pre> | 2670 | <pre> |
2672 | name = "Dan" | 2671 | switch name := "Dan" |
2673 | switch name | ||
2674 | when "Robert" | 2672 | when "Robert" |
2675 | print "你是Robert" | 2673 | print "你是Robert" |
2676 | when "Dan", "Daniel" | 2674 | when "Dan", "Daniel" |
2677 | print "你的名字是Dan" | 2675 | print "你的名字是Dan" |
2678 | else | 2676 | else |
2679 | print "我不知道你的名字" | 2677 | print "我不认识你,你的名字是#{name}" |
2680 | </pre> | 2678 | </pre> |
2681 | </YueDisplay> | 2679 | </YueDisplay> |
2682 | 2680 | ||
@@ -3484,13 +3482,13 @@ me = create_person "Leaf", [dad, mother, sister] | |||
3484 | 如果你想给表达式另外起一个名称的话,with语句中的表达式也可以是一个赋值语句。 | 3482 | 如果你想给表达式另外起一个名称的话,with语句中的表达式也可以是一个赋值语句。 |
3485 | 3483 | ||
3486 | ```moonscript | 3484 | ```moonscript |
3487 | with str = "你好" | 3485 | with str := "你好" |
3488 | print "原始:", str | 3486 | print "原始:", str |
3489 | print "大写:", \upper! | 3487 | print "大写:", \upper! |
3490 | ``` | 3488 | ``` |
3491 | <YueDisplay> | 3489 | <YueDisplay> |
3492 | <pre> | 3490 | <pre> |
3493 | with str = "你好" | 3491 | with str := "你好" |
3494 | print "原始:", str | 3492 | print "原始:", str |
3495 | print "大写:", \upper! | 3493 | print "大写:", \upper! |
3496 | </pre> | 3494 | </pre> |