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

Whitespace bugs? #1171

Open
andydude opened this issue Jul 28, 2023 · 4 comments · May be fixed by #1173
Open

Whitespace bugs? #1171

andydude opened this issue Jul 28, 2023 · 4 comments · May be fixed by #1173

Comments

@andydude
Copy link

I only started using Wren yesterday, so I might have missed something in the docs. I was experimenting with OOP in Wren and I couldn't understand why all of the properties were always null! So I made a simple test case, and managed to figure out that it was the white-space that was causing the issue. I boiled it down to this code:

var b = Box.new(2)
System.print(b.x)

When this code is run with Box1, it works as expected, it prints 2. When this code is run with Box2, then it prints null. What exactly is happening here? Shouldn't Box2 work exactly the same as Box1?

Box 1

class Box1 {
    x { _x }
    x=(value) { _x = value }
    construct new(x) { _x = x }
}

Box 2

class Box2 {
    x { 
        _x 
    }
    
    x=(value) { 
        _x = value 
    }
    
    construct new(x) {
        _x = x
    }
}
@ruby0x1
Copy link
Member

ruby0x1 commented Jul 28, 2023

So the gotcha here is that "short hand return" is only valid if it's a single line.

x { _x } will return the contents of _x but the following will return null, which is the default in a method if nothing is returned by the end.

x {
  _x
  //implicit return null
}

what you're looking for is like this: because it's not a single line return shortcut, it must use the return keyword (otherwise it's just a "put the _x value on the stack and do nothing with it")

x {
  return _x
}

@ruby0x1
Copy link
Member

ruby0x1 commented Jul 28, 2023

the inverse is also invalid, e.g x { return _x } is an error because the return isn't expected in a single line early return like that ✨

@andydude
Copy link
Author

Thank you for clarification.

@PureFox48
Copy link
Contributor

This 'shorthand return' behavior is actually documented for functions but not (AFAICS) for methods.

I'll do a PR to fix that.

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

Successfully merging a pull request may close this issue.

3 participants