fork(1) download
  1. with System;
  2. with System.Memory;
  3.  
  4. with Ada.Text_IO;
  5. with Ada.Tags;
  6. with Ada.Tags.Generic_Dispatching_Constructor;
  7.  
  8. procedure Test is
  9.  
  10. type Point is tagged record
  11. X, Y : Natural;
  12. end record;
  13.  
  14. procedure Put_Point (Item : in Point'class) is begin
  15. Ada.Text_IO.Put ("X: " & Item.X'image);
  16. Ada.Text_IO.Put ("Y: " & Item.Y'image);
  17. end;
  18.  
  19. type Saturation is range 0 .. 255;
  20.  
  21. type Pixel is new Point with record
  22. Red : Saturation;
  23. Green : Saturation;
  24. Blue : Saturation;
  25. end record;
  26.  
  27. procedure Put_Pixel (Item : in Pixel) is begin
  28. Put_Point (Item);
  29. Ada.Text_IO.Put ("RGB: (" & Item.Red'Image & "," & Item.Green'Image & "," & Item.Blue'Image & ")");
  30. end;
  31.  
  32. type Tile is new Point with record
  33. Elevation : Natural;
  34. end record;
  35.  
  36. procedure Put_Tile (Item : in Tile) is begin
  37. Put_Point (Item);
  38. Ada.Text_IO.Put ("Elevation: " & Item.Elevation'Image);
  39. end;
  40.  
  41. function Alloc (Size : not null access System.Memory.size_t) return Point'class is
  42. Instance_Address : System.Address := System.Memory.Alloc (Size.all);
  43. Instance : Point'class;
  44. for Instance'address use Instance_Address;
  45. begin
  46. return Instance;
  47. end;
  48.  
  49. procedure Free (P : Point'class) is begin
  50. System.Memory.Free (P'address);
  51. end;
  52.  
  53. function New_Point is new Ada.Tags.Generic_Dispatching_Constructor (T => Point, Parameters => System.Memory.size_t, Constructor => Alloc);
  54.  
  55. Point_Size : aliased System.Memory.size_t := Point'Size;
  56. P1 : Point := New_Point (Point'Tag, Point_Size'access);
  57. begin
  58. Put_Point (P1);
  59. end;
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
x86_64-linux-gnu-gcc-8 -c -pipe -O2 test.adb
test.adb:43:05: initialization required in class-wide declaration
test.adb:44:09: warning: class-wide object "Instance" must not be overlaid
test.adb:44:09: warning: Program_Error will be raised at run time
test.adb:53:134: no visible subprogram matches the specification for "Constructor"
test.adb:56:17: dynamically tagged expression not allowed
x86_64-linux-gnu-gnatmake-8: "test.adb" compilation error
stdout
Standard output is empty