• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • The Geezer’s Guide to Free-Form RPG, Part 3: Data Structures and More Data Definitions

    May 28, 2014 Jon Paris

    In my previous tip on the new free-form RPG support I discussed the basics of the new methods for defining data in RPG. In this tip I will be covering some additional aspects of the new style data definitions that are not so obvious from a simple reading of the documentation.

    More on Data Definitions

    Until now, if you wanted to ensure that all fields of a specific type (currency for example) used a common definition, you used the LIKE keyword. The one small problem with this was that you could not change the data type of the cloned field. If the base definition was packed, all of its clones were also going to be packed.

    In the new free-form support, however, IBM has relaxed the rules concerning the use of named constants. Since one place where we can now use them is in the length definitions for fields, it provides us with an alternative approach. Take a look at the following example:

      dcl-c  DIGITS   7;
      dcl-c  DECIMALS 2;
    
      dcl-s  customerBalance  Packed( DIGITS: DECIMALS);
      dcl-s  invoiceTotal     Zoned( DIGITS: DECIMALS);
    

    Rather than clone a field definition, I can now specify the data type I want while still using a common set of length definitions. Now any time I need to change the size of these types of field I can do so simply by changing (say) DIGITS from 7 to 9.

    Since I have already mentioned the LIKE keyword, I should probably point out one small change in that. If you currently have a definition like this:

    D reportTotal     S             +2    Like(customerBalance)
    

    It is not immediately obvious how to achieve the same thing in freeform, but the answer is quite simple. IBM has added a length adjustment parameter to the LIKE keyword. So to adjust the length of the cloned field we simply code that second parameter. Like so:

      dcl-S reportTotal2      Like(customerBalance: +2); 
    

    This new flexibility in the use of literals does have one potential pitfall. It means that some keywords that previously accepted literal names without the use of quotation marks such as DTAARA, EXTNAME, and EXTFLD, now need quotes. In free form an unquoted name will always refer to a field or a named constant. So without the quotes the compiler will assume that the name references a constant and will flag a field not defined error.

    So this fixed form code:

    D myNewExtDs    E DS                  ExtName(PRODUCTX)
    
    D myDataArea      S             20a   DtaAra(JONSDTAARA)
    

    Cannot simply be converted to this equivalent:

      Dcl-Ds myNewExtDs ExtName(PRODUCT) End-ds;
    
      Dcl-S myDataArea Char(20) DtaAra(JONSDTAARA);
    

    But rather must have the quotation marks added like so:

      Dcl-Ds myNewExtDs ExtName('PRODUCT') End-ds;
    
      Dcl-S myDataArea Char(20) DtaAra('JONSDTAARA');
    

    Another potential pitfall that bit me during my early explorations of this new support concerns the use of ellipses in data definitions. Since I try to use descriptive field names in my code I often need to use these. Here’s an example:

    D masterAccountDetail...
    D                 DS                  LikeDS(accountDetail_T)
    

    In converting this manually it is tempting just to replace the initial D with Dcl-Ds, remove the D and DS from the second line and voila! You’re done. But the result as shown here won’t work.

      Dcl-Ds masterAccountDetail...
          LikeDS(accountDetail_T);
    

    The reason is quite simple: the ellipses tell RPG to continue the specification at the first non-blank character of the next line. So the compiler sees the LikeDS simply as a continuation of the DS name and a number of errors will result. Ellipses can still be used in free-form, but only to continue the name, not the specification as a whole. So the simple fix is to code it like so:

      Dcl-Ds masterAccountDetail
          LikeDS(accountDetail_T);
    

    Before I conclude this tip, just a quick word about the use of conditional directives in the new free-form. Previously we could only use such directives to selectively include or omit individual data declarations, or other code blocks. But we could not use them to selectively use part of a definition. With the new support we can. Here’s a simple example based on the idea that we might want to have a single source but be able to use it to generate both a UTF-16 (double byte) version of the program as well as a conventional single byte version. Using the new support it can be done like this:

    Dcl-s accountName 
      /If Defined(UTF16)
          ucs2(40) ccsid(1200)
      /Else
          char(40)
      /EndIf
          ;
    

    If the condition UTF16 is defined at the time the program is compiled, the accountName field will be defined as usc2(40), but if it is not in force the field will be defined as char(40). It appears as if the concluding semi-colon has to be placed outside of the conditioned code as shown here. I would rather have been able to add it to both definitions in the knowledge that only one will be used. Hopefully we may see that change in the future.

    That’s all for this tip. Next time we will be look at how the new support changes, and in many ways simplifies, the definition of prototypes and procedure interfaces. Until then if you have any questions or comments please let me know via Ted Holt by using the IT Jungle Contact page.

    Jon Paris is one of the world’s most knowledgeable experts on programming on the System i platform. Paris cut his teeth on the System/38 way back when, and in 1987 he joined IBM’s Toronto software lab to work on the COBOL compilers for the System/38 and System/36. He also worked on the creation of the COBOL/400 compilers for the original AS/400s back in 1988, and was one of the key developers behind RPG IV and the CODE/400 development tool. In 1998, he left IBM to start his own education and training firm, a job he does to this day with his wife, Susan Gantner–also an expert in System i programming. Paris and Gantner, along with Paul Tuohy and Skip Marchesani, are co-founders of System i Developer, which hosts the new RPG & DB2 Summit conference. Send your questions or comments for Jon to Ted Holt via the IT Jungle Contact page.

    RELATED STORIES

    The Geezer’s Guide to Free-Form RPG, Part 2: Data Structures and More

    Four Reasons RPG Geezers Should Care About The New Free-Form RPG



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    BCD:  June 5 Webinar: RPG Open Access in Presto Gives You More Modernization Options
    CloudFax400:  Get IBM i Performance Stats to Go. See how Robot set Joe free in this video >
    Remain Software:  IBM i Application Lifecycle Management freedom and flexibility

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ EnterpriseTech: High Performance Computing Industry News From ITJ EIC Timothy Prickett Morgan

    Big Data Accelerates 2013 Software Market; IBM Absent From Top Spots Threading The Needle Of Power8 Performance

    Leave a Reply Cancel reply

Volume 14, Number 12 -- May 28, 2014
THIS ISSUE SPONSORED BY:

CCSS
WorksRight Software
Remain Software

Table of Contents

  • The Geezer’s Guide to Free-Form RPG, Part 3: Data Structures and More Data Definitions
  • DB2 for i 7.2 Functions, Functions, Functions
  • The IBM i Journal Cache Sweeper Knob

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle