views:

377

answers:

6

Hello,

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  A: 

You could use the Inc command like this:

Inc(MyVar, X);
andyp
+2  A: 
Inc(MyVar, X);

won't get any shorter, I fear.

Joey
Removing that unnecessary space would get it shorter.
Peter Boughton
That space might not be necessary for the language syntax but I consider it essential for readability :-)
Joey
+1  A: 

'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.

Charlie Salts
Nothing is part of the Delphi specification. There's no such thing.
Rob Kennedy
@ Rob Kennedy - Noted, and fixed.
Charlie Salts
+5  A: 

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.

eKek0
+15  A: 

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.

Jim McKeeth
+1  A: 

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.

jmucchiello
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.
Pavel Minaev
Read what I said: For standard types. Regardless, Delphi is not one of those languages.
jmucchiello
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.
Rob Kennedy
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.
Pavel Minaev
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.
jmucchiello
Pavel: I don't see that. Some older compilers might find += easier to optimize, but since they are equivalent the same according as per standard, it shouldn't matter.
Marco van de Voort