options(*CONVERT) in RPGfree '

*CONVERT option in RPG



The *CONVERT option in RPG is specified to tell the compiler to automatically convert a value passed as a parameter to the correct type if it differs from the type defined in the procedure.

 

How OPTIONS(*CONVERT) Works

OPTIONS(*CONVERT) allows for automatic data conversion between compatible types when a procedure or function is called with a parameter of a different type than expected. For example, if a procedure is defined with a numeric parameter and a different type, such as a decimal, is passed to it, the compiler will automatically handle the conversion so that the value matches the declared type.


For example:

**free
//      *******************************************************
//      * convert to char                                     *
//      *******************************************************
ctl-opt main(main) dftactgrp(*no);
 
//      *******************************************************
//      * PGM start                                           *
//      *******************************************************
dcl-proc main;
  dcl-s UTF8uni char(25) ccsid(*utf8) inz('IBM i (AS400) fans only');
  dcl-s vFloat Float(8)  inz(123) ;
  dsply (cvtToChar(UTF8uni))      ;
  dsply (cvtToChar(vFloat))       ;
  dsply (cvtToChar(%timestamp())) ;
  dsply (cvtToChar(%date()))      ;
  dsply (cvtToChar(1.2345678901)) ;
end-proc;
 
dcl-proc cvtToChar;
  dcl-pi *n varchar(40);
    ConvertedtoChar varchar(40) const options(*convert);
  end-pi;
  return ConvertedtoChar;
end-proc;


Here’s a breakdown of how each call works:

  1. String with UTF-8 (UTF8uni): cvtToChar receives a UTF-8 character string and uses OPTIONS(*CONVERT) to ensure it's interpreted as VARCHAR(40). Since it’s already a character type, minimal conversion is required.
  2. Float Value (vFloat): OPTIONS(*CONVERT) converts vFloat, a floating-point number, into a character string.
  3. Timestamp and Date (%timestamp() and %date()): Both are non-character types, but OPTIONS(*CONVERT) allows them to be passed as parameters, and the compiler automatically converts them to a string representation.
  4. Constant Decimal (1.2345678901): This decimal constant is automatically converted to a VARCHAR type, demonstrating compatibility with numeric literals.


When to Use It

Using OPTIONS(*CONVERT) is useful when you want to maintain flexibility in parameter data types, avoiding type errors when passing values of a compatible but different type. This is particularly valuable when integrating procedures with predefined data types or working on projects where parameter types may vary. 


Some tips to use it effectively?

What do you think? Have you used OPTION(*CONVERT) in your RPG projects?




Comments

  1. Triggers for log-programs. We can now drop MOVEL and use this instead.

    ReplyDelete

Post a Comment

Popular posts from this blog

IBMi (AS400) fans only ' Efficient WRKSPLF with WSF - How to Search string into spooled files, Sort, and Generate PDFs on IBMi

IBMi (AS400) fans only ' Detecting and Handling Non-Printable Characters in DB2 SQL Using LOCATE() and REPLACE() Functions

IBMi (AS400) fans only - How to Sniff User Access