# Is introspection possible?

**URL:** <https://fortran-lang.discourse.group/t/is-introspection-possible/10433>\
**Category:** Uncategorized\
**Created:** [October 8, 2025, 6:20pm UTC](https://fortran-lang.discourse.group/t/is-introspection-possible/10433 "2025-10-08T18:20:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simong](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@simong](https://fortran-lang.discourse.group/u/simong)\
**Post date:** [October 8, 2025, 6:20pm UTC](https://fortran-lang.discourse.group/t/is-introspection-possible/10433/1 "2025-10-08T18:20:39Z")

</div>

I’m writing a procedure that has as an input a function with a defined interface. Is it possible to declare a variable in this procedure to be the same type as an argument to the function, an equivalent of _decltype_ in C++?

Example

```auto
module numerical
    
    implicit none
    
    abstract interface
    
        function value_fun(v, err)
            real(8), intent(in) :: v
            integer, optional, intent(out) :: err ! 0 if no error
        end function value_fun
    
    end interface

contains

    subroutine my_proc(f)
        procedure(value_fun) :: f
      
! Declare x to be the same type as the first argument of f  
! typeof<f,1> :: x
    end subroutine my_proc

end module numerical

```

---

<div class="post-metadata">

**Author:** ![jwmwalrus](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jwmwalrus/32/4483_2.png) [@jwmwalrus](https://fortran-lang.discourse.group/u/jwmwalrus)\
**Post date:** [October 8, 2025, 7:18pm UTC](https://fortran-lang.discourse.group/t/is-introspection-possible/10433/2 "2025-10-08T19:18:38Z")

</div>

Not answering your post, but it made me wonder if the opposite holds when combined with `typeof`. Say:

```fortran
module numerical    
    implicit none

contains
    subroutine my_proc(f)
        real(8) :: x 
        interface
            function f(v, err)
                import
                typeof(x), intent(in) :: v
                integer, optional, intent(out) :: err ! 0 if no error
            end function
        end interface
        ...      
    end subroutine
end module numerical

```

Would that be valid code (once we find a compiler with `typeof` support)?

---

<div class="post-metadata">

**Author:** ![davidpfister](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/davidpfister/32/3949_2.png) [@davidpfister](https://fortran-lang.discourse.group/u/davidpfister)\
**Post date:** [October 8, 2025, 8:40pm UTC](https://fortran-lang.discourse.group/t/is-introspection-possible/10433/3 "2025-10-08T20:40:53Z")

</div>

I wanted to do something similar when I wrote [benchmark.f](https://github.com/davidpfister/benchmark.f), and I managed to achieve something very close with a combination of unlimited polymorphism (`class(*)`) and `associate`. Unfortunately the code does not seem to be portable and only works as expected with Intel compilers and not with gfortran. See the section [Associate and unlimited polymorphism](https://davidpfister.github.io/benchmark.f/compiler_differences.html#autotoc_md6).

To know more about the experimentations and the work around you can have a look at the [Method](https://github.com/davidpfister/benchmark.f/blob/master/src/Method.f90) derived type.

---

<div class="post-metadata">

**Author:** ![simong](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@simong](https://fortran-lang.discourse.group/u/simong)\
**Post date:** [October 8, 2025, 10:35pm UTC](https://fortran-lang.discourse.group/t/is-introspection-possible/10433/4 "2025-10-08T22:35:25Z")

</div>

Thanks - will do
