Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Enumeration members in IF staments. Compiler error.
Fri, Sep 25 2015 5:29 AMPermanent Link

Michael Dreher

Hi,

I've not fully understand the implicite enumeration to  integer (and back) type conversion in  the EWB Pascal. I get compiler errors. The version is 2.0.1 Build 2. Please have a look:

// Found in the help:
// Internally, enumerations are handled as integers by the compiler, and you can cast enumerations
// as integers and integers as enumerations.        

type
 TTestInstructionDataType = (
   TIDT_INT,
   TIDT_FLOAT,
   TIDT_BOOL,
   TIDT_STRING
);

procedure TForm1.Button1Click(Sender: TObject);
var
 n : integer;
begin       
        
 // Comparing const int with enum member in a case statement, it compiles
 case 1  of
   TIDT_INT    : n:=1;
   TIDT_FLOAT  : n:=1;
   TIDT_BOOL   : n:=1;
   TIDT_STRING : n:=1;
 else
   n:=1;
 end;
        
 // Comparing const int with enum member in an IF statement.
 // Does not compile:
 // [Error] Unit1.wbs (45,10): Expected boolean, integer, or double but instead found TIDT_STRING
 {
 if 1 = TIDT_STRING then
   n:=1;
 }

 // Make an explicite cast, this compiles
 // The case statement above does not need an explicite cast.
 if TTestInstructionDataType(1) = TTestInstructionDataType(TIDT_STRING) then
   n:=1;
     
 // Check some extension (using below), it compiles
 if (TTestInstructionDataType(1) = TTestInstructionDataType(TIDT_STRING)) then
   n:=1;   
    
 // Test AND operator in boolean expression (using below), it compiles
 if (1=1) AND (2=2) then
   n := 1;       

 // Combine enumeration member (with explicite casting) and AND operator in IF statement:
 // Does not compile:
 // [Error] Unit1.wbs (66,7): Expected Boolean but instead found TTestInstructionDataType(1)  
 {
 if (TTestInstructionDataType(1) = TTestInstructionDataType(TIDT_STRING))
   AND (2=2) then
   n:=1;   
 }
end;

How to get the last IF statement compilable?

Michael Dreher
Fri, Sep 25 2015 6:47 AMPermanent Link

Walter Matte

Tactical Business Corporation

Michael:

I just did a bunch of quick tests - all worked except once case where I get a compiler error.

Will have to see what Tim says.....

Walter


Type
 TTestEnum = (eA, eB, eC);


procedure TfrmMain.Button2Click(Sender: TObject);
begin
 ShowMessage(inttostr(integer(eB)));
end;

procedure TfrmMain.Button3Click(Sender: TObject);
begin
 if 1 = integer(eB) then
   ShowMessage('Yes eB')
 else
   ShowMessage('Not eB');
end;

procedure TfrmMain.Button4Click(Sender: TObject);
var
 XXX : TTestEnum;
begin
  XXX := TTestEnum(1);

  if XXX = eB then
    showmessage('x');
end;

procedure TfrmMain.Button5Click(Sender: TObject);
var
 XXX : TTestEnum;
begin
  XXX := eB;

  if integer(XXX) = 1 then
    showmessage('x');

end;

// Error Message from below
//  Expected TTestEnum but instead found TTestEnum(1)   !!!!


procedure TfrmMain.Button6Click(Sender: TObject);
var
 XXX : TTestEnum;
begin
  XXX := TTestEnum(1);

  if XXX = TTestEnum(1) then     // <<<<<<<<<<<< Will Not Compile  
    showmessage('x');

end;
Fri, Sep 25 2015 6:52 AMPermanent Link

Walter Matte

Tactical Business Corporation

Michael

Change:

if 1 = TIDT_STRING then

To

if 1 = integer(TIDT_STRING) then


Walter
Fri, Sep 25 2015 7:07 AMPermanent Link

Matthew Jones

Walter Matte wrote:

> if 1 = integer(TIDT_STRING) then

In Delphi, I'd use Ord(TIDT_STRING) for this sort of thing.

But if it matters for an enum, I'd either use simple integer const
values, or I'd have a routine to convert each way. Seems pointless to
have a type and then subvert it consistently.


--

Matthew Jones
Fri, Sep 25 2015 12:31 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

I'll check this out, but I really can't put any more fixes into 2.02.  It's already way late due to including late fixes, so it will have to wait until 2.02 B2.

Tim Young
Elevate Software
www.elevatesoft.com
Thu, Oct 1 2015 7:02 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

Alright, since things are already delayed a couple of days, I was able to squeeze in fixes for these issues also. Smile

This is what the compile-able version looks like for 2.02:

procedure TForm1.Button8Click(Sender: TObject);
var
n : integer;
begin       
       
// Comparing const int with enum member in a case statement, it compiles
case TTestInstructionDataType(1)  of
  TIDT_INT    : n:=1;
  TIDT_FLOAT  : n:=1;
  TIDT_BOOL   : n:=1;
  TIDT_STRING : n:=1;
else
  n:=1;
end;
       
// Comparing const int with enum member in an IF statement.
// Does not compile:
// [Error] Unit1.wbs (45,10): Expected boolean, integer, or double but instead found TIDT_STRING

{ if 1 = TIDT_STRING then
  n:=1;}


// Make an explicite cast, this compiles
// The case statement above does not need an explicite cast.
if TTestInstructionDataType(1) = TIDT_STRING then
  n:=1;
    
// Check some extension (using below), it compiles
if (TTestInstructionDataType(1) = TIDT_STRING) then
  n:=1;   
   
// Test AND operator in boolean expression (using below), it compiles
if (1=1) AND (2=2) then
  n := 1;       

// Combine enumeration member (with explicite casting) and AND operator in IF statement:
// Does not compile:
// [Error] Unit1.wbs (66,7): Expected Boolean but instead found TTestInstructionDataType(1)  

if (TTestInstructionDataType(1) = TIDT_STRING) AND (2=2) then
  n:=1;   

end;

The change to the:

  if TTestInstructionDataType(1) = TTestInstructionDataType(TIDT_STRING) then
     n:=1;
  
test to this:  

  if TTestInstructionDataType(1) = TIDT_STRING then
     n:=1;

(actually present in two tests)

is due to the fact that trying to cast an enumeration member to an enumeration type (even the same parent enumeration type) will now fail in the compiler (a side-effect of these fixes).  I considered changing this in the compiler because it's a nop, but figured that the error is best left in there because it signals an extraneous cast that should be removed.

Tim Young
Elevate Software
www.elevatesoft.com
Image