Lazarus

Programming => General => Topic started by: domasz on April 12, 2025, 07:20:25 am

Title: Overloading operator IN
Post by: domasz on April 12, 2025, 07:20:25 am
I want something like this:

Code: Pascal  [Select][+][-]
  1. var hey: String;
  2. begin
  3.   hey := 'one';
  4.   if hey in ['one', 'two'] then Exit;

I tried:

Code: Pascal  [Select][+][-]
  1. operator in (A: String; const B: array of const): boolean;
  2. begin
  3.   Result := False;
  4. end;

Not ideal solution:

Code: Pascal  [Select][+][-]
  1. function Group(const Ar: array of String): TStringArray;
  2. var i: Integer;
  3. begin
  4.   SetLength(Result, High(Ar)+1);
  5.  
  6.   for i:=0 to High(Ar) do Result[i] := Ar[i];
  7. end;
  8.  
  9. operator in (A: String; B: TStringArray): Boolean;
  10. var i: Integer;
  11. begin
  12.   for i:=0 to Length(B)-1 do
  13.     if B[i] = A then Exit(True);
  14.  
  15.   Result := False;
  16. end;  
  17.  
  18. if hey in Group(['one', 'two'])

How do I overload IN operator?
Title: Re: Overloading operator IN
Post by: cdbc on April 12, 2025, 07:33:59 am
Hi
Nah, instead you should use 'IndexStr' or 'IndexText' from 'StrUtils', in a case statement, like this:
Code: Pascal  [Select][+][-]
  1. var hey: String;
  2. begin
  3.   hey := 'one';
  4.   case IndexText(hey,['one','two','three']) of
  5.     0: exit; // 'one'
  6.     1: Caption:= 'Two';
  7.     2: showmessage('Three');
  8.   end;
  9. end;
  10.  
  11. /// edited later ///
  12. or
  13.   case IndexStr(hey,Group) of
  14.     0: /// your stuff
  15.   ...
  16.   end;
  17.  
Regards Benny
Title: Re: Overloading operator IN
Post by: domasz on April 12, 2025, 07:42:31 am
Thanks, Benny but that looks terrible. I want something pretty :)
Title: Re: Overloading operator IN
Post by: cdbc on April 12, 2025, 07:50:21 am
Hi
No worries, but be warned: 'array of const' is a whole other can of worms to open... But Hey, it's your party man...
Regards Benny
Title: Re: Overloading operator IN
Post by: domasz on April 12, 2025, 08:06:37 am
Code: Pascal  [Select][+][-]
  1. function Group(A: String; B: String = ''; C: String = ''; D: String = ''; E: String = ''; F: String = ''; G: String = ''; H: String = ''; I: String = ''; J: String = ''): TStringArray;
  2. var ii: Integer;
  3.     Ar: TStringArray;
  4.     Len: Integer;
  5. begin
  6.   Ar := TStringArray.Create(A,B,C,D,E, F,G,H,I,J);
  7.  
  8.   for ii:=9 downto 1 do begin
  9.     if Ar[ii] <> '' then begin
  10.         Len := ii+1;
  11.         break;
  12.       end;
  13.   end;
  14.  
  15.   SetLength(Result, Len);
  16.  
  17.   for ii:=0 to Len-1 do Result[ii] := Ar[ii];
  18. end;
  19.  
  20. operator in (A: String; B: TStringArray): Boolean;
  21. var i: Integer;
  22. begin
  23.   for i:=0 to Length(B)-1 do
  24.     if B[i] = A then Exit(True);
  25.  
  26.   Result := False;
  27. end;
  28.  
  29.  

Code: Pascal  [Select][+][-]
  1.   if hey in Group('one', 'two') then

It's slow but isn't it pretty? :D
Title: Re: Overloading operator IN
Post by: egsuh on April 12, 2025, 10:42:27 am
Why not following? This works.


Code: Pascal  [Select][+][-]
  1. operator in (a:string; b:array of string) : boolean;
  2.  
  3. implementation
  4.  
  5. operator in(a: string; b: array of string): boolean;
  6. var
  7.    c: string;
  8. begin
  9.    Result := false;
  10.    for c in b do if a=c then Exit(true);
  11. end;
  12.  
  13. {$R *.lfm}
  14.  
  15. { TForm1 }
  16.  
  17. procedure TForm1.Button1Click(Sender: TObject);
  18. begin
  19.    showmessage(booltostr('this' in TStringArray(['this', 'that']), true));
  20. end;
  21.  
Title: Re: Overloading operator IN
Post by: domasz on April 12, 2025, 11:57:21 am
Because it's a Fiat Multipla to my Porsche ;)
TinyPortal © 2005-2018