Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

What's TYPE accept

Allamarein
Beginner
946 Views

I found these two TYPE declarations are not welcome:

1. EXTERNAL in type is not accept

TYPE myvar
EXTERNAL :: fun
END TYPE

2. this trick is not allowed

TYPE myvec
INTEGER :: n
REAL*8, DIMENSION(1:n) :: x
END TYPE

Some workarounds is possible?

 

0 Kudos
8 Replies
Steven_L_Intel1
Employee
946 Views

Instead of EXTERNAL you want a procedure pointer. You would then have to assign some procedure to the pointer before you could use it.

It's not clear to me what you want in the second case. Maybe:

type myvec
REAL(8), ALLOCATABLE :: X(:)
end type myvec

and then allocate X to the desired size.

0 Kudos
Allamarein
Beginner
946 Views

Thank you STEVE.

I guess the pointer is the solution to my problem: could you provide an example?
 

The second point, using an allocatable, is the solution.

0 Kudos
Steven_L_Intel1
Employee
946 Views
type mytype
procedure(), nopass, pointer :: mp
end type mytype

external sub1, sub2

type(mytype) :: r

r%mp => sub1
call r%mp
r%mp => sub2
call r%mp
end

subroutine sub1
print *, "I'm in sub1"
end subroutine sub1

subroutine sub2
print *, "I'm in sub2"
end subroutine sub2

If I were writing this, I would not use external but would declare an ABSTRACT INTERFACE for the subroutines and would have explicit interfaces for sub1 and sub2. The NOPASS attribute is required when there is implicit interface.

0 Kudos
mecej4
Honored Contributor III
946 Views

Allamarein: I think that you are more likely to receive useful answers if you explain what your intentions are. Using pointers may solve one problem but create new problems. As to your second "trick", allocatable arrays may be a solution, but note that such arrays often need explicit allocation, and their sizes can be any positive integer. If you want to put limits on array sizes, another solution exists. If, for example, you want 3-vectors for x-y-z coordinates and 4-vectors for t-x-y-z coordinates, you can do

TYPE myvec(n)
   INTEGER, LEN :: n        ! length parameter of type
   REAL, DIMENSION(1:n) :: x
END TYPE

type(myvec(n=3)) :: point
type(myvec(n=4)) :: event

point%x = [1.0, 2.0, 3.0]
event%x = [2015.0, 1.0, 2.0, 3.0]

 

0 Kudos
FortranFan
Honored Contributor III
946 Views

Look into the books listed in Steve's Dr Fortran blog for more details and examples for many of these aspects: https://software.intel.com/en-us/blogs/2013/12/30/doctor-fortran-in-its-a-modern-fortran-world

For your first question on procedure pointers, here is a simple example:

program p

   abstract interface

      function Ifun() result(str)

         character(len=:), allocatable :: str

      end function Ifun

   end interface

   type :: t
      procedure(Ifun), nopass, pointer :: fun => null()
   end type t

   type(t) :: foo

   foo%fun => myfun

   print *, " message from myfun: ", foo%fun()

   stop

contains

   function myfun() result(str)

      character(len=:), allocatable :: str

      str = "Hello World!"

   end function myfun

end program p

On your second question, perhaps you are interested in parameterized derived types (PDTs)?  Look into PDTs in Modern Fortran Explained.  Here's a simple example for that too:

program p

   type :: t(n)
      integer, len :: n
      real :: x(n)
   end type t

   type(t(5)) :: foo
   integer :: i
   real :: x

   print *, " size of x: ", foo%n

   do i = 1, foo%n
      call RANDOM_NUMBER(x)
      foo%x(i) = x
   end do

   print *, " x = ", foo%x

   stop

end program p

 

0 Kudos
Allamarein
Beginner
946 Views
I think the pointer is the right solution, despite the fact it is slightly complicated for my skills. Is it compatible with Fortran 95?
0 Kudos
Steven_L_Intel1
Employee
946 Views

No, procedure pointers were not in Fortran 95. They were introduced in Fortran 2003. There isn't any compiler you should be using that doesn't support these.

0 Kudos
FortranFan
Honored Contributor III
946 Views

No, both of the above features are post Fortran 95.

0 Kudos
Reply