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 7585623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:13:16+00:00 2026-05-30T19:13:16+00:00

i have a problem about conversion of type using overload of operator, here i

  • 0

i have a problem about conversion of type using overload of operator, here i have an full example code:

program OVerloads;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  TMyRange = record
  private type
    TMyRangeEnum = 1 .. 90;
  var
    FMyRangeEnum: TMyRangeEnum;
  public
    class operator Implicit(const Value: Integer): TMyRange;
    class operator Implicit(const Value: TMyRange): Integer;
    class operator Explicit(const Value: Integer): TMyRange;
    class operator Explicit(const Value: TMyRange): Integer;
  end;

class operator TMyRange.Implicit(const Value: Integer): TMyRange;
begin
    Result.FMyRangeEnum := Value;
end;

class operator TMyRange.Implicit(const Value: TMyRange): Integer;
begin
  Result := Value.FMyRangeEnum;
end;

class operator TMyRange.Explicit(const Value: Integer): TMyRange;
begin
  Result.FMyRangeEnum := Value;
end;

class operator TMyRange.Explicit(const Value: TMyRange): Integer;
begin
  Result := Value.FMyRangeEnum;
end;

var
 MyValue: TMyRange;
 MyVar: TMyRange; // (or Integer, not change nothing)
 MyArr: array[1..90] of TMyRange; // (or Integer, not change nothing) 
begin
  try
    { TODO -oUser -cConsole Main : Insert code here }

    MyValue := 90;
    Writeln(MyValue);  // [1] Not work, error: E2054 Illegal in Write/Writeln statement

    Writeln(Integer(MyValue));  // Work

    MyVar := MyArr[MyValue];   // Not work, error: E2010 Incompatible types: 'Integer' and 'TMyRange'                

    MyVar := MyArr[Integer(MyValue)];  // work      

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

Probably i have forgotten something, but as i can solve problem [1] in mnode that not need explicit all time Integer(myValue) ?
Thanks again, very much.

  • 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-30T19:13:17+00:00Added an answer on May 30, 2026 at 7:13 pm

    You appear to be overcomplicating this. Define a range type like this:

    type
      TMyRange = 1..90;
    

    Then declare your array like this:

    var
      MyArr: array[TMyRange] of TMyRange;
    

    And that’s it.


    For whatever reason, you assert that you need to use a record here with operator overloading. Be that as it may, but you have pretty much identified that operator overloading implicit casts can not be used to shoehorn your type to be compatible with Writeln and integer array indexing. I can’t find any documentation that describes these scenarios, but it’s pretty clear that the compiler won’t let you do what you want.

    So far as I can tell, you can rely on the implicit cast for actual parameters and the right hand side of assignment statements.

    Here is my minimal sample to demonstrate the options:

    program Overloads;
    {$APPTYPE CONSOLE}
    type
      TRec = record
      private
        function GetOrd: Integer;
      public
        class operator Implicit(const Value: TRec): Integer;
        property ord: Integer read GetOrd;
      end;
    
    class operator TRec.Implicit(const Value: TRec): Integer;
    begin
      Result := 0;
    end;
    
    function TRec.GetOrd: Integer;
    begin
      Result := 0;
    end;
    
    procedure Foo(i: Integer);
    begin
    end;
    
    var
      R: TRec;
      a: array[0..0] of Integer;
    
    begin
      Writeln(R);//E2054 Illegal type in Write/Writeln statement
      Writeln(Integer(R));//explicit cast, provided by class operator Implicit
      Writeln(R.ord);//my preferred option, a property
      a[R] := 0;//E2010 Incompatible types: 'Integer' and 'TRec'
      a[Integer(R)] := 0;//again, explicit cast is fine
      a[R.ord] := 0;//or using a property
      Foo(R);//implicit cast used for actual parameters
    end.
    

    Notes:

    1. If you are supplying an implicit cast operator then there’s no need to supply an explicit cast operator also. An explicit cast will result in the implicit operator being called.
    2. I think a property on the record type results in more readable code than an explicit cast.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MFC CFile Seek function. I have a problem about Seek out
I have a little problem about using jQuery (I really do not know jQuery
I have a problem about div position relative alignment. I want the second div
I have hit upon this problem about whether to use bignums in my language
I have got a strange problem about in_array recently which I cannot understand. e.g.
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
i have a problem. i want to get info about my string have other
So I have a problem that I don't really know how to go about.
Following on from a question I posted yesterday about GUIs, I have another problem
The problem is not about randomness itself (we have rand), but in cryptographically secure

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.