From 6eda1c0cb67dca6ada35617faa5abda2146fb209 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Tue, 16 Aug 2022 11:14:14 +0800 Subject: update doc. --- doc/docs/doc/README.md | 64 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) (limited to 'doc') diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index 90cb309..c6b493c 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md @@ -542,6 +542,7 @@ with? io.open "test.txt", "w" ### Piping Instead of a series of nested function calls, you can pipe values with operator **|>**. + ```moonscript "hello" |> print 1 |> print 2 -- insert pipe item as the first argument @@ -560,7 +561,6 @@ readFile "example.txt" "hello" |> print 1 |> print 2 -- insert pipe item as the first argument 2 |> print 1, _, 3 -- pipe with a placeholder - -- pipe expression in multiline readFile "example.txt" |> extract language, {} @@ -571,6 +571,68 @@ readFile "example.txt" +### Backcall + +Backcalls are used for unnesting callbacks. They are defined using arrows pointed to the left as the last parameter by default filling in a function call. All the syntax is mostly the same as regular arrow functions except that it is just pointing the other way and the function body does not require indent. + +```moonscript +<- f +print "hello" +``` + +
+<- f
+print "hello"
+
+
+ +Fat arrow functions are also available. + +```moonscript +<= f +print @value +``` + +
+<= f
+print @value
+
+
+ +You can specify a placeholder for where you want the backcall function to go as a parameter. + +```moonscript +(x) <- map _, {1, 2, 3} +x * 2 +``` + +
+(x) <- map _, {1, 2, 3}
+x * 2
+
+
+ +If you wish to have further code after your backcalls, you can set them aside with a do statement. + +```moonscript +result, msg = do + (data) <- readAsync "filename.txt" + print data + (info) <- processAsync data + check info +print result, msg +``` + +
+result, msg = do
+  (data) <- readAsync "filename.txt"
+  print data
+  (info) <- processAsync data
+  check info
+print result, msg
+
+
+ ### Nil Coalescing The nil-coalescing operator **??** returns the value of its left-hand operand if it isn't **nil**; otherwise, it evaluates the right-hand operand and returns its result. The **??** operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-nil. -- cgit v1.2.3-55-g6feb