3

Very simple question but I couldn't find an answer on google

In delphi, is there a way to shorten this kind of code:

MyVar := MyVar + X;

Like in C++ I would do MyVar += X;. Given how trivial and useful it is there must be way, but I can't find any option for that anywhere ...

Thanks for any help

6 Answers 6

19

Are you looking for "fast" or short operators? Inc and Dec, as suggested, are the closest in function and length to += and -=, but they are also faster under some circumstances. If you have range checking turned on then they are faster then calling x := x + 1;

Here is the disassembly with range checking turned on, where all variables are a bytes (max value of $ff) for Inc(MyVar, x)

// Inc(MyVar, x);
  add bl, x

And here it is for x := x + 1;

// x := x + 1;
  movzx eax,bl
  movzx edx, x
  add eax,edx
  cmp eax,$000000ff
  jbe success
  call @BoundErr
success:
  mov ebx,eax

You can see the difference, even if there is not a range check failure.

However if you turn on Overflow checking, Inc is still subject to that overhead.

0
6

You could use the Inc command like this:

Inc(MyVar, X);
5

No, you don't. But you can use the

procedure Inc(var X [ ; N: longint ] );
Inc(avar)
Inc(avar, 10)

to increment a variable by N, or

procedure Dec(var X [ ; N: longint ] );
Dec(avar);
Dec(avar, 10);

to decremente a variable by N.

2
Inc(MyVar, X);

won't get any shorter, I fear.

2
  • 6
    Removing that unnecessary space would get it shorter. Nov 1, 2009 at 1:40
  • 4
    That space might not be necessary for the language syntax but I consider it essential for readability :-)
    – Joey
    Nov 1, 2009 at 11:14
1

In delphi, is there a way to shorten this kind of code:

What are you trying to shorten? x = x + 1 and x += 1 generate identical code in languages that support them (for standard types). Since Delphi doesn't have operator overloading, x := x + 1 is an integer or a float operation. The code syntax is just that: syntax. The one is no "faster" than the other.

6
  • When x is an expression (possibly with side effects, but even an expensive property would do), there definitely is a huge difference between x = x + 1 and x += 1 in languages that support it. Nov 1, 2009 at 8:32
  • Read what I said: For standard types. Regardless, Delphi is not one of those languages. Nov 1, 2009 at 15:33
  • 2
    Pavel, there is very little difference, even in languages that support it. Reading from and writing to a property involve completely separate functions, and both must be executed exactly once in either syntax. The difference comes when you have a compound expression like e.x := e.x + 1, where the expression e is expensive to evaluate. In that case, the shorthand makes a difference. Nov 1, 2009 at 20:32
  • When I mentioned properties, I meant things like foo.bar.baz += 1 where bar is an expensive property gettor (and baz could be just a field). And of course baz can be a "standard type", and it doesn't change anything. Nov 2, 2009 at 20:11
  • If bar is an expensive property that is frequently incremented, foo's class should provide a method to do the increment since foo's class can probably bypass the expense of calling the getter at all. Nov 2, 2009 at 21:11
1

'Do and Assign' operators like += and -= aren't part of the Delphi specification language - you'll need to do your incrementing another, likely longer, way.

2
  • 2
    Nothing is part of the Delphi specification. There's no such thing. Nov 1, 2009 at 15:46
  • @ Rob Kennedy - Noted, and fixed. Nov 2, 2009 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.