Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: generics: using the base definition (Delphi Seattle)


This question is answered.


Permlink Replies: 2 - Last Post: Feb 11, 2016 3:32 AM Last Post By: Rudy Velthuis (...
Ed Dressel

Posts: 42
Registered: 10/10/99
generics: using the base definition (Delphi Seattle)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 10, 2016 11:21 AM
Delphi Seattle (aka XE10, except for Rudy :-) )

I have the following class definitions:

  TBaseHistoricalRate = class...
  TBaseHistoricalRates<T: TBaseHistoricalRate> = class(TObjectList<T>)...
 
  THistoricMonthlyRate = class(TBaseHistoricalRate)...
  THistoricMonthlyRates = class(TBaseHistoricalRates<THistoricMonthlyRate>)...
 
  THistoricAnnualRate = class(TBaseHistoricalRate)...
  THistoricAnnualRates = class(TBaseHistoricalRates<THistoricAnnualRate>)...


I have a AnnualValues and MonthlyValues property. The user can select which values they want to see. As there are properties in common between AnnualValues and MonthlyValues, I would like to get a base-class list and assign the properties. So I tried the following code:

var
  lValues: TBaseHistoricalRates<TBaseHistoricalRate>;
begin
...
  if GetShowAnnualValues then
    lValues := FIndexMgr.AnnualRates
  else
    lValues := FIndexMgr.MonthlyRates;
...


but I get the following compiler exceptions:

*bold*[dcc32 Error] <myunit>.pas(498): E2010 Incompatible types: 'HistoricalIndexes.TBaseHistoricalRates<HistoricalIndexes.TBaseHistoricalRate>' and 'THistoricAnnualRates'
[dcc32 Error] <myunit>.pas(499): E2010 Incompatible types: 'HistoricalIndexes.TBaseHistoricalRates<HistoricalIndexes.TBaseHistoricalRate>' and 'THistoricMonthlyRates'*bold*

Is there a way to get to the base object list? Am I missing something?

Thank you,
Ed Dressel
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: generics: using the base definition (Delphi Seattle)
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 10, 2016 11:39 AM   in response to: Ed Dressel in response to: Ed Dressel
Ed wrote:

var
lValues: TBaseHistoricalRates<TBaseHistoricalRate>;
begin
...
if GetShowAnnualValues then
lValues := FIndexMgr.AnnualRates
else
lValues := FIndexMgr.MonthlyRates;
...

Sorry, that will not work. Generics are simply not that flexible. Although
THistoricAnnualRates and THistoricMonthlyRates both derive from TBaseHistoricalRates<T>,
their different T values make them have different TBaseHistoricalRates<T>
implementations, thus they effectively have different base classes that are
not related to each other, or related to TBaseHistoricalRates<TBaseHistoricalRate>
like you are expecting. TBaseHistoricalRates<THistoricMonthlyRate>, TBaseHistoricalRates<THistoricAnnualRate>,
TBaseHistoricalRates<TBaseHistoricalRate> are three distinct and unrelated
types. That is why the compiler complains about incompatible types.

Is there a way to get to the base object list?

No, unfortunately. You will have to re-think how you access the common properties.

--
Remy Lebeau (TeamB)
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: generics: using the base definition (Delphi Seattle)  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 11, 2016 3:32 AM   in response to: Ed Dressel in response to: Ed Dressel
Ed Dressel wrote:

Delphi Seattle (aka XE10, except for Rudy :-) )

Not for anyone else either. There is no XE in the name. It is Delphi 10
Seattle. What is so hard about that?
--
Rudy Velthuis http://www.rvelthuis.de

"You can get more with a kind word and a gun than you can with a
kind word alone." -- Al Capone (1899-1947)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02