This page is no longer maintained — Please continue to the home page at www.scala-lang.org

why does ++i or i++ not work?

14 replies
Aishwarya Singhal
Joined: 2011-09-09,
User offline. Last seen 42 years 45 weeks ago.

hi

I just tried something of curiosity and was surprised - integer
increment operators of the java and c++ world don't seem to work! If I
do a ++i or i++, I get a compilation error. Surely ++ and ++(x:Int)
could be defined as methods in Int (or Predef) easily, why aren't they
there?

On another note, borrowing from the ruby world, it would have been
nice to have a "times()" method on Int so I could say '10 times
println("hello")'. I know you can use a range and foreach but that's a
tad more verbose. What do you think?

Best regards
Aishwarya

roland.kuhn
Joined: 2011-02-21,
User offline. Last seen 35 weeks 3 days ago.
why does ++i or i++ not work?

Hi Aishwarya,

there is nothing stopping you from putting both small features somewhere in scope in your projects as needed. That is the point of Scala: it has all the machinery for being widely extensible without requiring changes to the core (compiler & library). Ideally that would mean that the standard library would be tiny, but that restraint is difficult to keep up ;-)

Regards,

Roland

Sameer Sundresh
Joined: 2011-12-25,
User offline. Last seen 42 years 45 weeks ago.
Re: why does ++i or i++ not work?
On Sun, Dec 25, 2011 at 12:25 AM, Aishwarya Singhal <asinghal79 [at] gmail [dot] com> wrote:
I just tried something of curiosity and was surprised - integer
increment operators of the java and c++ world don't seem to work! If I
do a ++i or i++, I get a compilation error. Surely ++ and ++(x:Int)
could be defined as methods in Int (or Predef) easily, why aren't they
there?

Int is an immutable type, so it wouldn't make sense to define an Int.++() method.  This is discussed on page 8 of "Scala for the Impatient" (http://typesafe.com/resources/scala-for-the-impatient).
On another note, borrowing from the ruby world, it would have been
nice to have a "times()" method on Int so I could say '10 times
println("hello")'. I know you can use a range and foreach but that's a
tad more verbose. What do you think?

How about this?


class IntTimes(n: Int) {
        def times[A](f: () => A): Unit = { 
                for (_ <- 1 to n)
                        f() 
        }
}

implicit def intTimes(n: Int) = new IntTimes(n)

10 times (() => println("hello"))


I admit it would be nice if we could just say println("hello") instead of (() => println("hello")) above, but I can't think of a better solution, since we need a 0-argument function here, so we wouldn't save anything using _ argument shorthand.
Michael Thaler 2
Joined: 2011-12-25,
User offline. Last seen 42 years 45 weeks ago.
Re: why does ++i or i++ not work?
Hi Roland,

there is nothing stopping you from putting both small features somewhere in scope in your projects as needed. That is the point of Scala: it has all the machinery for being widely extensible without requiring changes to the core (compiler & library). Ideally that would mean that the standard library would be tiny, but that restraint is difficult to keep up ;-)

Just curious. Is it really possible to write a function in Scala that does something like

var i = 1
val j = i++

that is, return a value and increment the argument at the same time? Wouldn't it be necessary to pass the int by reference to the function, which you can't do in Scala? Or is there a clever trick to do this?

Merry Christmas,
Michael

Andreas Scheinert
Joined: 2011-02-11,
User offline. Last seen 42 years 45 weeks ago.
Re: why does ++i or i++ not work?

Hi Aishwarya,
stackoverflow is a great source for questions on scala, so yours was
already asked there:
http://stackoverflow.com/questions/4520463/why-no-i-in-scala

regards andreas

On 25 Dez., 09:25, Aishwarya Singhal wrote:
> hi
>
> I just tried something of curiosity and was surprised - integer
> increment operators of the java and c++ world don't seem to work! If I
> do a ++i or i++, I get a compilation error. Surely ++ and ++(x:Int)
> could be defined as methods in Int (or Predef) easily, why aren't they
> there?
>
> On another note, borrowing from the ruby world, it would have been
> nice to have a "times()" method on Int so I could say '10 times
> println("hello")'. I know you can use a range and foreach but that's a
> tad more verbose. What do you think?
>
> Best regards
> Aishwarya

nilskp
Joined: 2009-01-30,
User offline. Last seen 1 year 27 weeks ago.
Re: why does ++i or i++ not work?
On Sun, Dec 25, 2011 at 3:26 AM, Sameer Sundresh <sameer [at] sundresh [dot] org> wrote:
On Sun, Dec 25, 2011 at 12:25 AM, Aishwarya Singhal <asinghal79 [at] gmail [dot] com> wrote:
I just tried something of curiosity and was surprised - integer
increment operators of the java and c++ world don't seem to work! If I
do a ++i or i++, I get a compilation error. Surely ++ and ++(x:Int)
could be defined as methods in Int (or Predef) easily, why aren't they
there?

Int is an immutable type, so it wouldn't make sense to define an Int.++() method.  This is discussed on page 8 of "Scala for the Impatient" (http://typesafe.com/resources/scala-for-the-impatient).
On another note, borrowing from the ruby world, it would have been
nice to have a "times()" method on Int so I could say '10 times
println("hello")'. I know you can use a range and foreach but that's a
tad more verbose. What do you think?

How about this?


class IntTimes(n: Int) {
        def times[A](f: () => A): Unit = { 
                for (_ <- 1 to n)
                        f() 
        }
}

implicit def intTimes(n: Int) = new IntTimes(n)

10 times (() => println("hello"))


I admit it would be nice if we could just say println("hello") instead of (() => println("hello")) above

Change your signature to def times[A](f: => A): Unit and now you can do that. 
Aishwarya Singhal
Joined: 2011-09-09,
User offline. Last seen 42 years 45 weeks ago.
Re: why does ++i or i++ not work?

Hi Andreas

:-) should have seen that, sorry.

OK, so I see this argument about immutability. The 2 arguments in the
SO answer viz. its not FP and its not needed because of flow
constructs, I am not so sure about either of them. Scala supports both
FP and OO so not having something because its not FP sounds a bit odd.
And well, I do actually need to increment a number and writing i = i+1
or i += 1 sounds like going back in time. IMHO, i += 1 challenges the
immutability argument too. I am missing the point on why += would be
supported and ++, or -- not. They have the same side effects, don't
they?

I guess the reason is what Michael pointed out here and Daniel on a
comment in the SO answer, there's no pass by reference so you can
increment but you cant get the expected behaviour of i++ (i.e. return
and then increment). Guess that's just sad :-)

Thanks Sameer for the code, I know I can write implicits, I was
wondering if they should be in the standard lib, but I guess not.

Merry Christmas and happy holidays
Best regards
Aishwarya

On Dec 25, 3:37 pm, Andreas Scheinert
wrote:
> Hi Aishwarya,
> stackoverflow is a great source for questions on scala, so yours was
> already asked there:http://stackoverflow.com/questions/4520463/why-no-i-in-scala
>
> regards andreas
>
> On 25 Dez., 09:25, Aishwarya Singhal wrote:
>
>
>
>
>
>
>
> > hi
>
> > I just tried something of curiosity and was surprised - integer
> > increment operators of the java and c++ world don't seem to work! If I
> > do a ++i or i++, I get a compilation error. Surely ++ and ++(x:Int)
> > could be defined as methods in Int (or Predef) easily, why aren't they
> > there?
>
> > On another note, borrowing from the ruby world, it would have been
> > nice to have a "times()" method on Int so I could say '10 times
> > println("hello")'. I know you can use a range and foreach but that's a
> > tad more verbose. What do you think?
>
> > Best regards
> > Aishwarya

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Re: why does ++i or i++ not work?

Also ++ is the Scala concatenation operator, not unary increment.

Cheers,
V

On Dec 25, 2011 11:37 AM, "Andreas Scheinert" <andreas [dot] scheinert [at] googlemail [dot] com> wrote:
Hi Aishwarya,
stackoverflow is a great source for questions on scala, so yours was
already asked there:
http://stackoverflow.com/questions/4520463/why-no-i-in-scala

regards andreas

On 25 Dez., 09:25, Aishwarya Singhal <asingha [dot] [dot] [dot] [at] gmail [dot] com> wrote:
> hi
>
> I just tried something of curiosity and was surprised - integer
> increment operators of the java and c++ world don't seem to work! If I
> do a ++i or i++, I get a compilation error. Surely ++ and ++(x:Int)
> could be defined as methods in Int (or Predef) easily, why aren't they
> there?
>
> On another note, borrowing from the ruby world, it would have been
> nice to have a "times()" method on Int so I could say '10 times
> println("hello")'. I know you can use a range and foreach but that's a
> tad more verbose. What do you think?
>
> Best regards
> Aishwarya
Björn Herzig
Joined: 2010-05-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: why does ++i or i++ not work?
On 12/25/11 12:20 PM, √iktor Ҡlang wrote:
CANPzfU-_Qk5WcQ-R-erSco40Q3r7Y3N2UKr3uvz_bPoKbfNwWA [at] mail [dot] gmail [dot] com" type="cite">

Also ++ is the Scala concatenation operator, not unary increment.

Cheers,
V

On Dec 25, 2011 11:37 AM, "Andreas Scheinert" <andreas [dot] scheinert [at] googlemail [dot] com" rel="nofollow">andreas [dot] scheinert [at] googlemail [dot] com> wrote:
Hi Aishwarya,
stackoverflow is a great source for questions on scala, so yours was
already asked there:
http://stackoverflow.com/questions/4520463/why-no-i-in-scala

regards andreas

On 25 Dez., 09:25, Aishwarya Singhal <asingha [dot] [dot] [dot] [at] gmail [dot] com" rel="nofollow">asingha [dot] [dot] [dot] [at] gmail [dot] com> wrote:
> hi
>
> I just tried something of curiosity and was surprised - integer
> increment operators of the java and c++ world don't seem to work! If I
> do a ++i or i++, I get a compilation error. Surely ++ and ++(x:Int)
> could be defined as methods in Int (or Predef) easily, why aren't they
> there?
Post/pre increment and decrement operators have been a source for a lot
of programming errors in the past especially when mixing them. Additionally
they don't buy you much. There is not a lot of difference between writing
1++ or 1 + 1. You could argue that ++ is more concise but it just saves you
2 spaces (that you could leave out if you wanted). You can define this operator
yourself by using an implicit:

implicit def inc(a: Int) = new {
  def +++ = a + 1
}

gives rise to an operator that enables you to do this
1+++

I personally think that this is pointless since writing 1 + 1 is clear and
concise, but if you insist in using an operator for this, that would be the
way.

CANPzfU-_Qk5WcQ-R-erSco40Q3r7Y3N2UKr3uvz_bPoKbfNwWA [at] mail [dot] gmail [dot] com" type="cite">
>
> On another note, borrowing from the ruby world, it would have been
> nice to have a "times()" method on Int so I could say '10 times
> println("hello")'. I know you can use a range and foreach but that's a
> tad more verbose. What do you think?
>

implicits also enable you to extend Int to do that:

scala> implicit def liftToTimes(a: Int) = new {
     | def times[A](f: => A) = 1 to a foreach { _ => f }
     | }

scala> 10 times println("hello world!")
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!

Regards,
raichoo


CANPzfU-_Qk5WcQ-R-erSco40Q3r7Y3N2UKr3uvz_bPoKbfNwWA [at] mail [dot] gmail [dot] com" type="cite">
> Best regards
> Aishwarya
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: why does ++i or i++ not work?

maybe the reason lies somewhere within code like this:

public static void main(String[] args) {
int x = 5;
int guessMyValue = - x++ - --x;
System.out.println(guessMyValue);
args = new String [] {"1","2","3"};
for (int i = args.length; 0 <= --i; ) {
System.out.println(args[i]);
}
}

