Overloading IN operator

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Overloading IN operator

Fabio Luis Girardi
Hi all!

I want to extend some operators in Freepascal. I want to achieve something like this:

if aStr in ['string item 1', 'string item 2', 'string item 3'] then
  ...
else
  ...;

Following the examples to overload operators, I wrote:

operator in (a:String; b:array of string):Boolean;
var
  c: Integer;
begin
  Result:=false;
  for c:=0 to High(b) do
    if b[c]=a then begin
      result:=true;
      exit;
    end;
end;

But when I try compile this piece of code:

if aStr in ['string item 1', 'string item 2', 'string item 3'] then
  ...
else
  ...;

I got:

Error: Ordinal expression expected

Looking to Freepascal documentation, IN operator overloading is possible:


So if the overloading is possible, Is possible to get this example working?

--
The best regards,

Fabio Luis Girardi
PascalSCADA Project
http://sourceforge.net/projects/pascalscada
http://www.pascalscada.com

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloading IN operator

vojtech.cihak

It seems that overloading is possible but compiler does not want to accept anonymous array of strings. It works with variable or with anonymous array of integers (FPC 3.0.4).

 

V.

______________________________________________________________
> Od: Fabio Luis Girardi <[hidden email]>
> Komu: FPC-Pascal users discussions <[hidden email]>
> Datum: 13.12.2017 02:37
> Předmět: [fpc-pascal] Overloading IN operator
>



----------

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloading IN operator

Free Pascal - General mailing list
In reply to this post by Fabio Luis Girardi
Am 13.12.2017 02:37 schrieb "Fabio Luis Girardi" <[hidden email]>:
Hi all!

I want to extend some operators in Freepascal. I want to achieve something like this:

if aStr in ['string item 1', 'string item 2', 'string item 3'] then
  ...
else
  ...;

Following the examples to overload operators, I wrote:

operator in (a:String; b:array of string):Boolean;
var
  c: Integer;
begin
  Result:=false;
  for c:=0 to High(b) do
    if b[c]=a then begin
      result:=true;
      exit;
    end;
end;

But when I try compile this piece of code:

if aStr in ['string item 1', 'string item 2', 'string item 3'] then
  ...
else
  ...;

I got:

Error: Ordinal expression expected

The problem is not the operator, but the array: before 3.1.1 the "[...]" operator if used outside a parameter that is an open array *always* generates a set. This only changed in trunk a few months ago so that the operator can be used as an array constructor as well. So for now you'd need to use an array variable on the right side. 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal