# Prototype implementation of Fortran generics in LFortran

**URL:** <https://fortran-lang.discourse.group/t/prototype-implementation-of-fortran-generics-in-lfortran/4664>\
**Category:** Language enhancement\
**Created:** [November 5, 2022, 9:40pm UTC](https://fortran-lang.discourse.group/t/prototype-implementation-of-fortran-generics-in-lfortran/4664 "2022-11-05T21:40:02Z")\
**Posts on this page:** 1\
**Showing post:** 26

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [November 8, 2022, 9:47pm UTC](https://fortran-lang.discourse.group/t/prototype-implementation-of-fortran-generics-in-lfortran/4664/26 "2022-11-08T21:47:31Z")

</div>

@certik @tom_clune @everythingfunctional thanks again for your work on the generics, and forgive me for tagging (the benefits of a clarification outweighed the risks of bugging you with explicit tags).

Consider the trivial scenario where an addition procedure should accept all possible `real` kinds. To achieve the goal, I need to write five separate procedures under a generic interface name. Every time this problem appears, I wish I could write,

```fortran
function add(a,b) result(sum)
   real(*), intent(in) :: a
   real(kind(a)), intent(in) :: b
   real(kind(a)) :: sum
   sum = a + b
end

```

where `(*)` implies any `real` kind supported by the compiler. If only certain kinds must be supported (e.g., real32, real64), then I wish to be able to write,

```fortran
function add(a,b) result(sum)
   use, iso_fortran_env, only: real32, real64
   real({real32, real64}), intent(in) :: a
   real(kind(a)), intent(in) :: b
   real(kind(a)) :: sum
   sum = a + b
end

```

where `{}` implies the collection of specified kinds (from the set theory notation).  
Should the function work for all numeric types and kinds, then I wish to write,

```fortran
function add(a,b) result(sum)
   use, iso_fortran_env, only: real32, real64
   numeric(*), intent(in) :: a
   numeric(kind(a)), intent(in) :: b
   numeric(kind(a)) :: sum
   sum = a + b
end

```

where `numeric` would be a new intrinsic type that implies all intrinsic numeric types in the language (`integer`, `complex`, `real`).

Should this functionality be extended to the character and logical kinds, I wish to be able to write something like,

```fortran
function add(a,b) result(sum)
   use, iso_fortran_env, only: real32, real64
   type({numeric(*), character(*,*), logical(*)}), intent(in) :: a
   type(typeof(a)), intent(in) :: b
   type(typeof(a)) :: sum
   select type a
   type is (numeric(*))
       sum = a + b
   type is (logical(*))
       sum = a .or. b
   type is (character(*,*))
       sum = a//b
   end select
end

```

This would still be a compile-time resolution and provide a fast custom implementation of sum for all supported types and kinds under a single generic function name.

If such a simplex syntax were possible, the size of our codebase would reduce by more than 5 fold (there are currently five kinds supported by most compilers). I can reduce it to half with the help of preprocessing, but the interfaces within a generic interface name cannot be reduced via the preprocessor (unless some more advanced preprocessing tool is used).

I improvised these examples within the past 5 minutes. I am pretty sure there are holes in this simplistic syntax proposal. But the such concise syntax is what I have longed for several years.

---

_[View the full topic](https://fortran-lang.discourse.group/t/prototype-implementation-of-fortran-generics-in-lfortran/4664)._
