You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[[ ReturnValue ]] Provide new return forms to return values and errors
This patch adds two new forms of the 'return' command:
return value <value>
return error <value>
These forms act slightly differently, depending on whether the handler
calling them is a command or a function handler.
If the handler performing the return is a command handler then:
The 'return value' form, sets 'it' in the caller to <value> and
sets 'the result' to empty.
The 'return error' form, sets 'it' in the caller to empty and
sets 'the result' to <value>.
If the handler performingthe return is a function handler then:
The 'return value' form, returns <value> as the return value of
the function call and sets 'the result' to empty.
The 'return error' form, returns empty as the return value of
the function call and sets 'the result' to <value>.
These forms of return are designed to be used by script library
functions to allow them to have the same ability as built-in engine
functions and commands - namely the ability to return a value (in it
for commands, or return value for functions) *or* return a status
(in the result).
Summary: Stops the current <handler> and <return|returns> a <value> to the <handler> that <call|called> the current <handler>. Sets the result to the value as well
7
+
Summary: Stops the current <handler> and <return|returns> a <value> to the
8
+
<handler> that <call|called> the current <handler>.
8
9
9
10
Introduced: 1.0
10
11
12
+
Changed: 8.1.0
13
+
11
14
OS: mac,windows,linux,ios,android
12
15
13
16
Platforms: desktop,server,web,mobile
14
17
15
-
Parameters:
16
-
value:
17
-
handler: The name of the handler in which the return control structure appears.
18
+
Example:
19
+
function simpleFunction
20
+
return 1
21
+
end simpleFunction
22
+
on testSimpleFunction
23
+
local tVar
24
+
put simpleFunction() into tVar
25
+
-- tVar contains 1, the result contains 1
26
+
end testSimpleFunction
27
+
28
+
Example:
29
+
function simpleFunction
30
+
return value 1
31
+
end simpleFunction
32
+
on testSimpleFunction
33
+
local tVar
34
+
put simpleFunction() into tVar
35
+
-- tVar contains 1, the result contains empty
36
+
end testSimpleFunction
37
+
38
+
Example:
39
+
function simpleFunction
40
+
return error 1
41
+
end simpleFunction
42
+
on testSimpleFunction
43
+
local tVar
44
+
put simpleFunction() into tVar
45
+
-- tVar contains empty, the result contains 1
46
+
end testSimpleFunction
47
+
48
+
Example:
49
+
command simpleCommand
50
+
return 1
51
+
end simpleCommand
52
+
on testSimpleCommand
53
+
simpleCommand
54
+
-- the result contains 1
55
+
end testSimpleCommand
56
+
57
+
Example:
58
+
command simpleCommand
59
+
return value 1
60
+
end simpleCommand
61
+
on testSimpleCommand
62
+
simpleCommand
63
+
-- it contains 1, the result contains empty
64
+
end testSimpleCommand
65
+
66
+
Example:
67
+
command simpleCommand
68
+
return error 1
69
+
end simpleCommand
70
+
on testSimpleCommand
71
+
simpleCommand
72
+
-- it contains empty, the result contains 1
73
+
end testSimpleCommand
18
74
19
-
The result: The <return> control structure set <the result> to the value being returned. If the <return> <control structure> is within an <on> or <setProp> <control structure>, the <value> can be retrieved by checking the <result> <function> in the <caller|calling handler>. Usually, when the <return> <control structure> is used within an <on> or <setProp> <control structure>, it <return(glossary)|returns> an error message. (If you want a <handler> to compute a <value> as its main reason for existence, you should implement it as a custom function rather than a custom command.). >*Note:* As well as the <return> <control structure> returns the value to the caller, it sets <the result> to the value.
75
+
Parameters:
76
+
value: The value to return to the calling handler.
20
77
21
78
Description:
22
-
Use the <return> <control structure> to <return(constant)> a <value> from a custom function or <getProp> <handler>, or to <return(constant)> an error message from a <message handler> or <setProp> <handler>.
79
+
Use the <return> <control structure> to <return(constant)> a <value> from a
80
+
custom function or <getProp> <handler>, or to <return(constant)> an error
81
+
message from a <message handler> or <setProp> <handler>.
23
82
24
83
Form:
25
-
The <return> <statement> appears on a line by itself, anywhere inside a <handler>.
26
-
27
-
When the <return> <control structure> is <execute|executed>, any remaining <statement|statements> in the <handler> are skipped. Hence, the <return> <control structure> is usually used either at the end of a <handler> or within an <if> <control structure>.
28
-
29
-
If the <return> <control structure> is within a <function> or <getProp> <control structure>, the <value> is returned to the <caller|calling handler> as the <function> <value> or <property> setting. For example, if you have the following <function> <handler> :
30
-
31
-
which is called in the following statement:
32
-
33
-
then 1, the value returned by "simpleFunction", is placed in the field.
34
-
35
-
When a message handler executes a <return> <statement>, the <message> stops and is not <pass|passed> to the next <object(glossary)> in the <message path>. To halt the current <message handler> and <pass> the <message> on through the <message path>, use the <pass> <control structure> instead. To halt the current <handler> without returning a result, use the <exit> <control structure> instead.
36
-
37
-
>*Note:* The <return> <control structure> is implemented internally as a <command> and appears in the <commandNames>.
38
-
39
-
40
-
References: object (glossary), return (constant), result (function), commandNames (function), value (function), merge (function), the result (function), message handler (glossary), return (glossary), call (glossary), property (glossary), pass (glossary), execute (glossary), command (glossary), control structure (glossary), message path (glossary), caller (glossary), message (glossary), statement (glossary), handler (glossary), setProp (control structure), getProp (control structure), throw (control structure), if (control structure), pass (control structure), exit (control structure), on (control structure), function (control structure)
84
+
The <return> <statement> appears on a line by itself, anywhere inside a
85
+
<handler>.
86
+
87
+
When the <return> <control structure> is <execute|executed>, any remaining
88
+
<statement|statements> in the <handler> are skipped. Hence, the <return>
89
+
<control structure> is usually used either at the end of a <handler> or within
90
+
an <if> <control structure>.
91
+
92
+
The plain form of the <return> <control structure> always sets <the result> to
93
+
<value>. Additionally, if it is used within a <function> or <getProp> <handler>
94
+
then <value> is passed to the calling handler as the return value of the
95
+
<function>, or value of the property.
96
+
97
+
The value form of the <return> <control structure> always sets <the result> to
98
+
empty. If it is used within a <command> or <message> handler then the <it>
99
+
variable in the calling handler will be set to <value>. If it is used within a
100
+
<function> handler, then <value> is passed to the calling handler as the return
101
+
value of the <function>, or value of the property.
102
+
103
+
The error form of the <return> <control structure> sets <the result> to <value>.
104
+
If it is used within a <command> or <message> handler, then the <it> variable
105
+
in the calling handler will be set to empty. If it is used within a <function>
106
+
handler, then the return value of the <function> or the value of the property is
107
+
returned as empty.
108
+
109
+
*Note:* The value and error forms can only be used within message, command and
110
+
function handlers - not getProp or setProp handlers.
111
+
112
+
When a message handler executes a <return> <statement>, the <message> stops and
113
+
is not <pass|passed> to the next <object(glossary)> in the <message path>. To
114
+
halt the current <message handler> and <pass> the <message> on through the
115
+
<message path>, use the <pass> <control structure> instead. To halt the current
116
+
<handler> without returning a value, use the <exit> <control structure>
117
+
instead.
118
+
119
+
The <return error> and <return value> forms of the <return> command allow much
120
+
easier scripting of the distinction between a return value of a handler call,
121
+
and an error return value of a handler call. If a command or function <handler>
122
+
succeeds, then the 'return value' form should be used to return the result of
123
+
the execution to the calling <handler>. If a command or function <handler> fails,
124
+
then the 'return error' form should be used to return an error status to the
125
+
calling <handler>.
126
+
127
+
Any callers of handlers using the new error and value forms of the <return>
128
+
command can uniformly check <the result> is empty to determine if the
129
+
operation succeeded, and if it did then either <it> or the return value can be
130
+
processed to continue operation.
131
+
132
+
>*Note:* The <return> <control structure> is implemented internally as a
133
+
<command> and appears in the <commandNames>.
134
+
135
+
References: object (glossary), return (constant), result (function),
136
+
commandNames (function), value (function), merge (function),
137
+
the result (function), message handler (glossary), return (glossary),
0 commit comments