[fpc-devel] Re: Testing for..in feature

"Vinzent Höfler" JeLlyFish.software at gmx.net
Thu Nov 5 20:51:59 CET 2009


Von: Michael Van Canneyt <michael at freepascal.org>:

> Like I said, we should not have named it enums. They are not enums.
> They're a hack to support some C construct in a type-safe way. 
> No more, no less.

IBTD.

-- 8< --
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Conversion;

procedure Enumeration_Magic is

   --  Enumeration type.
   type Op_Codes is (Jmp, Inc, Dec);

   --  Representation with holes and same size as Standard.Integer.
   for Op_Codes use (Jmp => 1, Inc => 2, Dec => 5);
   for Op_Codes'Size use Integer'Size;

   --  Unsafe cast function to get access to the actual values.
   function To_Int is new Ada.Unchecked_Conversion (Source => Op_Codes,
                                                    Target => Integer);

   Low  : constant Op_Codes := Op_Codes'First;
   High : constant Op_Codes := Op_Codes'Last;
begin
   --  First/Last.
   Ada.Text_IO.Put_Line ("Low => "    & Low'Img &
                         ", High => " & High'Img);

   --  Loop through the range.
   for X in Op_Codes'Range loop
      Ada.Text_IO.Put_Line (X'Img & " => " & To_Int (X)'Img);
   end loop;
end Enumeration_Magic;
-- 8< --

Output:

-- 8< --
Low => JMP, High => DEC
JMP =>  1
INC =>  2
DEC =>  5
-- 8< --

They might be an ugly hack, but I really doubt the part about being a C construct. And then, even in Ada, the definitions have to be in (ascending) order. Which means, the compiler only has to support "holes", not an arbitrary set of values.

Maybe this would be an option for FPC, too?


Vinzent.

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser



More information about the fpc-devel mailing list