Am 25.12.2011 12:20, schrieb Aishwarya Singhal:
> Hi Andreas
>
> :-) should have seen that, sorry.
>
> OK, so I see this argument about immutability. The 2 arguments in the
> SO answer viz. its not FP and its not needed because of flow
> constructs, I am not so sure about either of them. Scala supports both
> FP and OO so not having something because its not FP sounds a bit odd.
> And well, I do actually need to increment a number and writing i = i+1
> or i += 1 sounds like going back in time. IMHO, i += 1 challenges the
> immutability argument too. I am missing the point on why += would be
> supported and ++, or -- not. They have the same side effects, don't
> they?
>
> I guess the reason is what Michael pointed out here and Daniel on a
> comment in the SO answer, there's no pass by reference so you can
> increment but you cant get the expected behaviour of i++ (i.e. return
> and then increment). Guess that's just sad :-)
>
> Thanks Sameer for the code, I know I can write implicits, I was
> wondering if they should be in the standard lib, but I guess not.
>
> Merry Christmas and happy holidays
> Best regards
> Aishwarya
>
>
> On Dec 25, 3:37 pm, Andreas Scheinert
> wrote:
>> Hi Aishwarya,
>> stackoverflow is a great source for questions on scala, so yours was
>> already asked there:http://stackoverflow.com/questions/4520463/why-no-i-in-scala
>>
>> regards andreas
>>
>> On 25 Dez., 09:25, Aishwarya Singhal wrote:
>>
>>
>>
>>
>>
>>
>>
>>> hi
>>> I just tried something of curiosity and was surprised - integer
>>> increment operators of the java and c++ world don't seem to work! If I
>>> do a ++i or i++, I get a compilation error. Surely ++ and ++(x:Int)
>>> could be defined as methods in Int (or Predef) easily, why aren't they
>>> there?
>>> On another note, borrowing from the ruby world, it would have been
>>> nice to have a "times()" method on Int so I could say '10 times
>>> println("hello")'. I know you can use a range and foreach but that's a
>>> tad more verbose. What do you think?
>>> Best regards
>>> Aishwarya

Aishwarya Singhal
Joined: 2011-09-09,
User offline. Last seen 42 years 45 weeks ago.
Re: why does ++i or i++ not work?

ha ha, you've made your point @HamsterofDeath, thanks :-)

actually, I was just thinking about some code when this thought popped
up. The way I was thinking, ++ would actually save me a line per use,
not just a few spaces :-) i.e. I would increment while I was consuming
the value all in one line. Guess I'll just do a wrapper for now.

Best regards
Aishwarya

On Dec 25, 4:36 pm, HamsterofDeath wrote:
> maybe the reason lies somewhere within code like this:
>
>   public static void main(String[] args) {
>     int x = 5;
>     int guessMyValue = - x++ - --x;
>     System.out.println(guessMyValue);
>     args = new String [] {"1","2","3"};
>     for (int i = args.length; 0 <= --i; ) {
>       System.out.println(args[i]);
>     }
>   }
>
> Am 25.12.2011 12:20, schrieb Aishwarya Singhal:
>
>
>
>
>
>
>
> > Hi Andreas
>
> > :-) should have seen that, sorry.
>
> > OK, so I see this argument about immutability. The 2 arguments in the
> > SO answer viz. its not FP and its not needed because of flow
> > constructs, I am not so sure about either of them. Scala supports both
> > FP and OO so not having something because its not FP sounds a bit odd.
> > And well, I do actually need to increment a number and writing i = i+1
> > or i += 1 sounds like going back in time. IMHO, i += 1 challenges the
> > immutability argument too. I am missing the point on why += would be
> > supported and ++, or -- not. They have the same side effects, don't
> > they?
>
> > I guess the reason is what Michael pointed out here and Daniel on a
> > comment in the SO answer, there's no pass by reference so you can
> > increment but you cant get the expected behaviour of i++ (i.e. return
> > and then increment). Guess that's just sad :-)
>
> > Thanks Sameer for the code, I know I can write implicits, I was
> > wondering if they should be in the standard lib, but I guess not.
>
> > Merry Christmas and happy holidays
> > Best regards
> > Aishwarya
>
> > On Dec 25, 3:37 pm, Andreas Scheinert
> > wrote:
> >> Hi Aishwarya,
> >> stackoverflow is a great source for questions on scala, so yours was
> >> already asked there:http://stackoverflow.com/questions/4520463/why-no-i-in-scala
>
> >> regards andreas
>
> >> On 25 Dez., 09:25, Aishwarya Singhal wrote:
>
> >>> hi
> >>> I just tried something of curiosity and was surprised - integer
> >>> increment operators of the java and c++ world don't seem to work! If I
> >>> do a ++i or i++, I get a compilation error. Surely ++ and ++(x:Int)
> >>> could be defined as methods in Int (or Predef) easily, why aren't they
> >>> there?
> >>> On another note, borrowing from the ruby world, it would have been
> >>> nice to have a "times()" method on Int so I could say '10 times
> >>> println("hello")'. I know you can use a range and foreach but that's a
> >>> tad more verbose. What do you think?
> >>> Best regards
> >>> Aishwarya

