Skip to content

TypeCobolTypes

Olivier Smedile edited this page Mar 31, 2017 · 21 revisions

Future plan: * What is a type? * Weak, Strict and strong type * How to convert a type to another

What is a type?

A TYPE is both a structure of data and definition of a type. ## Structure of data

01 Person TYPEDEF STRICT. 
   05 Id         pic 9(05).     
   05 lastName   pic X(30).     
   05 FirstName  pic X(30).      
   05 BirthDate  pic X(08).
You can then use Person as a structure:
01 person1 TYPE Person.
Which is equivalent to:
01 person1.
   02 Id         pic 9(05).     
   02 lastName   pic X(30).     
   02 FirstName  pic X(30).      
   02 BirthDate  pic X(08).

A TYPE can be used at any level, but be aware that Cobol limits the number of level to 49.

01 person1 TYPE Person.
01 MyData.
    05 Array occurs 5.
       10 person2 TYPE Person.
Which is equivalent to:
01 person1.
   02 Id         pic 9(05).     
   02 lastName   pic X(30).     
   02 FirstName  pic X(30).      
   02 BirthDate  pic X(08).
01 MyData.
    05 Array occurs 5.
       10 person2.
         11 Id         pic 9(05).     
         11 lastName   pic X(30).     
         11 FirstName  pic X(30).      
         11 BirthDate  pic X(08).

Type (only apply to STRICT and STRONG type)

A TYPE allow you to control assignment to a variable. Only variable of the same TYPE can be moved into each other. (Note: only

With the following declaration:

01 Person TYPEDEF STRICT. 
   05 Id         pic 9(05).     
   05 lastName   pic X(30).     
   05 FirstName  pic X(30).      
   05 BirthDate  pic X(08).
01 person1 TYPE Person.
01 person2 TYPE Person.
01 person3 pic X(30).
Here, person2 is of TYPE Person and can only be assigned from Person1.
move person1 to person2  *> OK
move person3 to person2  *> KO - type not equals

TODO:

TypeCobol syntax (almost like Cobol 2002)

Translation into Cobol 85

01 Person TYPEDEF STRICT. 
   05 Id         pic 9(05).     
   05 lastName   pic X(30).     
   05 FirstName  pic X(30).      
   05 BirthDate  pic X(08).

Define a type Person.
Levels will be renumbered when used as a type

01 myPerson TYPE Person.

01 myPerson. 
   02 Id         pic 9(05).     
   02 lastName   pic X(30).     
   02 FirstName  pic X(30).      
   02 BirthDate  pic X(08).

01 myPerson    type Person.
01 myPerson2   type Person.
move myPerson  to myPerson2

*Ok because myPerson is of the same type as myPerson2
move myPerson to myPerson2

In a future release: * typed pointer * How to share type between programs * link to operator ::

Clone this wiki locally