J3/04-237 Date: Feb 11, 2004 To: J3 From: Dan Nagle Subject: Assumed Kind Support for generic programming is an important feature of modern programming languages; Fortran's support is, at present, very limited. Some proposals for generic programming propose to restrict a generic procedure to "universal operations", by which is meant: assignment, pointer assignment, array element selection, and other operations which may be applied to any type. The grave disadvantage of this limitation is obvious. For example, a special function (Bessel, zeta, erf(), erfc() etc) cannot be coded for any kind of real (or complex) using this approach. An alternative I call Assumed Kind is proposed here. The type, but not the kind, of the procedure is coded. In addition to allowing operations, this has the advantage of allowing the compiler to check for errors when the procedure is first encountered, an advantage especially for very large programs. I am not proposing that assumed kind be the only genericity considered for f03++, I do propose that assumed kind is complementary to some other approaches, and ought to be on the menu during the discussion of features for f03++ genericity. Number: Title: Assumed Kind Submitted By: J3 Status: For Consideration References: Basic Functionality: The result kind of a function, or the result kind of an intent( out) procedure argument, may be defined to be the same kind as an intent( in) or intent( inout) actual argument. The actual argument kind is not known until the procedure reference is found. That is, the kind of the function result is found by the kind of an actual argument. Rationale: When writing a module containing, say, special functions, one often must write the code for the function for each kind of, say type real, supported. This leads to two difficulties: First, that the "same" code is repeated for different kinds, and second, that the module must have different versions for different processors in order to support all kinds supported by each processor. Conditional compilation is a second rate solution. Estimated Impact: The compiler must check the procedure with an assumed kind argument, and compile it when the kind is known. Compilers do no such thing at present. However, the types of all arguments are known when the procedure is first encountered, so erroneous code may be flaged immediately. Detailed Specification: Example: real( kind= kind( x)) function bessel( x, n) real( kind= *), intent( in) :: x integer, intent( in) :: n ... ! initialize computation bessel = 0.0_kind( x) ! compute bessel ... bessel = return end function bessel If bessel is called with kind( x) == single, a single precision bessel is compiled. If bessel is called with kind( x) == double, a double bessel is compiled. All kinds supported by any processor are available, no compilation failure occurs due to a kind not supported being present. And yet there is only one version of the function to support. The alternative is to force the programmer to code the function once for each kind supported by each processor where this code is needed, and then change the module so that only all kinds supported by each processor are present. History: