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

i++ #41

Open
hakunin opened this issue Jan 13, 2011 · 6 comments
Open

i++ #41

hakunin opened this issue Jan 13, 2011 · 6 comments

Comments

@hakunin
Copy link

hakunin commented Jan 13, 2011

I often find myself in need of i++, for example:

i = 0
rows.each { |row|
  row_class = (i % 2 == 0) ? "even" : "odd"
  html += "..."
  i += 1
}

Writing i += 1 sucks. Also i++ seems to be more expressive because I don't mean to add one, I man to increment.

@headius
Copy link
Member

headius commented Jan 13, 2011

I agree. I have never liked the lack of i++ in Ruby, and I don't buy that it's misleading to make it an alias for "i += 1".

Java itself makes no explicit guarantees about ++ atomicity, making it atomic for local variables (iinc bytecode) and non-atomic for fields or array elements. So for us, making ++ follow the same atomicity guarantees seems quite reasonable.

@baroquebobcat
Copy link
Member

How hard would it be to do this, do we want it in 0.0.8?

@consiliens
Copy link
Contributor

"Comment 3 by rogerpack2005, May 28, 2011
++ !"
http://code.google.com/p/mirah/issues/detail?id=19

@baroquebobcat
Copy link
Member

++ does this currently:

$ mirah -e "i=1;i++"
expected terms before '++' (at line: 1, char: 5)

I think it's because it's not in the grammar yet. I think you change the grammar through this file ( https://github.com/mirah/mirah-parser/blob/master/src/mirahparser/impl/Mirah.mmeta ) in the mirah-parser project.

@felixvf
Copy link
Contributor

felixvf commented May 13, 2017

I’d like to close this as "WONTFIX".

Ruby does not have "i++", Crystal does not have "i++" and

f(i++,a[i++],i++)[i++] = b[i++] + c[i++]

is not well defined.

In addition, unlike some languages close to machine code (C), there is no performance optimization to say "i++". Instead, "i++" will always have to translate to the bytecode of "i+=1".

In addition, there is no clear meaning of "++"-style postfix operators. Should i++ mean the same as i = i.+(1)? If so, should i-- mean the same as i = i.-(1)? If so, should i** mean the same as i = i.*(1)? If so, should i// mean the same as i = i./(1)? If so, should i&& mean the same as i = i.&(1)?

Apparently, it is not useful to extend this concept to operators beyond "++" or "--". But if the concept is not generic enough, is its ungenericness, its peculiarity, its oddity warranted by its usefulness? I doubt that. In C and C++, "++" works on integers and pointers. In Mirah, we do not have pointer arithmetic. And we do not need explicit incrementing as often as in C, as we have #each, #map and other ways of iterating.

So I’m up for closing this.

However, if there is a strong need, I’m open for patches which make postfix "++" and postfix "--" an operator, so users can define their own meaning of "++" by defining a macro int#++ if the really want to.

@baroquebobcat
Copy link
Member

baroquebobcat commented May 13, 2017 via email

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

5 participants