Type Safety and COBOL

Having read a recent bog about COBOL and type-safety, I though I would jot down some comments.

ANS85 COBOL is naturally is type-unsafe due as every data item being part of one memory region (or storage area), because of this it can make is difficult to talk to type-safe language such as Java or the CLR.

However, just like any computer language it does not stay still. Object oriented features were added to COBOL, at this point in time you we were given the verbs to create 100% type safe application.

The key to writing type safe code is the use OBJECT-REFERENCE will a TYPE and using them on in your methods and using the INVOKE verb.

For example:


IDENTIFICATION DIVISION.
PROGRAM-ID. "TypeSafeExample".
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS SYS-STRING AS "System.String".
DATA DIVISION.
WORKING-STORAGE SECTION.
01 hello-world-string OBJECT REFERENCE SYS-STRING.

PROCEDURE DIVISION.
SET hello-world-string TO "Hello World"
DISPLAY hello-world-string
STOP RUN.

The use of COBOL pictures can still be used, however for easy interop with other type-safe languages you really have to stick to the right types for your target environment when exposing your COBOL program to the other languages, this means if you are creating classes, the method to be consumed by other languages should use standard types, for .Net/CLR applications the use of CLS-COMPLIANT types is the right approach. For JVM applications, keep to the “core” java.lang types.

Keep to these simple rules and COBOL will happy interop with type-safe languages. 🙂

[ad#Google Adsense-3]

This entry was posted in CLR, COBOL, JVM, TypeSafety and tagged , . Bookmark the permalink.