- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No, both of the above features are post Fortran 95.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page