Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/Errors - Warnings    [ Add a report in this area ]  
Report #:  35298   Status: Open
Operator overloading does not allow IN
Project:  Delphi Build #:  10.0.2288.42451
Version:    10.0 Submitted By:   Thomas Mueller
Report Type:  Basic functionality failure Date Reported:  10/16/2006 8:45:58 AM
Severity:    Commonly encountered problem Last Updated: 7/2/2014 5:33:02 PM
Platform:    All versions Internal Tracking #:   8387
Resolution: Need More Info (Resolution Comments) Resolved in Build: : XE6
Duplicate of:  None
Voting and Rating
Overall Rating: (6 Total Ratings)
4.50 out of 5
Total Votes: 3
Description
There seems to be no special operator for the IN operation (e.g. a in [1, 2, 4]). Also, implicit and explicit conversion do not work for the IN operation but result in a "incompatible types" compiler error

Example:

type
  TEnum = (one, two);

type
  TMyRec = record
  private
    FValue: TEnum;
  public
    class operator Implicit(a: TMyRec): TEnum;
    class operator Explicit(a: TMyRec): TEnum;
  end;

  { TMyRec }

class operator TMyRec.Explicit(a: TMyRec): TEnum;
begin
  Result := a.FValue;
end;

class operator TMyRec.Implicit(a: TMyRec): TEnum;
begin
  Result := a.FValue;
end;

var
  a: TMyRec;

begin
  a.FValue := one;
  if a in [one] then <<<<< this does not compile
    WriteLn('ok');
Steps to Reproduce:
open attached sample project in the IDE and try to compile it
Workarounds
temporarily assign the record to a variable of the correct kind:

var
  b: TEnum;

[...]

  b := a;
  if b in [one] then

OR

do an explicit typecast:

  if TEnum(a) in [one] then
Attachment
Project1.zip
Comments

Pierre le Riche at 11/12/2006 12:11:12 PM -
"In" is used for sets. "a in [one]" does not make sense, because "a" is a record not a TEnum. Also, you cannot have a set of records.

Thomas Mueller at 11/23/2006 1:23:45 AM -
But you can have a record that can be converted (implicitly or explicitly) to some literal type like integer or enum. These can be stored in a set, so in my view the code in details should work.

Pierre le Riche at 12/4/2006 4:23:58 AM -
Ah ok, sorry - I misread the report: I didn't see the implicit conversion operator.

I agree, it would be nice if the compiler supported that syntax if there is a conversion operator.

Asbjørn Heid at 7/2/2014 12:06:36 PM -
I don't know when it was introduced but XE5 at least allows you to write "class operator In(...)", for example:

type
  TRange = record
    lower, upper: integer;

    class operator In(const v: integer; const r: TRange): boolean;
  end;

class operator TRange.In(const v: integer; const r: TRange): boolean;
begin
  result := (v >= r.lower) and (v <= r.upper);
end;

var
  r: TRange;
  v: integer;
begin
  r.lower := 3;
  r.upper := 7;

  if 5 in r then
    WriteLn('5');
  v := 42;
  if v in r then
    WriteLn(v);
end.

Note that the class-completion does not like the "in" operator, you'll need to write the body manually.

Tomohiro Takahashi at 7/2/2014 5:36:34 PM -
Thanks for the confirmation. I will check the internal status of this report...

Server Response from: ETNACODE01