Recent

Author Topic: Operator "IN" is not overloaded – Simple question  (Read 1832 times)

emaza

  • Jr. Member
  • **
  • Posts: 59
    • http://GerenciaDeCondominios.com
Operator "IN" is not overloaded – Simple question
« on: July 11, 2024, 09:38:27 am »

Hi, I have read all I could find here about this error but have a hard time trying to understand the high-level discussions, so here my simple question:
I have used statements like this:
•   If not (Caracter2 in ['0'..'9']) then 
And also:
•   If not (Key in ['0'..'9', #8, #13, '.']) then Result:=#0;   
All of which work fine in my program without having to do “something extra”, but other statements result in an “Operator is not overloaded” error complemented by a statement of: “Not Constant String” or “not AnsiString” depending on how I assembled the string after the “IN” operator. Something as simple as:
•   If NOT 'Paul' in 'Peter, Paul, Mary'  Then exit;     
Will not work.
I am sure that if I could get this last statement to work then I could fix the more complicated ones that in the end, are a version of it. Needless to say, I have no idea how to overload an operator.

Using Lazaris v2.2.0 64 & Firebird.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7535
Re: Operator "IN" is not overloaded – Simple question
« Reply #1 on: July 11, 2024, 09:56:12 am »
•   If NOT 'Paul' in 'Peter, Paul, Mary'  Then exit;     
Will not work.

Two things here. First, in all cases using IN you need to be careful with operator precedence, so consider adding parentheses.

Second, "string" in the context of your error messages does not imply that you can use IN for substring operations etc., you should be using something like Pos().

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 15557
  • Censorship about opinions does not belong here.
Re: Operator "IN" is not overloaded – Simple question
« Reply #2 on: July 11, 2024, 10:14:45 am »
It will work if you write a proper enumerator for it. Will add example later if it has not already been done. It is quite easy to do.
If I smell bad code it usually is bad code and that includes my own code.

alpine

  • Hero Member
  • *****
  • Posts: 1255
Re: Operator "IN" is not overloaded – Simple question
« Reply #3 on: July 11, 2024, 10:22:47 am »

Hi, I have read all I could find here about this error but have a hard time trying to understand the high-level discussions, so here my simple question:
I have used statements like this:
•   If not (Caracter2 in ['0'..'9']) then 
And also:
•   If not (Key in ['0'..'9', #8, #13, '.']) then Result:=#0;
   
The expression into the if statement checks if the given element (Key) belongs to a set with the set given as a constant (the list into the square brackets). The base type of the set is a Char implicitly, which is OK. The sets are restricted to have maximum number of 256 elements. 

All of which work fine in my program without having to do “something extra”, but other statements result in an “Operator is not overloaded” error complemented by a statement of: “Not Constant String” or “not AnsiString” depending on how I assembled the string after the “IN” operator. Something as simple as:
•   If NOT 'Paul' in 'Peter, Paul, Mary'  Then exit;     
Will not work.
I am sure that if I could get this last statement to work then I could fix the more complicated ones that in the end, are a version of it. Needless to say, I have no idea how to overload an operator.
Since the sets can't be constructed out of strings, the compiler tries alternatively to find an overloaded version of the operator in and since there isn't any, it gives the corresponding error. The complementary errors aren't so much relevant here.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Operator "IN" is not overloaded – Simple question
« Reply #4 on: July 11, 2024, 10:23:37 am »
Hi
Adding to what @MarkMLl & @alpine said, I've got a tip to make your "If NOT 'Paul' in 'Peter, Paul, Mary'  Then exit;" work, just try this:
Code: Pascal  [Select][+][-]
  1. uses StrUtils;
  2. ...
  3. if IndexStr('Paul',['Peter','Paul','Mary']) = -1 then exit; // case sensitive
  4. // or
  5. if pos('Paul','Peter, Paul, Mary') = 0 then exit; // also case sensitive
  6.  
If you want case INsensitvity, then you can use 'IndexText' / 'UpperCase' respectively...
'UTF8Pos' is also a possibility, if you include 'LazUTF8' in your 'uses' clause.
HTH
Regards Benny
« Last Edit: July 11, 2024, 10:27:45 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Operator "IN" is not overloaded – Simple question
« Reply #5 on: July 11, 2024, 10:25:32 am »
Hi
Quote
Will add example later if it has not already been done.
@Thaddy: I think @TRon wrote one a few months ago... It's here in the forum...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

TRon

  • Hero Member
  • *****
  • Posts: 3176
Re: Operator "IN" is not overloaded – Simple question
« Reply #6 on: July 11, 2024, 11:16:44 am »
@Thaddy: I think @TRon wrote one a few months ago... It's here in the forum...
Strange, as I can't seem to recall that (but my brain is mush)  :D

I did wrote a reverse integer in operator for boleeman a few months back though.

Nevertheless:
Code: Pascal  [Select][+][-]
  1. uses
  2.   sysutils;
  3.  
  4. operator in (const aString: string; const aArray: array of string): boolean;
  5. var
  6.   s: string;
  7. begin
  8.   result := false;
  9.   for s in aArray do
  10.     if s = aString then exit(true);
  11. end;
  12.  
  13.   if 'paul' in TStringArray.Create('Jason', 'Paul', 'John')
  14.    then writeln('hello');
  15.  

And there you can see that your previous suggestions of using something like IndexStr or Pos or even
Code: Pascal  [Select][+][-]
  1.   if 'paul'.IndexOfAny(['Peter', 'Paul', 'Mary']) = -1
  2.    then writeln('not in')
  3.     else writeln('in');
  4.  

Is just as easy as using the in operator because when using the operator there is still need to create the (typed) array.

So your first thought was a good one cdbc  :)
All software is open source (as long as you can read assembler)

Zvoni

  • Hero Member
  • *****
  • Posts: 2641
Re: Operator "IN" is not overloaded – Simple question
« Reply #7 on: July 11, 2024, 11:31:54 am »
Everything said above is true

OP's Problem is here
Quote
If NOT 'Paul' in 'Peter, Paul, Mary'  Then exit;     
Will not work.
He looks for 'Paul' inside a String 'Peter, Paul, Mary' --> that's a simple string, so Mark's idea with Pos is actually the "go to"-solution

Any other of above proposed solutions imply Sets or Arrays, even the first 2 cases of OP's first post uses sets/Arrays

His "mistake" is in thinking 'Peter, Paul, Mary' is a Set/Array

He mentioned "assembling" the String.
Maybe if he showed this "assembling"-code we could point him directly to the solution, code-snippets above not withstanding
« Last Edit: July 11, 2024, 11:33:46 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 15557
  • Censorship about opinions does not belong here.
Re: Operator "IN" is not overloaded – Simple question
« Reply #8 on: July 11, 2024, 11:45:58 am »
Well, the basics are easy, and since Tron seems to say he did not write it, I will write it..
If I smell bad code it usually is bad code and that includes my own code.

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Operator "IN" is not overloaded – Simple question
« Reply #9 on: July 11, 2024, 11:49:53 am »
Hi
Look in a thread with 'Stemplot' or something, I remember, 'cause I wrote a function in there as well...
edit: Found it: https://forum.lazarus.freepascal.org/index.php/topic,64745.0.html
I see now, it wasn't specifically for strings, but 'stem-records', sorry...
Regards Benny
« Last Edit: July 11, 2024, 11:53:05 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

MarkMLl

  • Hero Member
  • *****
  • Posts: 7535
Re: Operator "IN" is not overloaded – Simple question
« Reply #10 on: July 11, 2024, 11:55:28 am »
Everything said above is true

He looks for 'Paul' inside a String 'Peter, Paul, Mary' --> that's a simple string, so Mark's idea with Pos is actually the "go to"-solution

Any other of above proposed solutions imply Sets or Arrays, even the first 2 cases of OP's first post uses sets/Arrays

IN could obviously be redefined as a substring presence predicate. However the thing that bothers me here is that while all this business about custom operators etc. demonstrates that Evolved Pascal and its more experienced users are remarkably versatile, it does little to help someone who I assume is a relatively inexperienced user- and nothing at all to dispel the myth that Pascal coding is afflicted by a grievous overload of boilerplate.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 15557
  • Censorship about opinions does not belong here.
Re: Operator "IN" is not overloaded – Simple question
« Reply #11 on: July 11, 2024, 12:11:37 pm »
It is all pure notational.
If I smell bad code it usually is bad code and that includes my own code.

cdbc

  • Hero Member
  • *****
  • Posts: 1499
    • http://www.cdbc.dk
Re: Operator "IN" is not overloaded – Simple question
« Reply #12 on: July 11, 2024, 12:27:28 pm »
Hi
Mark would this be better?
Code: Pascal  [Select][+][-]
  1. if not string('Peter, Paul, Mary').Contains('Paul') then exit;
  2.  
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Zvoni

  • Hero Member
  • *****
  • Posts: 2641
Re: Operator "IN" is not overloaded – Simple question
« Reply #13 on: July 11, 2024, 12:36:25 pm »
Hi
Mark would this be better?
Code: Pascal  [Select][+][-]
  1. if not string('Peter, Paul, Mary').Contains('Paul') then exit;
  2.  
Regards Benny

Oh, dear.... i see a "OOP vs. procedural programming" approaching....
*prepare popcorn*
 :P :P :P
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 7535
Re: Operator "IN" is not overloaded – Simple question
« Reply #14 on: July 11, 2024, 12:42:03 pm »
Mark would this be better?
Code: Pascal  [Select][+][-]
  1. if not string('Peter, Paul, Mary').Contains('Paul') then exit;
  2.  

<Shrug> Looks fine to me, although I'd caution that that's not necessarily something that somebody brought up on a classical Pascal book would know about... and might not even be something he can locate in the RTL/FCL documentation.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018