2

I would like to know Delphi equivalent of the following operators.

++i/--i
i++/i--
i+=1/i-=1

I am aware of just Inc(i) for i++ and Dec(i) for i--.

Also under the notion that Inc(i,1) for i+=1 and Dec(i,1) for i-=1.

But have no idea about --i and ++i. Is it supported?

Are my assumptions correct? If not suggest the necessary.

Thanx in advance.

Shirish11
  • 1,587
  • 5
  • 17
  • 39
  • 6
    Actually Inc is neither ++i nor i++, nor is Inc(i, 1) equal to i +=1, because it doesnt evaluate to a value. Same for Dec. – Seth Carnegie Dec 05 '11 at 05:56

3 Answers3

11

Delphi has no equivalents to any of those operators.

It is the case that inc and dec are similar to += and -= but they differ in that the C/C++ versions evaluate to a value.

Whilst in C and C++ you can write

x = a[i++];

this is simply not possible with inc in Delphi. So in Delphi I would write it as

x = a[i];
inc(i);

Having witnessed a seemingly endless supply of questions about the meaning of i++ + ++i + ++i++ I for one am happy that these operators do not exist in Delphi.

On a more serious note, you should be very wary about trying to reproduce such operators using, for example, inline functions. Once you start stringing such operators together into complex expressions you will observe unpredictable behaviour due to the fact that function evaluation order within expressions is undefined in Delphi.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
4

You could use something like this:

FUNCTION PreInc(VAR I : INTEGER) : INTEGER; INLINE; // ++I
  BEGIN
    INC(I);
    Result:=I
  END;

and

FUNCTION PostInc(VAR I : INTEGER) : INTEGER; INLINE; // I++
  BEGIN
    Result:=I;
    INC(I)
  END;

and with various overloads, you could implement the various variations of these C operators.

HeartWare
  • 7,464
  • 2
  • 26
  • 30
  • 2
    Are you still using Modula2 as your reference language, for writing all this in UPPERCASE? :) Just add `inline;` keyword on Delphi 2007+, and you'll have something fast at hand. – Arnaud Bouchez Dec 05 '11 at 06:39
  • @ArnaudBouchez Thank you for your comment my eyes are burning ;) – Olivier Pons Dec 05 '11 at 06:53
  • 1
    @HeartWare: I'd give one up if it were properly indented and with proper lowercase... sorry to say it that way I'm not into politics... – Olivier Pons Dec 05 '11 at 06:54
  • 1
    What is "proper lowercase"? As far as I know, Pascal is not a case-sensitive language, so why should its users be? :) Neither is Pascal a position-dependent language, so what qualifies as "properly indented"? :) Don't judge a book by its cover :-). – HeartWare Dec 05 '11 at 08:26
  • Maybe because of the common [styling](http://edn.embarcadero.com/article/10280) ? I would reject such code as fast as possible ;) – TLama Dec 05 '11 at 11:10
  • 2
    I'm surprised you rolled back my edit. I think people will pay more attention to this if it is in a style that they are familiar with. I personally write code differently at SO from how I do it in my day job. I do that just to make it easier for people to read. I think this answer would attract up votes if it didn't jar so much visually. – David Heffernan Dec 05 '11 at 12:07
  • 2
    Styling goes before content in many cases, so it's more a religious matter than political. I agree with David, make it easy for the reader, follow convention. – LU RD Dec 05 '11 at 12:26
  • Code styling evolves. See to what extent recent RTL and VCL Delphi source code styling are far different from early version ! – menjaraz Dec 05 '11 at 13:47
  • @Arnaud Bouchez et al, this is good ol' 198x style and it is pretty consistent. Complaining about own unfamiliarity is kinda illogical. – OnTheFly Dec 05 '11 at 15:16
  • @David: Two reasons, why I undid your edits. One - the Modula2 comment would appear like nonsense if the text wasn't in the original case. And two - I don't like people trying to force their view of the world down upon other people - especially not without consulting them first. I don't complain about other people's styles (f.ex. putting BEGIN/END where unnecessary, or having a line END ELSE BEGIN on a single line, which I don't personally like). People's coding style differs, and if you can't read a source snippet, then (IMO) it's you that is "faulty", since the compiler understands it fine. – HeartWare Dec 05 '11 at 16:16
  • Fair enough. I don't actually have strong views about style. That's why I try to fit in with what seems to be the prevalent style. Personally I always write end else begin on one line and always use redundant begin end for clarity. But not on Stack Overflow. And I can read your snippet fine, I never said I couldn't. I was just trying to be helpful. On Stack Overflow you have to expect edits to your answers, that's the nature of a wiki. – David Heffernan Dec 05 '11 at 16:33
  • 1
    @David: Just FYI: The "you" was the plural form - not you, personally. – HeartWare Dec 05 '11 at 17:33
  • @HeartWare, I was talking about code rejection, but in case I would review your code, what I'm not doing. I'm just giving the (up)votes here, and you don't get one because of this style. I can't imagine if that code styling and people who follow it doesn't exist. You would get "Hello world" on 10 pages A4 size in 10 size font. And wait, don't you forget to uppercase the `RESULT` :) – TLama Dec 08 '11 at 23:45
-2

An idiomatic delphi style would be to separate the C++ things into separate statements.

If the innermost highest precedent subexpression was a pre-decrement then the equivalent is to make that the first command in a block of commands in delphi.

  Line 1.         --i   --> Dec(i);  
  Line 2.      func(i)  --> func(i);
  Line 3.        x-=1;   --> Dec(x);

The expressiveness of C cannot and should not be reproduced elsewhere. Do not bring your C++ idioms into other languages and try to use them in any other language ever. It's similar to speaking English, and insisting on using phonetic rules that belong to Cantonese. Nobody will understand you and you'll be working in the dark.

Warren P
  • 65,725
  • 40
  • 181
  • 316