Recent

Author Topic: Can Type be Redefined in Inherited Class?  (Read 5420 times)

eoneuk

  • New member
  • *
  • Posts: 7
Can Type be Redefined in Inherited Class?
« on: September 09, 2014, 06:46:00 pm »
In the following simple example, I want to redefine z so that in class c it is an integer, and in d, a real.  When I compile I get an error message "duplicate identifier z". 

Can this be accomplished, and if so, how?

Ed


Code: [Select]
type
  c = class
 type
    z = integer;
  var
    x,y : z;
  end;

  d = class(c)
  type

    z = real;

 end  ;

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can Type be Redefined in Inherited Class?
« Reply #1 on: September 09, 2014, 06:53:31 pm »
Try to make it private.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

eoneuk

  • New member
  • *
  • Posts: 7
Re: Can Type be Redefined in Inherited Class?
« Reply #2 on: September 09, 2014, 07:17:51 pm »
Thanks for your reply, but making it private had no effect.

Ed

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Can Type be Redefined in Inherited Class?
« Reply #3 on: September 09, 2014, 07:27:31 pm »
No, this is not possible.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can Type be Redefined in Inherited Class?
« Reply #4 on: September 09, 2014, 07:31:41 pm »
It is possible (with type defined in private section), but descendant class has to be in other unit.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

eoneuk

  • New member
  • *
  • Posts: 7
Re: Can Type be Redefined in Inherited Class?
« Reply #5 on: September 09, 2014, 08:30:07 pm »
Placing the descendant class in another unit, resolved the compiler error.
However, when using the classes in a program, z is not redefined in class d, but is the same as whatever is defined in class c. 

Are there any additional statements needed to make this work?

Ed

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can Type be Redefined in Inherited Class?
« Reply #6 on: September 09, 2014, 08:46:18 pm »
And what are you trying to achieve? You can define type with the same name (z) in each class but that doesn't change the fact that pascal is statically typed language, so whatever method working with type "z" will have to be reintroduced.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Can Type be Redefined in Inherited Class?
« Reply #7 on: September 09, 2014, 09:09:22 pm »
It is possible (with type defined in private section), but descendant class has to be in other unit.

It is not possible to redefine it, only to define a different type, but with the same name in a different scope. Which is probably not a solution.

Generic types are, and can be inherited, but in Delphi generics, you can do nearly no operations on a generic type T. (since there is no constraint modifier for ordinal or real types)

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Can Type be Redefined in Inherited Class?
« Reply #8 on: September 09, 2014, 09:23:08 pm »
Thinking about a possible redesign of your class, would an overloaded function GetValue solve your problem?  Each descended class stores the value in it's own internal variable, and each descended class exposes an overloaded GetValue of the appropriate datatype?  (just ensure you don't declare your original GetValue as virtual, and I think this is doable)

Only other workaround I think of was for you to use Variants instead a static type.  Introduces memory overheads though...
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

eoneuk

  • New member
  • *
  • Posts: 7
Re: Can Type be Redefined in Inherited Class?
« Reply #9 on: September 10, 2014, 04:49:31 pm »
I was able to accomplish my goal by the use of variants.

Thank all of you for your timely replies and suggestions.  This is a great forum!


Ed

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Can Type be Redefined in Inherited Class?
« Reply #10 on: September 10, 2014, 08:06:54 pm »
I guess the solution is to not declare x,y : z; in the class C at all. Since type Z is not known yet, C is just a abstract class.

 

TinyPortal © 2005-2018