From a346494922986378d6bc49ded50b095779ec60be Mon Sep 17 00:00:00 2001 From: PureFox48 <64583745+PureFox48@users.noreply.github.com> Date: Fri, 16 Apr 2021 19:09:28 +0100 Subject: [PATCH] Documents 'return from module'. (#976) Fixes #974. --- doc/site/modularity.markdown | 14 ++++++++++++++ test/language/module/returns/module_return.wren | 4 ++++ .../module/returns/module_return_value.wren | 4 ++++ test/language/module/returns/return.wren | 3 +++ .../module/returns/return_from_import.wren | 5 +++++ test/language/module/returns/return_value.wren | 3 +++ .../module/returns/return_value_from_import.wren | 5 +++++ 7 files changed, 38 insertions(+) create mode 100644 test/language/module/returns/module_return.wren create mode 100644 test/language/module/returns/module_return_value.wren create mode 100644 test/language/module/returns/return.wren create mode 100644 test/language/module/returns/return_from_import.wren create mode 100644 test/language/module/returns/return_value.wren create mode 100644 test/language/module/returns/return_value_from_import.wren diff --git a/doc/site/modularity.markdown b/doc/site/modularity.markdown index 5c215056..0d62bb03 100644 --- a/doc/site/modularity.markdown +++ b/doc/site/modularity.markdown @@ -306,5 +306,19 @@ This sounds super hairy, but that's because cyclic dependencies are hairy in general. The key point here is that Wren *can* handle them in the rare cases where you need them. +## Exiting a module early + +Although the `return` statement is normally used to exit from a [method](classes.html#methods) or a [function](functions.html), it can also be used from a module's top-level code to exit the module. For example, if the script consists of a single module, this code would exit the module (and therefore the script) early: + +
+for (i in 1..2) {
+ if (i == 2) return
+ System.print(i) //> prints 1 but not 2
+}
+System.print(3) //> not reached
+
+
+Although it is not invalid to return a value, there is no way to access that value and it is therefore simply discarded.
+