Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return statements inside object constructors behave inconsistently #325

Open
IsaacOscar opened this issue Feb 18, 2020 · 3 comments
Open

Comments

@IsaacOscar
Copy link
Contributor

Consider the following four methods:

method foo1 {
  object { return "Hello" }
  "Goodbye" }

method foo2 {
  object { {return "Hello"}.apply }
  "Goodbye" }

method foo3 {
  object { return "Hello" }}

method foo4 {
  object { {return "Hello"}.apply }}

I would expect all 4 methods to return "Hello" when called, however this is not the case:

print(foo1) // prints "Goodbye"
print(foo2) // prints "hello"
print(foo3) // prints "a foo3"
print(foo4) // crashes with:
// RequestError: error on line 18 of module test: undefined
//  17: print(foo3)
//  18: print(foo4)
// minigrace: program test exited with code 3.
@apblack
Copy link
Contributor

apblack commented Feb 23, 2020 via email

@IsaacOscar
Copy link
Contributor Author

Hmm good point, all the examples I can think of involve putting a return inside a block (e.g. if (object_construction_went_bad) then { return fallback_object }) but that's technically not directly within the object constructor.

In the same vain there's no real reason to support returns directly within methods, e.g.

foo
return bar
baz // unreachable

You could just delete the baz and return and get the same effect.

Really, there only use is inside blocks.

@apblack
Copy link
Contributor

apblack commented May 15, 2020

If we allow

(object_construction_went_bad) then { return fallback_object }

then it is hard to know (statically) the shape of the object returned from a class. I suppose that we could check that the fallback_object has the same shape as the object under construction ... but this seems a little baroque.

I would be inclined to say that either:

  1. The only exits allowed from an object constructor are executing it to completion, and raising an exception, or
  2. Returning from an object constructor by means of a return statement inside the object constructor's initialization code is permitted, but any method that contains such an object constructor is not fresh.

Either way, a compiler has to detect such a return statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants