View Issue Details

IDProjectCategoryView StatusLast Update
0035347FPCDocumentationpublic2019-04-09 20:59
ReporterThaddy de KoningAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
PlatformallOSallOS Versionall
Product Version3.3.1Product Build41848 
Target VersionFixed in Version 
Summary0035347: the operators inc/dec are not documeted as operators. (as opposed to the procedures inc/dec)
DescriptionAccording to http://wiki.freepascal.org/Operator_overloading inc and dec are operators. I tested that and that is true. It is not in the official documentation (yet).
As compared to the inc/dec procedures, there seems a limitation that you can not specify an amount with a default value. I report that separately.
Steps To Reproduce{$mode objfpc}($H+}
type sometype = type string;
operator inc(var value:sometype):sometype;
begin
end;
Additional InformationSee also forum https://forum.lazarus.freepascal.org/index.php/topic,44982.msg316824.html#msg316824
TagsNo tags attached.
Fixed in Revision1596
FPCOldBugId
FPCTarget
Attached Files

Activities

Michael Van Canneyt

2019-04-09 12:04

administrator   ~0115343

Inc and Dec are not operators. They are procedures or even compiler intrinsics.

But you can indeed define an "operator" for Dec/Inc.

Only god knows why the Delphi engineers chose to define operators for Dec/inc.
Presumably they were on some kind of drugs when they did this. They clearly didn't think it through.

And now we're stuck with the ensuing confusion...

But I will add them to the list of possible 'operators'

Thaddy de Koning

2019-04-09 13:22

reporter   ~0115345

Last edited: 2019-04-09 13:23

View 2 revisions

I know that, Michael, but the compiler really treats them as operators and they really -partially- work! See my other report for context.
There I also suggest to remove that.

The wiki link shows what is expected, but I do not consider the wiki above your documentation, which is almost always correct.
I wonder who implemented that operators for inc/dec or is it an accident?

Thaddy de Koning

2019-04-09 13:26

reporter   ~0115346

IMHO that should be gone...and not in a release.

Michael Van Canneyt

2019-04-09 13:45

administrator   ~0115348

I repeat:

Inc and Dec are not operators. They are procedures, not even functions.
If they were operators you could use them in an expression, but you can't.

Try to compile this:

program testo;

Var
  a,b,C : integer;

begin
  a:=1;
  b:=1;
  c:=Inc(a,b);
  Writeln('C: ',C);
end.

Does not work. Rightly so, since Inc is a procedure...

I know that you can define an "operator" for Inc/Dec, but this is something inherited from Delphi for compatibility reasons.

I suspect the reason Delphi introduced it is that the Delphi compiler has the same backend as C++ builder, where you have the ++/-- and += and -= operators. Inc/Dec are the pascal 'equivalent' of these operators- more or less.

But in C++ an assignment in itself is an expression so ++ += = etc. are actually operators. So allowing people to define a ++ etc. operator is OK in C++

Not so in Pascal, where Inc/Dec are not operators.
Similarly for = and typecasts I suppose.

As said, we're stuck with the confusion.

Thaddy de Koning

2019-04-09 15:06

reporter   ~0115349

Last edited: 2019-04-09 15:09

View 2 revisions

I repeat:
{$mode objfpc}{$H+}
 
Type
  TAZ=String;
 
operator inc(az: TAZ; i: integer) raz:TAZ; // inline;
begin
  result := '';
end;
 
begin
end.

 Works!

This surprires me too... do not dismiss it...

Thaddy de Koning

2019-04-09 16:12

reporter   ~0115351

Even if I am ill I give always compileable examples.

Michael Van Canneyt

2019-04-09 17:10

administrator   ~0115352

Thaddy, what are you trying to prove ?

I didn't say you cannot *define* an Inc/Dec "operator".

On the contrary, I acknowledged that, see my sentence

"I know that you can define an "operator" for Inc/Dec, but this is something inherited from Delphi for compatibility reasons."

I claim that the terminology is confusing, and that Inc/Dec cannot be *used* as an operator in expressions. They are usable as procedures.

Maybe it is logical to you that to make a *procedure call* work for a user-defined type, you need to define an *operator*, but to me it is not.

It would be entirely logical and consistent just to define
Procedure Inc(var origin : TMyType);
Procedure Inc(var origin : TMyType; Term : TMyType);
That would work equally well.

Thaddy de Koning

2019-04-09 18:07

reporter   ~0115355

I know.Compile example.

Michael Van Canneyt

2019-04-09 18:15

administrator   ~0115356

Thaddy, I did that already after your initial post.
I know you can define an Inc operator.

So what am I suposed to see, except that it compiles ?
What do you want to achieve ?

J. Gareth Moreton

2019-04-09 18:50

developer   ~0115357

Last edited: 2019-04-09 18:51

View 2 revisions

