× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hello,

Trying to figure out why the following /free code is blowing raspberries at me:

   cuPD   = %dec( %char( %date : *ISO0 )
                : %len( cuPD )
                : %decpos( cuPD )        );

cuPD is defined as P(8,0) in a database file defined for output.  The
compiler errors I'm getting are related to the 2nd & 3rd parameters of
the %dec BIF:

The %DEC() BIF can be used to control intermediate representations and has a big impact on the way that the code gets compiled. Since that's the case, the 2nd and 3rd parameters to this BIF need to be known at compile time -- after all, how can it control the way that the code is compiled if it doesn't know what the sizes of the fields are until the program is running?

However, the %len() BIF calcualtes the length at runtime instead of compile time. That obviously creates problems! So you can't use %len() in a parameter of %dec().

One simple solution is to FORCE %len() to be done at compile time by using it as part of a constant. For example, the following code should work:

     D cuPDLen         c                   const(%len(cuPD))
     D cuPDDec         c                   const(%decpos(cuPD))

      /free

            cuPD = %dec( %char( %date: *ISO0 )
                       : cuPDLen
                       : cuPDDec );

The reason it solves the problem is that %len() is being used as part of a constant. Constants are always defined at compile-time and can't be changed during a program run. That way, RPG feels safe in using the values at compile-time.

However, in your particular situation, I wouldn't use %dec() at all, I'd use %int() because it makes the code easier to read. I don't understand why you're using %dec() here, since the result of the expression doesn't have to be the exact same data type and size as the variable you're assigning it to.

Just as you can assign a value from a packed field to a zoned field, or the value of a literal to a packed field, you can assign the value of an integer to a packed(8,0) field quite easily.

So I'd use %int(), and accomplish the same thing in 1 line of code that took 5 lines, above. As follows:

           cuPD = %int( %char( %date: *ISO0) );

I think that code will be easier for the next guy to read, and therefore will make program maintenace easier.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.