Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6816349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:54:54+00:00 2026-05-26T20:54:54+00:00

In Delphi XE2, I’m trying to overload the in operator on a record to

  • 0

In Delphi XE2, I’m trying to overload the in operator on a record to allow me to check whether the value represented by the record is part of a set. My code looks like this:

type
  MyEnum = (value1, value2, value3);
  MySet = set of MyEnum;
  MyRecord = record
    Value: MyEnum;
    class operator In(const A: MyRecord; B: MySet): Boolean;
  end;

class operator MyRecord.In(const A: MyRecord; B: MySet): Boolean;
begin
  Result := A.Value in B;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  R: MyRecord;
  S: MySet;
begin
  R.Value := value1;
  S := [value1, value2];
  Button1.Caption := BoolToStr(R in S);
end;

The code fails to compile. For the statement R in S the compiler says: Incompatible types MyRecord and MyEnum.

How can I overload the In operator on MyRecord so that R in S will evaluate to True in the above code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:54:55+00:00Added an answer on May 26, 2026 at 8:54 pm

    Well, you can almost do this, but you may not want to. AFAIK, class operators only work on the class (or record) they are defined within, so both R and S in your code have to be TMyRecord. With some injudicious use of implicit casting, we get the following:

    unit Unit2;
    interface
    type
      MyEnum = (value1, value2, value3);
      MySet = set of MyEnum;
      MyRecord = record
        Value: MyEnum;
        ValueSet: MySet;
        class operator Implicit(A: MyEnum): MyRecord;
        class operator Implicit(A: MySet): MyRecord;
        class operator In (Left,Right:MyRecord): Boolean;
      end;
    
    implementation
    
    class operator MyRecord.Implicit(A: MyEnum): MyRecord;
    begin
      Result.Value := A;
    end;
    
    class operator MyRecord.Implicit(A: MySet): MyRecord;
    begin
      Result.ValueSet := A;
    end;
    
    class operator MyRecord.In(Left, Right: MyRecord): Boolean;
    begin
      Result:= left.Value in Right.ValueSet;
    end;
    end.
    

    The following will now complile, and even work:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      R: MyRecord;
      S: MyRecord;
    begin
      R.Value := value1;
      S := [value1,value2,value3];
      Button1.Caption := BoolToStr(R In S,true);
    end;
    

    Which, I’m sure we will all agree, is much more elegant than ‘BoolToStr(R.Value in S)’.
    However the following will also compile, but give the wrong result:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      R: MyRecord;
      S: MyRecord;
    begin
      R.Value := value1;
      S := [value1,value2,value3];
      Button1.Caption := BoolToStr(S In R,true);
    end;
    

    So, as Dorin commented, better to just have dull, staid old ‘BoolToStr(R.Value in S)’. Unless of course you are being paid per line of code. And a bonus for bug-fixing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've porting code to Delphi XE2, and need to change our data access components
Running the following code in Delphi XE2 Win32 platform works. However, the same code
I am trying to use the randomrange function in Delphi XE2 but for some
I have an (Delphi XE2) VCL app containing an object TDownloadUrl (VCL.ExtActns) to check
Delphi 2009 introduced a hierarchical system for project options configuration, where you set base
Delphi 2010 has a nice set of new file access functions in IOUtils.pas (I
Delphi XE2. There is a form & a frame. The form and the frame
The Delphi XE2 skinning option is fantastic, but there are cases where you want
In Delphi XE2 the automatically generated build numbers functionality now uses some kind of
I have ZipForge for Delphi XE2 & Delphi XE2. I try to test any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.