Virtual Constants

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Virtual Constants

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;
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Virtual Constants

Michael Van Canneyt


On Fri, 1 Mar 2013, Daniel Gaspary wrote:

> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Virtual Constants

Joao Morais-2
In reply to this post by Daniel Gaspary
On Fri, Mar 1, 2013 at 5:31 PM, Daniel Gaspary <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Virtual Constants

Daniel Gaspary
On Sun, Mar 3, 2013 at 11:13 AM, Joao Morais <[hidden email]> wrote:
> Afaik no, but you can override class virtual methods which might give
> you the correct value:

Exactly what I was trying to avoid. :)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Virtual Constants

Adrian Veith-2
In reply to this post by Daniel Gaspary
Am 01.03.2013 21:31, schrieb 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;
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal