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

Make ** be right associative #9684

Merged
merged 1 commit into from
Oct 5, 2020
Merged

Make ** be right associative #9684

merged 1 commit into from
Oct 5, 2020

Conversation

asterite
Copy link
Member

For https://forum.crystal-lang.org/t/exponentiation-operator-has-unexpected-behaviour/2457

I think it would be better if the associativity is the same as most major languages out there. And we better do this change before 1.0

@bcardiff
Copy link
Member

Can we add a spec to settle also the -2 ** 2 case as explained by https://codeplea.com/exponentiation-associativity-options ?

As of 0.35.1 -2 ** 2 == 4, but in Ruby is -4.

@asterite
Copy link
Member Author

Oh, that's totally unexpected for me, but I guess if most other programming languages, and even Google, work this way it makes sense to do it like that.

@bcardiff
Copy link
Member

I also find it confusing :-). Even if we choose to leave as it is and behave differently from others a spec will settle the choice at least.

@asterite
Copy link
Member Author

I don't know how to make it work :'-(

@asterite
Copy link
Member Author

Feel free to try to make it work. It's already tricky as it is, but it's even more tricky with things like -2 ** 4 being parsed as -(2 ** 4), where -2 is parsed as a single token.

Maybe we can leave things as they are now, but document them well.

@asterite asterite closed this Sep 11, 2020
@asterite asterite deleted the exp-left-associative branch September 11, 2020 18:29
@radha
Copy link

radha commented Sep 12, 2020

Feel free to try to make it work. It's already tricky as it is, but it's even more tricky with things like -2 ** 4 being parsed as -(2 ** 4), where -2 is parsed as a single token.

Maybe we can leave things as they are now, but document them well.

Unary minus has a higher precedence than exponentiation in crystal. So it is expected -2 ** 2 == 4. In ruby the order is unary plus > exponentiaion > unary minus.

I think associativity of exponentiation and precedence of unary minus are separate problems.

@asterite
Copy link
Member Author

Yes, I think they are separate problems too. Should we merge this and tackle the other problem later if needed?

@bcardiff
Copy link
Member

Ok, sure.

@asterite asterite restored the exp-left-associative branch September 12, 2020 14:22
@bcardiff
Copy link
Member

bcardiff commented Oct 5, 2020

@asterite I'm reopening this. I think we can merge this and change the ** associativity to match what most programming languages do as a convention.

Yet, I think the pr and code should be named right associative 🙈 . Left associative is what we have now.

Do you want to do the rename or should I?

@bcardiff bcardiff reopened this Oct 5, 2020
@bcardiff bcardiff added this to the 1.0.0 milestone Oct 5, 2020
@asterite asterite changed the title Make ** be left associative Make ** be right associative Oct 5, 2020
@asterite
Copy link
Member Author

asterite commented Oct 5, 2020

should be named right associative 🙈

Oops! 😄

@asterite
Copy link
Member Author

asterite commented Oct 5, 2020

Ready for another review... I can't change the branch name, though :-P

Copy link
Member

@bcardiff bcardiff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

FYI, I continued the discussion of unary negation in the forum thread.

@asterite asterite merged commit 719d314 into master Oct 5, 2020
@bcardiff bcardiff deleted the exp-left-associative branch October 16, 2020 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants