Poll: Preferred style for kind parameters for real and character

When you declare a real variable with a kind parameter, which style do you prefer:

Real declaration formatting
  • real(dp)
  • real(kind=dp)
  • both / undecided (please comment below)

0 voters

(Anything can be used instead of dp, such as sp, wp, qp, real32, …)

What about a character variable?

Character declaration formatting
  • character(*)
  • character(len=*)
  • both / undecided (please comment below)

0 voters

(One can also use : or a number instead of *.)

This question came up in https://gitlab.com/lfortran/lfortran/-/merge_requests/635 about what the automatic formatter should do.

I always use real(dp), but it turns out I use both character(:) as well as character(len=:) in my codes, inconsistently. Now when I think about it, I think I would prefer to use the shorter version character(:), character(*) and character(n).

1 Like

For real/complex/integer kinds, I use the short version. For characters, I spell it out because they have both kind and len type parameters. As an exception, I omit the length for single-character variables. In a world where I could use PDTs without worry, I’d always do the long form.

real(wp) :: x
character(len=12) :: ascii_string
character(len=12, kind=ucs4) :: unicode_string
character :: ascii_char
character(kind=ucs4) :: unicode_char
type(matrix(kind=wp, m=10, n=8)) :: pdtmat_10x8
7 Likes

I do the same with REAL, INTEGER variables:

   use, intrinsic :: iso_fortran_env, only : .. I4 => int32, I8 => int64, ..
   use, intrinsic :: ieee_arithmetic, only : .., ieee_selected_real_kind, ..
   ..
   integer(I4) :: n
   ..
   integer, parameter :: WP = ieee_selected_real_kind( p=.. )
   ..
   real(WP) :: x

Note: all the compute environments where I get to work are supposed to follow IEEE floating-point arithmetic anyway, so I employ IEEE modules in Fortran and the IEEE functions.

But now, I ALWAYS use len= form with CHARACTER objects even though that makes it rather verbose in code. Being a polyglot when it comes to programming languages has long been an imperative in any form of computing including scientific and technical and I feel I myself and especially any colleagues who may read the code can overlook the intricacies of CHARACTER type in Fortran given the need to work with other languages so often at other times.

And the same with PDTs: I agree with the long form usage.

P.S.> Re: “In a world where I could use PDTs without worry …” I am really hoping Fortran compilers can get their act together and fix their problems with implementation of PDTs, pronto! Generics is at least about 6 years away for the standard (possibly much longer) and if the processors struggle with the LENGTH-type parameter in PDTs, they “ain’t seen nothing yet” in terms of the complexity with semantics that will come about trying to serve the most rudimentary of programming needs for scientists and engineers with Generics.

1 Like

I find the len=* or len=: notation visually jarring in its use of the “=” symbol. One expects to see a value or expression to the right of =. The most common usage I see is character(*) or character(:). While character does have a KIND type parameter, I’ve never seen it used in an actual program (i.e. not an artificial test base example code).

1 Like

I find the kind specifier is useful in interfacing to C codes:

character(kind=c_char) :: str(*)

(Even though I am not aware of compilers were the default character kind is not equal to C’s char type).

2 Likes

Thanks everybody who answered so far. Currently it looks like most people prefer real(dp) and character(len=*), so I made that the default in lfortran fmt. However, I want to make it configurable in the future, so that one can get exactly the formatting one wants.

As @billlong mentioned, I feel the same way, and even though in my codes I have mostly used character(len=*), I will try to use character(*) from now on and see how it feels.

I share @FortranFan’s concern about people new to Fortran, but I think the confusion is low, one must learn how to work with strings in Fortran anyway, and simply get used to writing character(:) or character(*). With a good tutorial how to use strings at fortran-lang (any volunteers? :), I think things will be rather clear.