How to return a set as function result

97 Views Asked by At

What I have

In TurboPascal I have defined an enumerated type TState and a set TStateS of this type.

type
  TState = (CS_Type1, CS_Type2, CS_Type3, CS_Type4 );
  TStateS = set of TState;

Also I have a function doing some calculation that should return a set

function Calc: TStateS;
begin
  { do calculate here and return }
  Calc := CS_Type1 + CS_Type2;
end;

My problem

The TurboPascal compiler complains with Error 34 "Invalid result type of function". The turbo pascal manual states that ordinal types are ok but does not mention sets.

My Question

Is there any other method of returning a set as a function result?

1

There are 1 best solutions below

0
On BEST ANSWER

"+" operates on sets and creates a union of the two, but CS_Type1 and CS_Type2 are not sets, but as you stated, enumerated types. To create and return a set of those two enums, you can use a set literal:

  Calc := [CS_Type1 , CS_Type2];

That allows your code to compile and work on Free Pascal. Not sure about Turbo Pascal, but the principle is the same, create a set of the enumerated types CS_Type1 and CS_Type2 and return that.