# Type-bound procedure with array

**URL:** <https://fortran-lang.discourse.group/t/type-bound-procedure-with-array/10691>\
**Category:** Help\
**Created:** [February 3, 2026, 8:07am UTC](https://fortran-lang.discourse.group/t/type-bound-procedure-with-array/10691 "2026-02-03T08:07:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [February 3, 2026, 8:07am UTC](https://fortran-lang.discourse.group/t/type-bound-procedure-with-array/10691/1 "2026-02-03T08:07:24Z")

</div>

I have a question about invoking a type bound procedure on an array. I thought I knew how to do this, but what I’m trying isn’t working. Look at this code:

```fortran
   implicit none
   public :: tbp_t
   type tbp_t
      integer :: i
   contains
      procedure :: p_0 => print_0
      !procedure :: p_1 => print_1
   end type tbp_t
contains
   subroutine print_0( this, s )
      class(tbp_t), intent(in) :: this
      character(*), intent(in) :: s
      print '(a,1x,a,1x,i0)', 'print_0:', s, this%i
      return
   end subroutine print_0
   subroutine print_1( this, s )
      class(tbp_t), intent(in) :: this(:)
      character(*), intent(in) :: s
      integer :: p
      print '(a)', 'print_1:'
      do p = 1, size(this)
         call this(p)%p_0( s )
      enddo
      return
   end subroutine print_1
end module tbp_mod
program xxx
   use tbp_mod
   implicit none
   type(tbp_t) :: a, b(3)
   a%i = 42
   b%i = [1,2,3]
   call print_0( a, 'standard scalar' )
   call a%p_0( 'tbp scalar' )
   call print_1( b, 'standard array' )
   !call b%p_1( 'tbp array' )
end program xxx

```

This small program prints a derived type in the standard way, and then prints the same thing through the type bound procedure. Then the program prints an array in the standard way. But I don’t see how to print the array with a type bound procedure. The two lines that are commented out in the above program are the ones that do not work. I cannot see how to bind the procedure (`print_1()` in this code) to the derived type.

Anyone know the right incantation?

---

<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:** [February 3, 2026, 10:07am UTC](https://fortran-lang.discourse.group/t/type-bound-procedure-with-array/10691/2 "2026-02-03T10:07:34Z")

</div>

Seems like you can achive what you want if `p_0` is declared `elemental` (or in your case `impure elemental` because of the print)

```fortran
module tbp_mod
implicit none
   public :: tbp_t
   type tbp_t
      integer :: i
   contains
      procedure :: p_0 => print_0
   end type tbp_t
contains
   impure elemental subroutine print_0( this, s )
      class(tbp_t), intent(in) :: this
      character(*), intent(in) :: s
      print '(a,1x,a,1x,i0)', 'print_0:', s, this%i
      return
   end subroutine print_0
end module tbp_mod
program xxx
   use tbp_mod
   implicit none
   type(tbp_t) :: a, b(3)
   a%i = 42
   b%i = [1,2,3]
   call print_0( a, 'standard scalar' )
   call a%p_0( 'tbp scalar' )
   call b%p_0( ['tbp array', 'tbp array', 'tbp array'] )
end program xxx

```

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [February 3, 2026, 10:48am UTC](https://fortran-lang.discourse.group/t/type-bound-procedure-with-array/10691/3 "2026-02-03T10:48:31Z")

</div>

An alternative could be to use a signature like:

procedure, nopass :: p\_1 =\> print \_1

subroutine print\_1( array )

etc.

if an elemental procedure would not be appropriate (I can imagine that you might want to include the index or something like that)

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [February 3, 2026, 11:18am UTC](https://fortran-lang.discourse.group/t/type-bound-procedure-with-array/10691/4 "2026-02-03T11:18:30Z")

</div>

This seems the most concise solution, and you can just pass a scalar CHARACTER that would be used for all elements. If you want special handling for arrays (you might want to elide elements), then something like this:

```auto
module tbp_mod
implicit none
   public :: tbp_t
   type tbp_t
      integer :: i
   contains
      procedure :: p_0 => print_0
      procedure :: p_1 => print_1
   end type tbp_t
contains
   impure elemental subroutine print_0( this, s )
      class(tbp_t), intent(in) :: this
      character(*), intent(in) :: s
      print '(a,1x,a,1x,i0)', 'print_0:', s, this%i
      return
    end subroutine print_0
    subroutine print_1(this, arr, s)
      class(tbp_t), intent(in) :: this, arr(:)
      character(*), intent(in) :: s
      print '(a,1x,a,1x,*(i0,:,","))', 'print_1:', s, arr%i
    end subroutine print_1
end module tbp_mod
program xxx
   use tbp_mod
   implicit none
   type(tbp_t) :: a, b(3)
   integer :: m
   a%i = 42
   b%i = [1,2,3]
   call print_0( a, 'standard scalar' )
   call print_0( b, 'standard array' )

   call a%p_0( 'tbp scalar' )
   call b%p_0( 'tbp array' )

   call b(1)%p_1(b, 'alt')
   call b(1)%p_1([(b(1),m=1,0)], 'alt')
end program xxx

```

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [February 4, 2026, 1:09am UTC](https://fortran-lang.discourse.group/t/type-bound-procedure-with-array/10691/5 "2026-02-04T01:09:34Z")

</div>

Thanks for the suggestions. The `impure elemental` combination solves my problem.

However, it does seem odd that a more general procedure that takes an array argument cannot be a type bound procedure. My original plan was to write a scalar procedure, and an array procedure, and then bind them both to the derived type and make them generic with something like

```auto
generic :: sub => sub_scalar, sub_array

```

in the type definition. But if you can’t bind sub\_array in the first place, then none of that works.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [February 4, 2026, 12:26pm UTC](https://fortran-lang.discourse.group/t/type-bound-procedure-with-array/10691/6 "2026-02-04T12:26:39Z")

</div>

Not as elegant as intended, but this might be a workaround:

```auto
module tbp_mod
   implicit none
   public :: tbp_t
   type tbp_t
      integer :: i
   contains
      procedure :: p_0 => print_0
      procedure :: p_1 => print_1
      generic :: print => p_0, p_1
   end type tbp_t
contains
   subroutine print_0( this, s )
      class(tbp_t), intent(in) :: this
      character(*), intent(in) :: s
      print '(a,1x,a,1x,i0)', 'print_0:', s, this%i
      return
   end subroutine print_0
   subroutine print_1( this, array, s )
      class(tbp_t), intent(in) :: this
      class(tbp_t), intent(in) :: array(:)
      character(*), intent(in) :: s
      integer :: p
      print '(a)', 'print_1:'
      do p = 1, size(array)
         call array(p)%p_0( s )
      enddo
      return
   end subroutine print_1
end module tbp_mod
program xxx
   use tbp_mod
   implicit none
   type(tbp_t) :: a, b(3)
   a%i = 42
   b%i = [1,2,3]
   call a%print( 'standard scalar' )
   call b(1)%print( b, 'standard array' )
end program xxx

```

---

<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:** [February 4, 2026, 9:53pm UTC](https://fortran-lang.discourse.group/t/type-bound-procedure-with-array/10691/7 "2026-02-04T21:53:43Z")

</div>

> [@RonShepard](#):
>
> However, it does seem odd that a more general procedure that takes an array argument cannot be a type bound procedure.

It all comes down to the `passed-object` (i.e., the hidden `this`, `self` or receiver in other languages). The standard explicitly says:

> C765  
> The passed-object dummy argument shall be a scalar, nonpointer, nonallocatable dummy data object  
> with the same declared type as the type being defined; all of its length type parameters shall be assumed;  
> it shall be polymorphic (7.3.2.3) if and only if the type being defined is extensible (7.5.7). It shall not  
> have the VALUE attribute.

If you think in terms of `[obj msg]` (i.e., a message being dynamically dispatched to eventually be handled by an object), then it makes more sense for…

- the object to be scalar: one message handled by one object,  
`[this p_0(s)]`.

- the procedure to be elemental:

The option not covered (by OOP), and which is probably the one you want, is having an array of objects to somehow handle a single non-elemental message —in my example, it’s not clear which of the 5 elements of `obj(:)` would receive the message.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [February 5, 2026, 3:18am UTC](https://fortran-lang.discourse.group/t/type-bound-procedure-with-array/10691/8 "2026-02-05T03:18:51Z")

</div>

> [@jwmwalrus](#):
>
> The option not covered (by OOP), and which is probably the one you want, is having an array of objects to somehow handle a single non-elemental message —in my example, it’s not clear which of the 5 elements of `obj(:)` would receive the message.

I think your explanation is probably correct, and it makes sense.

With normal procedure calls, you can have a subprogram that takes a scalar argument, and you can have another subprogram that takes an array argument, and you can define a generic interface and reference that interface with either a scalar argument or an array argument. The compiler looks at the argument list and finds the appropriate match at compile time. I thought that one could do the same thing with type bound procedures, including the compile time resolution of the generic interface.
