[nem-en] Increment and decrement operators

Kannan Goundan cakoose at yahoo.com
Mon Feb 27 10:48:42 CET 2006


Wow, that's a surprise.  I guess part of the problem is that
WriteLine isn't properly typed.  I should remember to use the
built-in string interpolation.

I think I can see cases where the implicit semicolon helps, but why
the implicit comma?

--- Kamil Skalski <kamil.skalski at gmail.com> wrote:

> The rule that after } there is implicit ; or , depending on context
> So your ({ x++; x } + {x++; x}) is equivalent to
> ( { x++;  x }, + { x++; x })
> 
> This is (not planned) side effect of making
> if (x) {
>   a
> }
> else
>   b {
> }
> - 5
> 
> mean that we first compute  a / b and then return -5  instead of
> subtracting 5 from a/b
> 
> 
> 2006/2/27, Kannan Goundan <cakoose at yahoo.com>:
> >
> > Why are there both "x++" and "++x" if the expression doesn't
> return a
> > value?  Is it just to avoid syntax errors for those users who use
> one
> > or the other out of habit?
> >
> > Also, I couldn't figure out exactly how the operator behaves.  It
> > seems the commas in a function call are sequence points but the
> part
> > of an infix expression aren't, which gives weird results:
> >
> > class Increment
> > {
> >    static Main() : void
> >    {
> >      mutable x : uint = 1;
> >
> >      x = 1;
> >      System.Console.Write("Val: {0}", {x++; x});
> >      System.Console.WriteLine(", End: {0}", x);
> >
> >      x = 1;
> >      System.Console.Write("Val: {0}", {++x; x});
> >      System.Console.WriteLine(", End: {0}", x);
> >
> >      x = 1;
> >      System.Console.Write("Add: {0}", {x++; x} + {x++; x});
> >      System.Console.WriteLine(", End: {0}", x);
> >
> >      x = 1;
> >      System.Console.Write("Add: {0}", {++x; x} + {++x; x});
> >      System.Console.WriteLine(", End: {0}", x);
> >
> >      x = 1;
> >      System.Console.Write("Add: {0}", Add({x++; x}, {x++; x}));
> >      System.Console.WriteLine(", End: {0}", x);
> >
> >      x = 1;
> >      System.Console.Write("Add: {0}", Add({++x; x}, {++x; x}));
> >      System.Console.WriteLine(", End: {0}", x);
> >    }
> >
> >    static Add(a : uint, b : uint) : uint
> >    {
> >      a + b;
> >    }
> > }
> >
> > -------------------
> > Output:
> >
> > Val: 2, End: 2
> > Val: 2, End: 2
> > Add: 2, End: 3
> > Add: 2, End: 3
> > Add: 5, End: 3
> > Add: 5, End: 3
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com
> >
> > _______________________________________________
> > https://nemerle.org/mailman/listinfo/devel-en
> >
> 
> 
> --
> Kamil Skalski
> http://nazgul.omega.pl
> > _______________________________________________
> https://nemerle.org/mailman/listinfo/devel-en
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the devel-en mailing list