Re: [fpc-pascal] Overloading IN operator

2017-12-12 Thread Sven Barth via fpc-pascal
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
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Overloading IN operator

2017-12-12 Thread 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.
__

Od: Fabio Luis Girardi <fluisgira...@gmail.com>
Komu: FPC-Pascal users discussions <fpc-pascal@lists.freepascal.org>
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>

--

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal 
<http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] Overloading IN operator

2017-12-12 Thread 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
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal