Sign in Join

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

Mailing lists / fpc-pascal / 2013-03

Virtual Constants

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Daniel Gaspary

The idea is strange. But here it is:

Is possible to have a Class constant with different values in class descendants ?

Would be something like that:

TA = class
const
        c1: integer; virtual;
        class function GetC1: integer; //returns c1
end;
TB = class(TA)
const
        c1: integer = 1;
end;
TC = class(TA)
const
        c1: integer = 2;
end;
Michael Van Canneyt
to Daniel Gaspary
Daniel Gaspary wrote 23 lines
The idea is strange. But here it is: Is possible to have a Class constant with different values in class descendants ? Would be something like that: TA = class const c1: integer; virtual; class function GetC1: integer; //returns c1 end; TB = class(TA) const c1: integer = 1; end; TC = class(TA) const c1: integer = 2; end;

Funny coincidence, I was looking for the same thing today :)

Michael.

Joao Morais
to Daniel Gaspary: The idea is strange. But here it is: Is possible to have a Class constant with different…
Daniel Gaspary wrote
Is possible to have a Class constant with different values in class descendants ?

Afaik no, but you can override class virtual methods which might give you the correct value:

  TA = class
  protected
    class function value: integer; virtual; abstract;
  public
    class function getvalue: integer;
  end;
  TB = class(TA)
  protected
    class function value: integer; override;
  end;
  TC = class(TA)
  protected
    class function value: integer; override;
  end;

Joao Morais

Daniel Gaspary
to Joao Morais
Joao Morais wrote
Afaik no, but you can override class virtual methods which might give you the correct value:

Exactly what I was trying to avoid. :)

Adrian Veith
to Daniel Gaspary: The idea is strange. But here it is: Is possible to have a Class constant with different…
Daniel Gaspary wrote 26 lines
The idea is strange. But here it is: Is possible to have a Class constant with different values in class descendants ? Would be something like that: TA = class const c1: integer; virtual; class function GetC1: integer; //returns c1 end; TB = class(TA) const c1: integer = 1; end; TC = class(TA) const c1: integer = 2; end; _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

currently I use a dirty hack to achieve something like this: I have a fixed naming scheme for the classname and encode the "virtual" const in the classname.