5

I have been reading about LIKEDS, TEMPLATE, and BASED trying to determine if there is a way to create data structure templates (prototypes) with inheritance. I have:

D costs           DS                  QUALIFIED TEMPLATE
D  material                      6  0
D  cutting                       6  0
D  ...etc...

D boxCosts        DS                  LIKEDS(costs)
D  folding                       6  0
D  ...etc...

D posterCosts     DS                  LIKEDS(costs)
D  laminating                    6  0
D  ...etc...

Where I want boxCosts to look like:

boxCosts:
  material
  cutting
  folding
  etc. (no laminating, this isn't a poster)

Is there any way to achieve this type of data structure template? I know I could do:

D boxCosts        DS                  
D  common                             LIKEDS(costs)
D  folding                       6  0
D  ...etc...

But this creates a hierarchy when I want a flat structure.

I could maybe do this with a copybook, but I don't know if it would be worse to have a copy book for just the data structure parts I want in its own file, or to have potentially complicated conditional copy book for the whole application that has a small area for copying this information...? Templates come so close to what I want I suspect I must just be missing something.

If you are wondering, the compile error I get from trying to create an inherited data structure like I have shown is RNF3703: The subfield or parameter definition is not specified within a group. on the first D spec below the LIKEDS keyword.

Thanks for reading.

Sarah Kemp
  • 2,670
  • 3
  • 21
  • 29

2 Answers2

3

RPG data structures are memory maps. They define a way to group and overlap variables in a specific way in memory. That's why if you LIKEDS() you get a hierarchy - the compiler is copying the hierarchy from the template to your destination.

There's at least one way to flatten the structure:

 d costs           ds                  template
 d  t_material                    6s 0
 d  t_cutting                     6s 0

 d box           e ds                  extname(boxcosts) prefix(t_) template

 d boxCosts        ds                  qualified
 d  material                           like(t_material)
 d  cutting                            like(t_cutting)
 d  folding                            like(t_folding)

   boxCosts.cutting = 1;
   boxCosts.folding = 2;

The first structure is defined in the program; the second is based on a file. I did that only to show two different ways of getting the subfields defined.

Buck Calabro
  • 7,558
  • 22
  • 25
  • I wonder if there might not be a way to do this without having to explicitly specify each subfield (*material* and *cutting*) in secondary data structures (eg. *boxCosts*). It would be nice if derived data structures automatically inherited any fields added to the original. Maybe this is another job for "smart" copybooks. – WarrenT Mar 04 '14 at 18:08
  • Thank you for helping me understand my options. There is so much to learn and so few examples of the things I am trying to do... it's very difficult thinking outside OOP (for me anyway). – Sarah Kemp Mar 04 '14 at 22:38
2

You can accomplish your goal, if you are willing to use SQL to solve the problem. While ILE RPG data structures do not have inheritance, SQL tables can simulate this.

CREATE TABLE costs
(material    num(6,0)  
,cutting     num(6,0)  
);

CREATE TABLE boxCosts
(      LIKE  costs  
,folding     num(6,0)
,sealing     num(6,0)
);

CREATE TABLE postrCosts
(      LIKE  costs
,laminating  num(6,0)
);

If all you care about it the field names and definitions, that method may be fine, and all you need to use those structures in RPG would be

D boxCosts      E DS                  EXTNAME(boxCosts)

D posterCosts   E DS                  EXTNAME(postrCosts)

If field text or other attributes are important to you, then you may be better off with a slightly different strategy.

CREATE TABLE costs
(material    num(6,0)  
,cutting     num(6,0)  
);

LABEL ON COLUMN costs
(material    text is 'Material Costs'
,cutting     text is 'Cutting Costs'
);

CREATE TABLE boxCosts as
(SELECT * 
     FROM costs
) with no data
;
ALTER TABLE boxCosts 
  ADD COLUMN folding  num(6,0)
  ADD COLUMN sealing  num(6,0)
;
LABEL ON COLUMN boxCosts 
(folding     text is 'Folding Costs'
,sealing     text is 'Folding Costs'
);
WarrenT
  • 4,502
  • 19
  • 27
  • Of course if you want *method* inheritance, that will be a tougher nut to crack, lol. Maybe someday, right? – WarrenT Mar 04 '14 at 18:05
  • At the moment that feels a *bit* optimistic... thank you for your answer though, I hadn't considered it. – Sarah Kemp Mar 04 '14 at 22:33
  • Quite a bit optimistic perhaps. Then again Bjarne Stroustrup pulled it off with C, so we *could* say why not with RPG? We have ways of imitating the elementary features of classes, such as encapsulating the procedures (methods) and data structures in a service program. Inheritance is probably the next key feature, and as you've just seen, we can (sort of) do that with data structures. Methods would be next. Reflection might be nice. I don't think these things are necessarily unreachable, if we put some thought and effort into it. – WarrenT Mar 05 '14 at 15:07
  • @WarrenT Interfaces are already doable in RPG, see https://github.com/OSSILE/OSSILE/tree/master/code_examples/rpg/interfaces . Using this technique for method inheritance is just taking it one step further. – Mihael Sep 12 '17 at 03:50