Replies: 2 comments
-
|
Return statements are allowed inside method definitions and result in terminating the execution of the method body and optionally returning a value. As I recall we had a little magic working to make sure returns in blocks execute a return from the methods containing them, e.g., if we have a user-defined control structure like Is there a use-case for allowing returns elsewhere (e.g. in construction code for objects)? I can understand throwing exceptions, but I'm not sure what returns e.g. at the top level of a module, would return to. |
Beta Was this translation helpful? Give feedback.
-
|
I updated your example so that the As you say, we can hardly outlaw exceptions inside object constructors, so we have to give them a semantics. I think that the semantics is clear: the object constructor never answers an object, but instead throws an exception. Hence, the expression executing the object constructor throws an exception, and so on. The semantics of a It seems to me that we have two choices.
We have to do something similar when a block containing a Implementation-wise, minigrace currently implements return using an exception, because JavaScript does not have non-local returns. This almost certainly plays havoc with the V8 optimizer. I have a long-term plan to replace these "return exceptions" with return codes that are checked after every method return. If we decide that the right semantics is (2), I would have to be careful to check these return codes after every JavaScript function call, including those that implement module initialization and object construction, as well as those that implement method request. But it should all be a mere matter of programming … |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Where is a return statement legal in Grace? And what does it mean?
return is clearly allowed inside (any number of nested) blocks inside a method; executing the return returns from the enclosing method (with the return value, if provided, and with
doneif there is no return value).Can return appear in a module, or in another object constructor? If so, what does it mean? Are there other places where we need to say that return can or cannot appear? Directly in a method? Directly in an object constructor (this is minigrace issue #325)? In a block inside an object constructor?
Beta Was this translation helpful? Give feedback.
All reactions