Case..of with record type

Issue #66 wontfix
Warley Alex Camargo created an issue

Can we use case..of with record type structure in dwscript (crossplatform/sms)?

type
   TEmployee = record
   case Salaried: Boolean of
     True: (AnnualSalary: float);
     False: (HourlyWage: float);
 end;

  // If this person is paid by the hour
  Person.Salaried := False;
  Person.HourlyWage := 20.00;

  // If this person is paid an annual salary instead
  Person.Salaried := True;
  Person.AnnualSalary := 80000.00;

Comments (2)

  1. Eric Grange repo owner

    Union records are not supported, in part because it would be very inefficient to emulate the binary mapping, and people using union usually do that for efficiency or low-level purposes.

    Also in Delphi, neither the compiler nor the runtime enforce anything wrt to that "case".

    In case where you do not have any complex mappings between the alternatives (like in your TEmployee), the declaration is thus equivalent to

      TEmployee = record
         Salaried : Boolean;
         AnnualSalary: float;
         property HourlyWage: float read AnnualSalary write AnnualSalary;
      end;
    

    ie. it is not a very useful type.

  2. Log in to comment