I suppose if I had to suggest anything, it would be to remove Inc and Dec from the operator list completely, so if you want to define them for a particular type, you have to do so as a regular overloaded procedure.

Staying compatible with Delphi is commendable, but should that only be enforced with $MODE DELPHI? It might be beneficial to streamline some of the convolution for Free Pascal. That is, remove Inc/Dec from the operator list for modes FPC and OBJFPC, unless that's going to break something.

Akira1364

2019-04-09 19:11

reporter   ~0115358

Last edited: 2019-04-09 19:23

View 7 revisions

I don't think it has anything to do with Delphi at all.

Inc and Dec are definitely literally the equivalent of ++ and --. They're not actually implemented as real methods anywhere, only within the scope of the compiler itself. The procedural definitions of them shown in the docs don't exist.

To say they aren't operators is no different than saying shl and shr are not operators, IMO. All four of those textual identifiers actually originate in assembly language, also.

Lastly the fact you can't use Inc and Dec in expressions is just an implementation detail, and kind of an inconsistent one at that.

FPC has both shl and shr and also << and >>, which compile to the same thing, so there's no reason it could not also have ++ and -- to accompany Inc and Dec.

J. Gareth Moreton

2019-04-09 19:58

developer   ~0115359

There are subtle differences: shl and shr are binary operators like + and -, while Inc and Dec are intrinsics that take an ordinal variable and an optional ordinal parameter. They essentially wrap ++/+= and --/-= into one function respectively.

However, we can't simply turn Inc and Dec into function-like intrinsics because C has prefix and suffix versions of ++ and -- that return different values in an expression. Similarly, Object Pascal does not support the use of := in an expression, and I hope it never does, because that is just asking for bugs to be introduced into people's code.

Michael Van Canneyt

2019-04-09 20:23

administrator   ~0115360

Gareth expresses my views exactly.

The fact that Inc and Dec are "definable" as "operators" is due solely to delphi compatibility, I asked.

They are *usable* only as procedures, which means they can only be used as a statement, which is not an expression. You could perfectly overload them using regular functions for other types than the compiler-supported ones.

Operators by contrast are usable only in expressions.

Hence my view that Inc/Dec are not operators.

Maybe one day their nature will change and they'll become usable in expressions.
But today they are not, and so today they are not operators.

Michael Van Canneyt

2019-04-09 20:59

administrator   ~0115361

Documented Inc/Dec operator definitions, added a word to introduction to specify that 'operator' is used broader than just expressions. Added a section about enumerator operator.

Issue History

Date Modified Username Field Change
2019-04-09 10:01 Thaddy de Koning New Issue
2019-04-09 10:01 Thaddy de Koning Status new => assigned
2019-04-09 10:01 Thaddy de Koning Assigned To => Michael Van Canneyt
2019-04-09 12:04 Michael Van Canneyt Note Added: 0115343
2019-04-09 13:22 Thaddy de Koning Note Added: 0115345
2019-04-09 13:23 Thaddy de Koning Note Edited: 0115345 View Revisions
2019-04-09 13:26 Thaddy de Koning Note Added: 0115346
2019-04-09 13:45 Michael Van Canneyt Note Added: 0115348
2019-04-09 15:06 Thaddy de Koning Note Added: 0115349
2019-04-09 15:09 Thaddy de Koning Note Edited: 0115349 View Revisions
2019-04-09 16:12 Thaddy de Koning Note Added: 0115351
2019-04-09 17:10 Michael Van Canneyt Note Added: 0115352
2019-04-09 18:07 Thaddy de Koning Note Added: 0115355
2019-04-09 18:15 Michael Van Canneyt Note Added: 0115356
2019-04-09 18:50 J. Gareth Moreton Note Added: 0115357
2019-04-09 18:51 J. Gareth Moreton Note Edited: 0115357 View Revisions
2019-04-09 19:11 Akira1364 Note Added: 0115358
2019-04-09 19:16 Akira1364 Note Edited: 0115358 View Revisions
2019-04-09 19:18 Akira1364 Note Edited: 0115358 View Revisions
2019-04-09 19:20 Akira1364 Note Edited: 0115358 View Revisions
2019-04-09 19:21 Akira1364 Note Edited: 0115358 View Revisions
2019-04-09 19:22 Akira1364 Note Edited: 0115358 View Revisions
2019-04-09 19:23 Akira1364 Note Edited: 0115358 View Revisions
2019-04-09 19:58 J. Gareth Moreton Note Added: 0115359
2019-04-09 20:23 Michael Van Canneyt Note Added: 0115360
2019-04-09 20:59 Michael Van Canneyt Fixed in Revision => 1596
2019-04-09 20:59 Michael Van Canneyt Note Added: 0115361
2019-04-09 20:59 Michael Van Canneyt Status assigned => resolved
2019-04-09 20:59 Michael Van Canneyt Resolution open => fixed