# 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:** 3

<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:** [November 6, 2022, 12:19am UTC](https://fortran-lang.discourse.group/t/prototype-implementation-of-fortran-generics-in-lfortran/4664/3 "2022-11-06T00:19:14Z")

</div>

> [@certik](#):
>
> … we quite urgently need more examples .

@certik ,

Can you and the contributors working on this prototype implementation in LFortran please take a look and consider the Fortran source below which is based on current standard i.e, Fortran 2018 with a rather, simple-minded implementation of a poor Fortranner version i.e., using unlimited polymorphism toward a generic 1-D sort that employs the Cormen’s algorithm?

Using this code, two Fortran compilers help generate the following output from the program:

```auto
C:\temp>gfortran kinds.f90 base.f90 string.f90 utils.f90 Qsort.f90 p.f90 -o p.exe

C:\temp>p.exe
Block 1
initial array is 42 999 -1 0 50 12
sorted array is -1 0 12 42 50 999

Block 2
initial array is 32.0 30.0 75.0 54.0 64.0 66.0
sorted array is 30.0 32.0 54.0 64.0 66.0 75.0

Block 3
Initial array is q w e r t y
sorted array is e q r t w y
Block 4
Initial array is Mary had a little lamb
sorted array is M aar lady hittle lamb
Block 5
Initial array is
Mary had a little lamb
sorted array is
Mary a had lamb little

```

So you will notice the above “generic” code supports 4 kinds of integers, 3 different floating-point kinds, 1 kind of Fortran’s CHARACTER type, and any user-defined type which extends an arbitrary base type has deferred procedures for comparer functions. The calling example program, you will notice, sorts 1-D arrays of 5 different types.

The Generics subcommittee might perhaps be able to help putting together an **equivalent** example of the above using the new, proposed TEMPLATE facility for Fortran 202X? If so, can it be an example you are looking for?

The reason I suggest something like this is because it is very **simple** , it does **not** involve any math or computer science concepts such semigroups, monoids, rings, fields, etc. and so a reader can thus focus on Fortran TEMPLATE facility and its syntax and semantics.

[sort.f90](https://fortran-lang.discourse.group/uploads/short-url/j7Q7xS2yQkLNPhB0wCZcGLXOhtO.f90) (26.2 KB)

---

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