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

Calling non-deferred procedure in abstract type by parent name

Diarmaid_d_
Beginner
544 Views
So I have an abstract type with some stuff in it, and a procedure to set that stuff
module firstTypeMod
type, abstract:: firstType
    character(len=30):: tempChar
contains
  procedure(defProc), deferred:: deferredProcedure
  procedure:: setChar
end type

abstract interface
  subroutine defProc(self)
     import firstType
    class(firstType), intent(inout):: self
  end subroutine defProc
end interface

contains
 subroutine setChar(self, charIn)
   class(firstType), intent(inout):: self
  character(len=*):: charIn

  self%tempChar = charIn
 end subroutine setChar
end module firstTypeMod

I now want to define another type which extends this

module secondTypeMod
 use firstTypeMod

type, extends(firstType):: secondType
   character(len=100):: tempChar2
  contains
  procedure:: deferredProcedure => secondDef
  procedure:: setChar => overRideSetChar
end type

contains
subroutine secondDef(self)
class(secondType), intent(inout):: self

write(*,*) "OVERRIDDEN"
end subroutine secondDef

subroutine overRideSetChar(self, charIn)
 class(secondType), intent(inout):: self
 character(len=*), intent(in):: charIn

 call self%firstType%setChar(charIn)

 self%tempChar2 = charIn
end subroutine overRideSetChar

end module secondTypeMod 
However, when I try this I get the following error:
error #8314: If the rightmost part-name is of abstract type, data-ref shall be polymorphic.

 

Is it possible to do what I want directly?   I know that I can make an intermediate class that isn't abstract that will allow me to do this, but I would really prefer to avoid that.   Is it possible to call the parent of an overridden non-deferred subroutine of an abstract type?

Thank you.

Edit: A slightly modified version (to remove some typos) is attached to this post which illustrates the error.

0 Kudos
2 Replies
IanH
Honored Contributor II
544 Views

The simplest method is to just call the procedure in the firstTypeMod directly, like you would have in Fortran 95.

call setChar(self, charIn)

With the language as it is, the syntax object%binding(...) is mostly to do with dynamic dispatch.  You aren't doing dynamic dispatch (you are statically specifying the procedure) - so don't use that syntax.

0 Kudos
Diarmaid_d_
Beginner
544 Views

Yes, that is what I am doing at the moment. This appears to work fine.

0 Kudos
Reply