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

Implement postfix:<++> and postfix:<--> #122

Closed
masak opened this issue Mar 7, 2016 · 4 comments
Closed

Implement postfix:<++> and postfix:<--> #122

masak opened this issue Mar 7, 2016 · 4 comments

Comments

@masak
Copy link
Owner

masak commented Mar 7, 2016

After we implement #119, there would be little reason not to have postfix:<++> and postfix:<-->.

Not only that, our ability to construct these as macros would be a good showcase for macros and storage locations/lvalues. Because we'd expect something like

some_array[19]++;

to work, and that kind of presupposes that we think in terms of storage locations instead of (as is the case in many places in the code right now) in terms of identifiers.

@masak
Copy link
Owner Author

masak commented Feb 17, 2018

I'd just like to take this opportunity to point out that the introduction of something like postfix:<++> into the language — even as a macro — means we can no longer stay agnostic on the topic of sequence points.

Something like this:

my i = 0;
say(i++ + i);    # 0? or 1? Or sometimes one and sometimes the other? Or implementation-dependent?

I say that bit of code should print 1, and I suggest the reason for that should be that 007 has sequence points. The way Java does it is fine.

Actually, this business is not specific to macros that change stuff around:

my i = 0;
sub postinc() { i = i + 1; return i - 1 }
say(postinc() + i);    # same alternatives as above

With sequence points we basically mean that the compiler does not have the freedom to re-order the evaluation of operands or arguments. (At least not unless it knows it can get away with it.)

Java specifies sequence points in a way I find intuitively satisfying. We might want to have some wording on that, too.

@masak
Copy link
Owner Author

masak commented Jun 24, 2018

I'd just like to take this opportunity to point out that the introduction of something like postfix:<++> into the language — even as a macro — means we can no longer stay agnostic on the topic of sequence points.

Actually, the damage is already done with infix:<=>. Which makes sense, because the most straightforward implementation of x++ is (x = x + 1) - 1, with adequate handwaving.

Actually * 2, the damage is already done with functions. I hear functions can have side effects. 😛


I really stopped by here to say that if we make assignment a statement (#279), then maybe postfix:<++> should be one too. Not because it has to, but for consistency.

If that also means people rely a little bit less on the value of postfix:<++>, or at least write code that's a little bit less contorted, then I won't shed tears over that.

I hear x++ is a statement form in Go.

@masak
Copy link
Owner Author

masak commented Aug 8, 2018

macro prefix:<++>(expr) {
    quasi {
        my L = lvalue({{{expr}}});
        my value = L.read() + 1;
        L.write(value);
        value;
    }
}

macro postfix:<++>(expr) {
    quasi {
        my L = lvalue({{{expr}}});
        my value = L.read();
        ++{{{expr}}};
        value;
    }
}

@masak
Copy link
Owner Author

masak commented Feb 21, 2019

Implemented in #477.

@masak masak closed this as completed Feb 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant