AFAIK Inc() and Dec() are built in functions that where, at the time of Turbo Pascal, a faster way to inc/dec ordinals. Now the compiler is smart enough to optimise things anyway.

AFAIK Inc() and Dec() are built in functions that where, at the time of Turbo Pascal, a faster way to inc/dec ordinals. Now the compiler is smart enough to optimise things anyway.

would it be nice to allow them now to Inc/Dec properties ?
Inc(Button1.Tag) will be a shortcut for Button1.Tag := Button1.Tag + 1

Comments

  1. AFAIK, Inc and Dec have been slower than a simole increment for some time now.

    ReplyDelete
  2. "would it be nice to allow them now to Inc/Dec properties ?"
    I'd go further and add the unary operators for increment and decrement:
    Button1.Tag++;
    or
    ++Button1.Tag;

    I see no reason for sticking to Inc/Dec, might be missing something...

    ReplyDelete
  3. just because Inc/Dec have been Pascal keywords for decades, and ++ / -- have C like flavor

    ReplyDelete
  4. might be a valid reason for others, for me, I see C-like languages more productive; we can't stick to the "old ways" when we have better alternatives

    ReplyDelete
  5. Dorin Duminica
    I would prefer: Button.Tag += 1, as already implemented in FreePascal.
    It's more pascalish. ++ is too symbolic too me.

    ReplyDelete
  6. Well Dorin, why do you use Delphi then ? At least you can use C++Builder ?!

    ReplyDelete
  7. Markus Ja that can work too, it's a bit more clearer, but it's all syntax, nothing special, people can get used to it

    Paul TOTH  I haven't touched Delphi in probably half a year by now...

    ReplyDelete
  8. but that's another question, anyway "+=" could be not so weird beside ":=" :)

    and Inc/Dec could be also function like in

    While Inc(Index) < 100 do

    ReplyDelete
  9. Andrea Raimondi Where does it come from? Agner Foq writes that it may occur only in some cases related to reading the carry flag, which is AFAIK never done by the Delphi compiler. See "16.2 INC and DEC" in http://www.agner.org/optimize/optimizing_assembly.pdf

    ReplyDelete
  10. Paul TOTH inc/dec as a function could indeed make sense. But code may be more difficult to debug in some cases. You can already do this using an inlined function - I've seen this in the Delphi RTL.

    ReplyDelete
  11. A. Bouchez​ it is something at the back of my mind. I think it was a senior developer at a company I worked at who said this. I never made tests on this though because I do not have many cases of increments :-)

    ReplyDelete
  12. Andrea Raimondi The compiler should produce the same code for inc(x) and x := x + 1. Do you have evidence that this is no the case?

    ReplyDelete
  13. David Heffernan a long time ago there was a debate about this and I am talking about 20 years ago. I recall that Inc and Dec produced a larger code which executed a bit faster, whereas a simple increment was generating a smaller, but slower, code. I haven't looked into this for a long time, but chances are that references to that debate are still available on the net. When this debate happened, it was a different computing era, so the size of the code mattered just about as much as its speed. This is why I remember it :) I don't know what happened ever since because, as I said, this is not something I use a lot (really, almost not at all).

    A

    ReplyDelete
  14. grabs popcorn I see a "is this syntax pascal'ish enough..." discussion on the horizon.

    ReplyDelete
  15. Dorin Duminica will new language feature bring you back to Delphi anyway ?

    ReplyDelete
  16. Paul TOTH one developer, or one developer's thoughts are pretty much a drop in the ocean OR irrelevant; 
    you asked
    "would it be nice to allow them now to Inc/Dec properties ?"

    I commented
    "I'd go further and add the unary operators for increment and decrement:
    Button1.Tag++;
    or
    ++Button1.Tag;
    "
    did you just get an AV and wanna drop it on me? (;

    ReplyDelete
  17. +Andrea It's not really helpful to spread FUD like this. Get some facts to back up what you say, or say nothing.

    ReplyDelete
  18. Lars Fosdal Nice. So you increase the value you retrieved by the property. Hello value types.

    ReplyDelete
  19. And ambiguity is ambiguous.

    Putting a method on a result of a property does not imply anyhow that it means increasing the value of the property.

    ReplyDelete
  20. IMO, it's just another helper-like syntax.
    Object.Prop.Action;
    Object.List.Clear;
    Object.Value.Increment;
    Object.Value.Trunc;
    but I do see the ambiguity.

    The cleanest is probably
    Inc(Object.Value);

    Efficiencywise, such a construct doesn't really improve anything, as you still need to call getter and setter.

    ReplyDelete
  21. Andrea Raimondi " I recall that Inc and Dec produced a larger code which executed a bit faster, whereas a simple increment was generating a smaller, but slower, code." This is incorrect, even in Pentium 4 times, I'm afraid. In x86 asm, INC/DEC is smaller in code size than ADD/SUB, and "may" be a little slower, about pipelined execution, if the carry flag is checked just after the opcode. It is in all cases, always executed in one CPU cycle, and would always be pipelined in the asm generated by the Delphi compiler. The Agner Fox resources I linked are a good entry point, when you want to know more about asm level.

    ReplyDelete
  22. Lars Fosdal "Efficiencywise, such a construct doesn't really improve anything, as you still need to call getter and setter."

    If there is a getter it should be called with the increased value! If the property is read-only you must not increase the value. It is simple as that: no matter what syntax - it is only sugar.

    ReplyDelete
  23. If it is read only, it shouldn't compile, just like for constants.

    const
      MyConst = Integer(1);
    begin
        Inc(MyConst);
    [dcc32 Error]: E2064 Left side cannot be assigned to

    Calling the getter with the increased value, would make Inc behave differently from prop := prop + 1;

    ReplyDelete
  24. Lars Fosdal "Calling the getter with the increased value, would make Inc behave differently from prop := prop + 1;"

    Eh, huh, what? Read the first post again.

    ReplyDelete
  25. Ditto?  
    Inc must do a SetValue(GetValue + 1);

    ReplyDelete
  26. Lars Fosdal And what does x.Value := x.Value + 1 do?

    ReplyDelete
  27. What I wrote.  Perhaps I misinterpreted your statement "If there is a getter it should be called with the increased value!" as if the field value should be set, bypassing the getter, and the getter then called.

    ReplyDelete
  28. Lars Fosdal "Perhaps I misinterpreted your statement" Indeed you did. I was commenting on your proposal to make it look like a method call and its ambiguity and potential confusion. We already have that problem with properties returning records that have methods. People call the method and then wonder why the internal state change is not present in the backing field (because it was done on the copy that was returned by the getter).

    ReplyDelete
  29. Adding mutating methods to built in simple types like integer would be  a terrible thing to do.

    ReplyDelete
  30. Dorin Duminica 'I see C-like languages more productive' - er, so x++ is 'more productive' than Inc(x). Riiiiiight...

    ReplyDelete
  31. Chris Rolliston the unary inc/dec alone not so much, the overall short-hand calls, yes

    ReplyDelete
  32. +Dorin I don't think ++ or += make C, C++, C#, Objective-C or Java more productive than Delphi. Presence or otherwise has no real bearing on productivity. Personally I'd like to see += and friends. Pre and post increment I'm less keen on. They lead to horrible UB/sequence point issues in C and C++. Even though there's no UB with these ops in C# the code becomes unreadable quite rapidly.

    ReplyDelete
  33. David Heffernan probably not, but it certainly feels like it to me; with respect to pre and post inc/decrement, I've seen fantastic delphi code in both new and legacy apps, it all has to do with how the developer choses to use the language features...

    ReplyDelete
  34. David Heffernan
    Remind me please, what's UB, it doesn't ring a bell.

    ReplyDelete
  35. +Johan there are daily questions on SO asking why x++ + ++x++ gives one value or another. And the answer is that the standard allows such code but does not define what the behaviour is. Anything is permitted to happen.

    Similar to this in Delphi would be the order of evaluation of function arguments. You must not write code that depends on that order because it is not specified.

    ReplyDelete
  36. if possible, i;d better like xxx.inc() than x++.

    ReplyDelete

Post a Comment