Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29273 Discussions

Compilation failure when passed-object argument follows absent optional argument

Cristian_P_
Beginner
878 Views
Module mytype_module
!Example taken from "Fortran 95/2003 explained," by Metcalf, Reid, Cohen, Page 280;

!IFORT 16.0.0 compilation fails with
!"error #6074: The number of actual arguments does not match the definition.   [WRITE]"
!when the passed-object argument follows an absent optional argument

!It seems to me that
!              call x%write
!is implemented as
!            call write_mytype(x),
!instead of 
!          call write_mytype(this = x),
!which could explain the compilation failure

   Use iso_fortran_env, Only: output_unit
   Implicit None

   Type mytype
      Real :: myvalue(4) = 0
   Contains
      Procedure, Pass(this) :: write => write_mytype
   End Type mytype

   Private :: write_mytype
Contains

   Subroutine write_mytype(unit, this)
      Integer, Optional         :: unit
      Class(mytype), Intent(In) :: this

      If(Present(unit)) Then
         Write(unit, *) this%myvalue
      Else
         Print '(4f6.3)', this%myvalue
      End If
   End Subroutine write_mytype

End Module mytype_module

Program test
Use mytype_module
   Implicit None

   Type(mytype) :: x = mytype((/1, 2, 3, 4/))

                               !WAY to make IFORT compile:
   Call x%write                !comment out this line and
   !Call x%write(output_unit)  !uncomment this, so as to provide the
                               !optional argument 

End Program test

 

0 Kudos
3 Replies
Steven_L_Intel1
Employee
878 Views

Thanks, we'll take a look.

0 Kudos
Steven_L_Intel1
Employee
878 Views

Escalated as issue DPD200381108 - thanks. I will reply here when there is news.

0 Kudos
Steven_L_Intel1
Employee
878 Views

I expect this to be fixed in the 17.0 compiler (not in the current beta.) Interestingly, the NAG compiler has the same problem - I've let them know about it. The test case isn't exactly from the book, it's an adaptation. (The book example that I saw doesn't have optional arguments.)

Furthermore, my opinion is that the standard could use some clarifying words about how the passed-object is handled to make it clearer what is supposed to happen.

0 Kudos
Reply