"Union" types in Ada
24 March 2023 at 12:15
submitted by /u/dubst3pp4
Dear Ada community,
I've just picked up Ada (again) and try to implement a little API client as a first learning project. Currently I'm creating model classes for the entities returned by a JSON API. In the API specs, there is a JSON field, which can contain different data types, which are an ISO timestring *or* an ISO time interval.
Now I'm trying to find out, what is the "Ada way" to define a field, that can handle multiple types. The only thing that comes into my mind for my example is a variant record. Something like
type Time_Or_Interval (Has_End : Boolean) is record Begin_Date : Ada.Calendar.Time; case Has_End is when True => End_Date : Ada.Calendar.Time; when False => null; end case; end record;
Is this the preferred way?