Skip to content

TypeCobolTypes

Olivier Smedile edited this page Apr 10, 2017 · 21 revisions

What is a TYPE

What is a type?

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

Structure of data

TypeCobol syntax

Translation into Cobol 85

You can declare a TYPE Person using the TYPEDEF keyword:

01 Person TYPEDEF STRICT. 
   05 Id         pic 9(05).     
   05 lastName   pic X(30).     
   05 FirstName  pic X(30).      
   05 BirthDate  pic X(08).
Levels will be renumbered when used as a type

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.

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

Weak, Strict and Strong type

  • Weak (like Cobol 2002 ISO Specifications) — The type is only used as a structure

  • Strict (TypeCobol) — Strict both a structure and a type

  • Strong (like Cobol 2002 ISO Specifications)

  • Strong both a structure and a type

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

Clone this wiki locally