Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-12

Overloading IN operator

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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:

https://www.freepascal.org/docs-html/ref/refse104.html

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

Reply by email

Vojtěch Čihák

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

Show quoted text 5 lines
Od: Fabio Luis Girardi Komu: FPC-Pascal users discussions Datum: 13.12.2017 02:37 Předmět: [fpc-pascal] Overloading IN operator

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

----------

Reply by email

Sven Barth
to Fabio Luis Girardi: Hi all! I want to extend some operators in Freepascal. I want to achieve something like…

Am 13.12.2017 02:37 schrieb "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

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

Reply by email