roland.kuhn
Joined: 2011-02-21,
User offline. Last seen 35 weeks 3 days ago.
Re: why does ++i or i++ not work?
Hi Michael,

Am Sonntag, 25. Dezember 2011 11:07:09 UTC+1 schrieb Michi:
Hi Roland,

there is nothing stopping you from putting both small features somewhere in scope in your projects as needed. That is the point of Scala: it has all the machinery for being widely extensible without requiring changes to the core (compiler & library). Ideally that would mean that the standard library would be tiny, but that restraint is difficult to keep up ;-)

Just curious. Is it really possible to write a function in Scala that does something like

var i = 1
val j = i++

that is, return a value and increment the argument at the same time? Wouldn't it be necessary to pass the int by reference to the function, which you can't do in Scala? Or is there a clever trick to do this?

Now that you put it this way: the trick I imagined does not actually work (while it is possible to capture symbols in by-name parameters and write to them if they’re vars, this is only possible from within the closure; otherwise you’d have to inspect the byte code and access the var using reflection). So, good point. Would this be easier with macros?

Still, as the OP already said: you can easily roll a wrapper which does the things you want in a nice fashion, and that was my main point.

Regards,

Roland
Simon Ochsenreither
Joined: 2011-07-17,
User offline. Last seen 42 years 45 weeks ago.
Re: why does ++i or i++ not work?
As far as I remember the idea of having ++/-- was eliminated very early, because it would have required special rules in the language and the "feature" itself is not consistent, because i++ or ++i do two things, which is a) confusing and b) inconsistent with the rest of the language.

I remember that there were some discussions about times years ago. Afair the were some problems with it, Googling might help ...
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: why does ++i or i++ not work?
On Sun, Dec 25, 2011 at 06:25, Aishwarya Singhal <asinghal79 [at] gmail [dot] com> wrote:
hi

I just tried something of curiosity and was surprised - integer
increment operators of the java and c++ world don't seem to work! If I
do a ++i or i++, I get a compilation error. Surely ++ and ++(x:Int)
could be defined as methods in Int (or Predef) easily, why aren't they
there?

It is not possible to define them as methods. 
On another note, borrowing from the ruby world, it would have been
nice to have a "times()" method on Int so I could say '10 times
println("hello")'. I know you can use a range and foreach but that's a
tad more verbose. What do you think?

Everyone wanted their own "times" method, so no one got them.
Actually, I think I'm overstating this -- there was only one competing use of times, iirc. The problem was that, once this was suggested, everyone felt they had to put in their own two cents on how it should be done, if it should be done at all, etc, etc. The mass hysteria-like behavior of people on the mailing list convinced Odersky it was better to leave the subject well alone. I'm convinced that if people just left it alone, replying with, at most, a "+1", then paulp's *committed* times method would be left alone and we would have it.
So, there you have it. IMO, we don't have times because everyone who wanted it felt they had to have their say about it instead of being content with what had been provided. 
I hope people remember that in the future.
-- Daniel C. Sobral

I travel to the future all the time.
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: why does ++i or i++ not work?

On another note, borrowing from the ruby world, it would have been
nice to have a "times()" method on Int so I could say '10 times
println("hello")'. I know you can use a range and foreach but that's a
tad more verbose. What do you think?


1 to 10 foreach {_ => println("Hello")}
List.fill(10){println("Hello")}

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland