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

Two dummy arguments with CLASS attribute

Jon_D
New Contributor I
326 Views

Hello,

In the past I was successfully able to specify two dummy arguments of a subroutine with the CLASS attribute. However, the attached example code is throwing a compiler error (see in the "class_data" module the ImplementMethod subroutine that overrides the abstract Method subroutine from the abstract data type BaseType). Am I getting this error beacuse I am dealing with abstract data types, abstract methods and overriding procedures? Is such an approach not allowed in Fortran?

I am using the latest version of the Intel Fortran under Windows 7.

Thanks for any help.

Jon

[fortran]module class_surrogate
    implicit none
    
    type,abstract :: SurrogateType
    end type

end module
    


module class_a
  use class_surrogate
  implicit none
 
  type,extends(SurrogateType) :: AType
      integer :: a
  end type

end module
    



module class_base
  use class_surrogate
  implicit none
 
  type,abstract :: BaseType
  contains
     procedure(abstract_method),pass,deferred :: method
  end type
 
  abstract interface
    subroutine abstract_method(MyData,SuppData)
       import               :: BaseType,SurrogateType
       class(BaseType)      :: MyData
       class(SurrogateType) :: SuppData
    end subroutine
  end interface
 
end module
    
    
module class_data
   use class_base
   use class_a
   implicit none
   
   type,extends(BaseType) :: DataType
       integer :: n
       integer,allocatable :: i(:)
   contains
       procedure,pass :: method => ImplementMethod
   end type
   
contains
    
   subroutine ImplementMethod(MyData,SuppData)
      class(DataType) :: MyData
      class(AType)    :: SuppData
      
      !Computations
      
   end subroutine
   
end module
    
    
    
program main
  use class_data
  implicit none
 
  !Do something
end[/fortran]

0 Kudos
2 Replies
Steven_L_Intel1
Employee
326 Views

The error message tells you exactly what is wrong. Other than the "passed object" implied argument, all other dummy arguments for overridden procedures must be of the same declared type as that of the original. What you want here is to say class(SurrogateType) in the procedure and use SELECT TYPE to distinguish extensions.

0 Kudos
IanH
Honored Contributor II
326 Views

The characteristics of the procedure for the binding in the extension don't match the characteristics of the interface of the deferred binding in the parent - the declared type of the second argument differs.

Perhaps change the second argument to be CLASS(SurrogateType) and then use select type to cast things to AType.  (The fact that the binding could be called with a second object that is an extension of SurrogateType but not specically AType explains why the requirement in the first paragraph of this post exists).

0 Kudos
Reply