# Purpose of kind parameters in derived types

**URL:** <https://fortran-lang.discourse.group/t/purpose-of-kind-parameters-in-derived-types/2383>\
**Category:** Uncategorized\
**Created:** [December 9, 2021, 12:25pm UTC](https://fortran-lang.discourse.group/t/purpose-of-kind-parameters-in-derived-types/2383 "2021-12-09T12:25:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfurquan](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@mfurquan](https://fortran-lang.discourse.group/u/mfurquan)\
**Post date:** [December 9, 2021, 12:25pm UTC](https://fortran-lang.discourse.group/t/purpose-of-kind-parameters-in-derived-types/2383/1 "2021-12-09T12:25:25Z")

</div>

Currently kind parameters are mandated to be compile time constants. Suppose I want to write a library without committing to a value of precision (this is how good libraries should be, user should be able to control the precision used in types and functions) while compiling it. In C++, I will switch from float to double et cetra in the template parameter. In Fortran, this could be even more elegant by leaving the choice of precision to the user via a kind parameter. However, it does allow writing functions/subroutines with arguments of unspecified precision. I feel this is a very severe limitation of a potentially powerful feature which could significantly reduce the use of preprocessors.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [December 9, 2021, 12:46pm UTC](https://fortran-lang.discourse.group/t/purpose-of-kind-parameters-in-derived-types/2383/2 "2021-12-09T12:46:06Z")

</div>

Are your comments aimed at functions/subroutines, derived types, or both?

For derived types you have the parameterized version, which give you the generic kind. I’ve got an example of a PDT for LU factorization of a square matrix available [here](https://gist.github.com/ivan-pi/2ce101cf5349a062f6063f66083517c3).

The derived type comes together quite easily:

```fortran
  type :: lu_workspace(wp,n)
    integer, kind :: wp
    integer, len :: n
    real(wp) :: a(n,n)
    real(wp) :: b(n)
    integer :: ipiv(n)
    logical :: factorized = .false.
  end type

```

Problems show up when you start adding procedures. In that case you cannot avoid boilerplate (without resorting to preprocessing):

```fortran
  integer, parameter :: sp = kind(1.0e0)
  integer, parameter :: dp = kind(1.0d0)

  interface factorize
    module procedure factorize_sp
    module procedure factorize_dp
  end interface

contains

  subroutine factorize_sp(this,info)
    use lapack, only: lapack_factorize => sgetrf
    type(lu_workspace(sp,*)), intent(inout) :: this
    integer, intent(out), optional :: info  
    include "lu_pdt.inc"
  end subroutine

  subroutine factorize_dp(this,info)
    use lapack, only: lapack_factorize => dgetrf
    type(lu_workspace(dp,*)), intent(inout) :: this
    integer, intent(out), optional :: info  
    include "lu_pdt.inc"
  end subroutine

```

I will note that in this usage case, the underlying LAPACK is only available in single and double precision, so declaring them up-front makes sense to me (in a perfect world, we’d also have a generic LAPACK).

I can merely suggest you have a look and participate in the [j3-fortran/generics](https://github.com/j3-fortran/generics) repository.

---

<div class="post-metadata">

**Author:** ![mfurquan](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@mfurquan](https://fortran-lang.discourse.group/u/mfurquan)\
**Post date:** [December 9, 2021, 3:04pm UTC](https://fortran-lang.discourse.group/t/purpose-of-kind-parameters-in-derived-types/2383/3 "2021-12-09T15:04:00Z")

</div>

I was concerned about type bound procedures. One should be able to pass the parameterization from the derived type to the type bound procedures. Your solution is good for independent procedures.

Thanks for suggesting the j3 repo.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [December 10, 2021, 2:56am UTC](https://fortran-lang.discourse.group/t/purpose-of-kind-parameters-in-derived-types/2383/4 "2021-12-10T02:56:44Z")

</div>

> [@mfurquan](#):
>
> I was concerned about type bound procedures. One should be able to pass the parameterization from the derived type to the type bound procedures. …

The option listed by @ivanpribec is applicable to type-bound procedures also with

1. the usual requirement the passed-object dummy argument (`this` per code above) be polymorphic (`class(lu_workspace(sp,*)) ..` given every derived type in Fortran is an **extensible** type per current standard and
2. generic interface is also type-bound

```Fortran
type :: lu_workspace(wp,n)
    integer, kind :: wp
    integer, len :: n
    ..
  contains
    ..
    generic :: factorize => ..
  end type

```
