LIKE attribute

The LIKE attribute specifies that the name that is declared has an organization that is logically the same as the referenced structure or union, the object of the LIKE attribute.

The object variable's member names and their attributes, including the dimension attribute, are effectively copied and become members of the name being declared. If necessary, the level-numbers of the copied members are automatically adjusted. The object variable name and its attributes, including the dimension attribute, are ignored.

Read syntax diagramSkip visual syntax diagram
>>-LIKE--object-variable---------------------------------------><

object-variable
Can be a major structure, a minor structure, or a union. It must be known in the block containing the LIKE attribute specification. It can be qualified but must not be subscripted. The object or its members can also have the LIKE attribute if they were declared previously.

The objects in all LIKE attributes are associated with declared names before any LIKE attributes are expanded.

New members cannot be added to the created structure or union. Any level-number that immediately follows the object variable in the LIKE attribute must be equal to or less than the level-number of the name with the LIKE attribute.

Start of changeThe LIKE attribute is supported in ENTRY descriptions and in parameter declarations. If used in an ENTRY description, the member names are not copied. For example, the following declares are valid:
dcl
    1 name,
    2 first   char(20) var,
    2 middle  char(10) var,
    2 last    char(30) var;

dcl func entry( like name  );  
The declare for the entry "func"would be the same as this longer declare:
dcl func entry( 1, 2 char(20) var, 2 char(10) var, 2 char(30) var );
End of change
The following declarations yield the same structure for X.
  dcl
    1 A(10) aligned static,
      2 B    bit(4),
      2 C    bit(4),
    1 X like A;

 
  dcl
    1 X,
      2 B bit(4),
      2 C bit(4);

Notice that the dimension (DIM(10)), ALIGNED, and STATIC attributes are not copied as part of the LIKE expansion.

The LIKE attribute is expanded before the defaults are applied and before the ALIGNED and UNALIGNED attributes are applied to the contained elements of the LIKE object variable. However, the LIKE attribute is expanded only after all LIKE attributes have been resolved.

Examples

Consider the following declarations:

  declare 1 A,
            2 C,
              3 E(3) union,
                5 E1,
                5 E2,
              3 F;
  declare 1 B(10) union,
            2 C, 3 G, 3 H,
            2 D;
  begin;
  declare 1 C like B;
  declare 1 D(2),
            5 BB like A.C;
  end;
Declarations C and D have the results shown in the following example:
  dcl
      1 C,               /* DIM and UNION not copied. */
        2 C, 3 G, 3 H,
        2 D;

 
  dcl 1 D(2),
        5 BB,
          6 E(3) union,  /* DIM(3) and UNION copied.      */
            7 E1,        /* Note adjusted level-numbers.  */
            7 E2,
          6 F;
The following declarations are valid, but only because B is declared before C and E is declared before F:
   dcl 1 a, 2 a1 fixed bin; 
   dcl 1 b, 2 b1 like a;    
   dcl 1 c, 2 c1 like b;    
                             
   dcl 1 d, 2 d1 fixed bin; 
   dcl 1 e  like d;         
The following example is valid, but only because the LIKE references are expanded after they are all resolved, otherwise the reference aa3_array would be ambiguous:
  dcl 1 aa(30)                           
     ,5 aa1               char( 5)       
     ,5 aa2               fixed bin(31)  
     ,5 aa3_array(30)                    
       ,7 aa3_1           fixed dec(15,2)
       ,7 aa3_2           fixed dec(15,2)
       ,7 aa3_3           fixed dec(11,4)
       ,7 aa3_4           fixed dec(7,3) 
    ;  

  dcl bb                  like aa;          
  dcl cc                  like aa3_array;                                                                           
The following example is invalid because C.E has the LIKE attribute and because B is declared after A. If the order of the declarations for A and B is reversed, the code is valid.
  declare 1 A like C,
          1 B,
            2 C,
              3 D,
              3 E like X,
            2 F,
          1 X,
            2 Y,
            2 Z;
The following example is invalid because G.C cannot be resolved. G.C is not resolved because the expansion of the LIKE for G occurs after the attempt to resolve the LIKE attribute for A:
  declare 1 B,
            2 C,
              3 D,
              3 E,
            2 F,
          1 G like B;
          1 A like G.C,





Published: 23 December 2018