Recent

Author Topic: [SOLVED] Declare operator in Delphi mode  (Read 1935 times)

Okoba

  • Hero Member
  • *****
  • Posts: 533
[SOLVED] Declare operator in Delphi mode
« on: November 12, 2020, 11:12:00 am »
How can I declare an operator in Delphi mode?
in ObjFPC it is like this:
Code: Pascal  [Select][+][-]
  1.   operator := (r: Real) z: complex;
  2.   begin
  3.     z.re := r;
  4.     z.im := 0.0;
  5.   end;      

As documented here, I should use the operator name like this:
Code: Pascal  [Select][+][-]
  1.   operator assign (r: Real) z: complex;
  2.   begin
  3.     z.re := r;
  4.     z.im := 0.0;
  5.   end;

So why the compiler gives error on this code?
Quote
project1.lpr(12,12) Fatal: Syntax error, "=" expected but "identifier ASSIGN" found
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$MODE Delphi}
  4.  
  5.  
  6. type
  7.   complex = record
  8.     re: Real;
  9.     im: Real;
  10.   end;
  11.  
  12.   operator assign (r: Real) z: complex;
  13.   begin
  14.     z.re := r;
  15.     z.im := 0.0;
  16.   end;
  17.  
  18. var
  19.   R: Real;
  20.   C: complex;
  21.  
  22. begin
  23.   C := R;
  24. end.            

It is also not possible to declare operators for simple types like integer or string in Delphi mode.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode delphi}
  4.  
  5.   operator := (a: Integer) s: String; //Error
  6.   begin
  7.   end;
  8.  
  9. begin
  10. end.                        
« Last Edit: November 16, 2020, 09:43:22 am by OkobaPatino »

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Declare operator in Delphi mode
« Reply #1 on: November 12, 2020, 11:23:58 am »
See https://wiki.freepascal.org/Operator_overloading

It states: "In {$mode Delphi}, operator overloading can only be done in the context of classes or advanced records "

Your type complex is neither.

Bart

Okoba

  • Hero Member
  • *****
  • Posts: 533
Re: Declare operator in Delphi mode
« Reply #2 on: November 12, 2020, 11:34:56 am »
I saw this, but I thought the actual FPC document is the true reference, and I could not find that statement in there.
So there is no way to declare an operator for a simple type like integer in Delphi mode? Just for records and classes and in their context?

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Declare operator in Delphi mode
« Reply #3 on: November 12, 2020, 11:41:27 am »
AFAIK not, because that would be Delphi incompatible.
So, either make it a class or an advanced record, or use mode objfpc, where you can benefit from the ease of overloading operators (we are better than Delphi!).

Bart

Okoba

  • Hero Member
  • *****
  • Posts: 533
Re: Declare operator in Delphi mode
« Reply #4 on: November 12, 2020, 11:46:11 am »
I like to use objfpc but it has its own problems like needing specializing every generic method for every use: https://forum.lazarus.freepascal.org/index.php/topic,52007

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Declare operator in Delphi mode
« Reply #5 on: November 12, 2020, 12:01:16 pm »
Make it an advanced record then, the operators will be part of the type definition IIRC.

Bart

Okoba

  • Hero Member
  • *****
  • Posts: 533
Re: Declare operator in Delphi mode
« Reply #6 on: November 12, 2020, 12:03:41 pm »
Yes, if it is a record, I can do that. But for simple types like integer or string, it is not possible.
Should I report a bug to update the FPC document to show this limitation in the Delphi mode?

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Declare operator in Delphi mode
« Reply #7 on: November 12, 2020, 12:13:29 pm »
If the official docs don't mention it, by all means do.
Or ask on fpc-devel mailinglist before doing so, it may already have been fixed in trunk.

Bart

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: Declare operator in Delphi mode
« Reply #8 on: November 12, 2020, 12:16:26 pm »
In delphi mode you use "implicit" instead of the ":="

Btw, the is already a complex unit in the libs.

I can't remember the name off. "Ucomplex" maybe?
The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Declare operator in Delphi mode
« Reply #9 on: November 12, 2020, 12:30:43 pm »
In delphi mode you use "implicit" instead of the ":="
If so, then the wiki page is wrong.
The offical docs say you can use assigment operators and/or implicit/explicit operators.

Btw, the is already a complex unit in the libs.

I can't remember the name off. "Ucomplex" maybe?

Yes, its unit uComplex.

I gathered TS was just playing around with operator overloading and use a complex type as an example.

Bart
« Last Edit: November 12, 2020, 12:33:23 pm by Bart »

Okoba

  • Hero Member
  • *****
  • Posts: 533
Re: Declare operator in Delphi mode
« Reply #10 on: November 12, 2020, 01:27:08 pm »
@Bart, yes and I updated the question.
BTW, I checked with trunk so it is not fixed.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Declare operator in Delphi mode
« Reply #11 on: November 12, 2020, 02:26:57 pm »
I saw this, but I thought the actual FPC document is the true reference, and I could not find that statement in there.

The FPC documentation being the true reference does not necessarily mean that it is complete and/or bug free. Please file a bug against the documentation.

So there is no way to declare an operator for a simple type like integer in Delphi mode? Just for records and classes and in their context?

Nearly correct: only records support operators (in either mode).

Also as a solution you can put your type and the operator overloads into a separate unit compiled in mode ObjFPC and use it in a unit with mode Delphi. Cause the operator resolution does not depend no the mode (the only exception is the +-operator for dynamic arrays which depends on the modeswitch ArrayOperators).

I like to use objfpc but it has its own problems like needing specializing every generic method for every use: https://forum.lazarus.freepascal.org/index.php/topic,52007

Correction: you need to write the specialize keyword every time. But the specialization itself is handled the same in both modes.

@Bart, yes and I updated the question.
BTW, I checked with trunk so it is not fixed.

The only fix necessary is a fix for the documentation. That global operators are not allowed in mode Delphi is by design and choice. Please file a bug report for the documentation, so that this is cleared up.


Okoba

  • Hero Member
  • *****
  • Posts: 533
Re: Declare operator in Delphi mode
« Reply #12 on: November 12, 2020, 04:41:25 pm »
Thanks for clarification.
Here is the bug: https://bugs.freepascal.org/view.php?id=38071

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Declare operator in Delphi mode
« Reply #13 on: November 12, 2020, 04:44:54 pm »
Thanks for clarification.
Here is the bug: https://bugs.freepascal.org/view.php?id=38071
You beat me by a few seconds.

Bart

Okoba

  • Hero Member
  • *****
  • Posts: 533
Re: Declare operator in Delphi mode
« Reply #14 on: November 12, 2020, 04:46:19 pm »
That's a pleasure :D

 

TinyPortal © 2005-2018