Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-12

evaluation of set constant

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Torsten Bonde Christiansen

Hi List.

Is it possible to create a constant of a set that is based on an evaluation of other constants? (using fpc 2.4.2)

A basic case could look like this:

type
   TMyType = (a, b, c ,d);
   TMyTypes = set of TMyTypes;
const
   SetX:        TMyTypes = (a, b);
   SetY:        TMyTypes = (c, d);
   SetCombined: TMyTypes = SetX + SetY;   // this gives me an "Error:
Illegal expression"

I have been reading throught the FPC docs, but it is unclear whether this is set operators allow this and according to FPC doc section on constants some expression evaluation is possible.

Kind regards,
Torsten Bonde Christiansen.
EpiData Association.

Jonas Maebe
to Torsten Bonde Christiansen
Torsten Bonde Christiansen wrote 12 lines
Is it possible to create a constant of a set that is based on an evaluation of other constants? (using fpc 2.4.2) A basic case could look like this: type TMyType = (a, b, c ,d); TMyTypes = set of TMyTypes; const SetX: TMyTypes = (a, b); SetY: TMyTypes = (c, d);

These definitions should not compile, a set is defined as [a, b], not (a, b).

Show quoted text 7 lines
SetCombined: TMyTypes = SetX + SetY; // this gives me an "Error: Illegal expression" I have been reading throught the FPC docs, but it is unclear whether this is set operators allow this and according to FPC doc section on constants some expression evaluation is possible.

So-called "typed constants" are actually initialised variables (for historical reasons). The compiler cannot evaluate expressions that involve variables (initialised or not).

Change the constants into symbol constants if you want to use them in constant expressions:

const
  SetX = [a, b];
  SetY = [c, d];
  SetCombined = SetX + SetY;

Jonas

Howard Page-Clark
to Torsten Bonde Christiansen: Hi List. Is it possible to create a constant of a set that is based on an evaluation of…
Torsten Bonde Christiansen wrote 8 lines
type TMyType = (a, b, c ,d); TMyTypes = set of TMyTypes; const SetX: TMyTypes = (a, b); SetY: TMyTypes = (c, d); SetCombined: TMyTypes = SetX + SetY; // this gives me an "Error: Illegal expression"

Because you have not declared your sets with the correct square brackets. Try this sample:

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
   Classes, Forms, Controls, Dialogs, StdCtrls;
type
   TForm1 = class(TForm)
     Button1: TButton;
     procedure Button1Click(Sender: TObject);
   end;
   TMyType = (a, b, c , d);
   TMyTypes = set of TMyType;
const
   SetX:      TMyTypes = [a, b];
   SetY:      TMyTypes = [c, d];
   typeNames: array [TMyType] of string = ('a', 'b', 'c', 'd');
var
   Form1: TForm1;
implementation
{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var SetCombined: TMyTypes;
     myType: TMyType;
     s: string;
begin
   s := '';
   setCombined :=  SetY + SetX;
   for myType in setCombined do
     s := s + typenames[myType];
   ShowMessage(s);
end;
end.

HTH

Howard

Torsten Bonde Christiansen
to Jonas Maebe: These definitions should not compile, a set is defined as [a, b], not (a, b). So-called…
Jonas Maebe wrote 18 lines
On 22 Dec 2010, at 14:15, Torsten Bonde Christiansen wrote: > Is it possible to create a constant of a set that is based on an > evaluation of other constants? (using fpc 2.4.2) > > A basic case could look like this: > > type > TMyType = (a, b, c ,d); > TMyTypes = set of TMyTypes; > > const > SetX: TMyTypes = (a, b); > SetY: TMyTypes = (c, d); These definitions should not compile, a set is defined as [a, b], not (a, b).

Sorry - that was my bad, i just made a typo in the previous email. It's written with [] in my code.

Show quoted text 19 lines
> SetCombined: TMyTypes = SetX + SetY; // this gives me an "Error: > Illegal expression" > > I have been reading throught the FPC docs, but it is unclear whether > this is set operators allow this and according > to FPC doc section on constants some expression evaluation is possible. So-called "typed constants" are actually initialised variables (for historical reasons). The compiler cannot evaluate expressions that involve variables (initialised or not). Change the constants into symbol constants if you want to use them in constant expressions: const SetX = [a, b]; SetY = [c, d]; SetCombined = SetX + SetY;

Ahh... great. I never thought of the difference between typed constants and symbol constants. Now it works, Thank!

Kind regards,
Torsten Bonde Christiansen.
EpiData Association.