Fortran 2003

26 views
Skip to first unread message

Cliff

unread,
Dec 12, 2008, 8:28:10 PM12/12/08
to
Hi,

I'm trying to use the procedure pointer to create a method for a
derived type. The code is as follows:


module mymod

type :: mytype
integer :: i
procedure(seti_interface), pointer :: seti
end type

interface
subroutine seti_interface(this,i)
import
type(mytype), intent(out) :: this
integer, intent(in) :: i
end subroutine seti_interface
end interface

contains

subroutine seti_interface(this,i)
type(mytype), intent(out) :: this
integer, intent(in) :: i
this%i=i
end subroutine seti_interface

end module mymod

program Test_03
use mymod
implicit none

type(mytype) :: m

call m%seti(5)

end program Test_03


Unfortunately this doesn't compile (complaining about a name conflict
for seti_interface). I'm using Intel Visual Fortran v11.

Any ideas about where I'm going wrong?

Thanks in advance,

Cliff

Richard Maine

unread,
Dec 12, 2008, 8:36:15 PM12/12/08
to
Cliff <c.che...@physics.org> wrote:

> module mymod
>
> type :: mytype
> integer :: i
> procedure(seti_interface), pointer :: seti
> end type
>
> interface
> subroutine seti_interface(this,i)
> import
> type(mytype), intent(out) :: this
> integer, intent(in) :: i
> end subroutine seti_interface
> end interface
>
> contains
>
> subroutine seti_interface(this,i)
> type(mytype), intent(out) :: this
> integer, intent(in) :: i
> this%i=i
> end subroutine seti_interface
>
> end module mymod

...


> Unfortunately this doesn't compile (complaining about a name conflict
> for seti_interface). I'm using Intel Visual Fortran v11.

Don't write an interface body for a module procedure. Seti_interface is
a module procedure and thus already has an explicit interface. It is an
error to try to also specify its interface with an interface body. That
looks to the compiler as though you are specifying the interface of some
external procedure, but the external procedure has the same name as the
module one - this a conflict.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain

Cliff

unread,
Dec 12, 2008, 9:18:49 PM12/12/08
to
On 13 Dec, 01:36, nos...@see.signature (Richard Maine) wrote:
> domain: summertriangle           |  -- Mark Twain- Hide quoted text -
>
> - Show quoted text -

Richard,

Thanks for the quick response.

I started writing the code as you suggest but added the interface
because I got the compiler error: "The specified interface is not
declared. [SETI_INTERFACE]".

I don't understand why it isn't picking up the module procedure.

Cliff

Simulate

unread,
Dec 12, 2008, 10:04:51 PM12/12/08
to
There is an example from intel forum. May be helpful for you

module proc_component_example

type t
real :: a
procedure(print_int), pointer, &
nopass :: proc
end type t

abstract interface

subroutine print_int (arg, lun)
import
type(t), intent(in) :: arg
integer, intent(in) :: lun
end subroutine print_int

end interface

contains

subroutine print_me (arg, lun)
type(t), intent(in) :: arg
integer, intent(in) :: lun
write (lun,*) arg%a
end subroutine print_me

subroutine print_my_square (arg, lun)
type(t), intent(in) :: arg
integer, intent(in) :: lun
write (lun,*) arg%a**2
end subroutine print_my_square

end module proc_component_example


program main

use proc_component_example
use iso_fortran_env, only : output_unit

type(t) :: x

x%a = 2.71828

x%proc => print_me
call x%proc(x, output_unit)
x%proc => print_my_square
call x%proc(x, output_unit)

end program main

Damian

unread,
Dec 12, 2008, 10:33:41 PM12/12/08
to

Just in case it helps any, you'll have full access to type-bound
procedures if you have access to the IBM, Cray, or NAG compilers and
you'll have a provisional (though non-standard) version of type-bound
procedures if you compile with gfortran. Then you won't have to use
procedure pointers to emulate methods.

Damian

Cliff

unread,
Dec 12, 2008, 10:35:49 PM12/12/08
to

That helped thanks.

Cliff

Richard Maine

unread,
Dec 12, 2008, 10:58:57 PM12/12/08
to
Cliff <c.che...@physics.org> wrote:

> On 13 Dec, 01:36, nos...@see.signature (Richard Maine) wrote:

> > Don't write an interface body for a module procedure. Seti_interface is
> > a module procedure and thus already has an explicit interface. It is an

> > error to try to also specify its interface with an interface body. ...

> I started writing the code as you suggest but added the interface
> because I got the compiler error: "The specified interface is not
> declared. [SETI_INTERFACE]".
>
> I don't understand why it isn't picking up the module procedure.

I don't either, unless it is just that the compiler doesn't yet fully
implement the f2003 procedure statement (which seems quite plausible and
would be my first guess). In any case, writing an interface body for a
module procedure won't fix it; that just adds a different error.

George

unread,
Dec 12, 2008, 11:19:37 PM12/12/08
to
On Fri, 12 Dec 2008 18:18:49 -0800 (PST), Cliff wrote:

>> Don't write an interface body for a module procedure. Seti_interface is
>> a module procedure and thus already has an explicit interface. It is an
>> error to try to also specify its interface with an interface body. That
>> looks to the compiler as though you are specifying the interface of some
>> external procedure, but the external procedure has the same name as the
>> module one - this a conflict.
>>
>> --
>> Richard Maine                    | Good judgment comes from experience;
>> email: last name at domain . net | experience comes from bad judgment.
>> domain: summertriangle           |  -- Mark Twain- Hide quoted text -
>>
>> - Show quoted text -
>
> Richard,
>
> Thanks for the quick response.
>
> I started writing the code as you suggest but added the interface
> because I got the compiler error: "The specified interface is not
> declared. [SETI_INTERFACE]".
>
> I don't understand why it isn't picking up the module procedure.
>
> Cliff

You seem to be posting through the google portal, which is a hardship for
proper quoting. You might want to think of what better access to usenet
could do for OO fortran game. I recommend news.individual.net .

I tried a few versions of the interface without success. The compiler
never liked the type of m%seti despite my best effort. I was using g95.

Good luck. Come back and teach us how you get it done.
--
George

The course of this conflict is not known, yet its outcome is certain.
Freedom and fear, justice and cruelty, have always been at war, and we know
that God is not neutral between them.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/

Gary Scott

unread,
Dec 13, 2008, 10:48:56 AM12/13/08
to
Simulate wrote:
> There is an example from intel forum. May be helpful for you
>
> module proc_component_example
>
> type t
> real :: a
> procedure(print_int), pointer, &
> nopass :: proc
> end type t
>
> abstract interface

I think I wish this had been designed as an attribute:

interface, abstract

I don't like the "abstract" being ended by an "end interface" (shoulda
been "end abstract interface" at least.


--

Gary Scott
mailto:garylscott@sbcglobal dot net

Fortran Library: http://www.fortranlib.com

Support the Original G95 Project: http://www.g95.org
-OR-
Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html

If you want to do the impossible, don't hire an expert because he knows
it can't be done.

-- Henry Ford

Cliff

unread,
Dec 13, 2008, 3:27:00 PM12/13/08
to
Thanks for all you responses.

The following program works:

!===================================================
module mymod

type :: mytype
integer :: i
procedure(set_int_value), pointer :: seti
end type

abstract interface
subroutine set_int_value(this,i)
import
type(mytype), intent(inout) :: this
integer, intent(in) :: i
end subroutine set_int_value
end interface

contains

subroutine seti_proc(this,i)
type(mytype), intent(inout) :: this
integer, intent(in) :: i
this%i=i
end subroutine seti_proc

end module mymod

program Test_03
use mymod
implicit none

type(mytype) :: m

m%seti=>seti_proc

call m%seti(5)

print *,'m%i = ', m%i

end program Test_03
!===================================================

However, what i'd really like to avoid (or not even be able to do) is
the m%seti=>seti_proc assignment in the main program. What I really
want is seti to be a type-bound procedure and the pointer to the
procedure to be private to the type.

I've tried using contains in the type but I don't think this is
supported (yet) in Intel VF (which I have to use by the way).

I'll keep trying but would appreciate any ideas in the meantime.

Cheers
Cliff

Paul van Delst

unread,
Dec 15, 2008, 6:16:02 PM12/15/08
to
Gary Scott wrote:
> Simulate wrote:
>> There is an example from intel forum. May be helpful for you
>>
>> module proc_component_example
>>
>> type t
>> real :: a
>> procedure(print_int), pointer, &
>> nopass :: proc
>> end type t
>>
>> abstract interface
>
> I think I wish this had been designed as an attribute:
>
> interface, abstract
>
> I don't like the "abstract" being ended by an "end interface" (shoulda
> been "end abstract interface" at least.

Isn't it similar to a "PURE" or "ELEMENTAL" modifier (clause, prefix, whatever)? If you have

PURE FUNCTION x(...)

you don't do a

END PURE FUNCTION x

cheers,

paulv

Gary Scott

unread,
Dec 15, 2008, 8:01:47 PM12/15/08
to

yeah, I guess. There are statements in some philosophical pieces on the
design of Fortran 90-2k3 that indicate a goal of "regularizing" Fortran.
(e.g. ACM and Fortran Forum articles). But it seems to have been done
inconsistently. There are a number of areas where something could have
been viewed as an attribute and that would have led to a more intuitive,
clean, and consistent design.

>
> cheers,
>
> paulv

George

unread,
Dec 16, 2008, 12:12:36 AM12/16/08
to

I'm still uncertain where I fail here, but it looks like g95 and gfortran
aren't there yet:

C:\MinGW\source>g95 -Wall cliff4.f03 -o x.exe
In file cliff4.f03:34

call m%seti(5)
1
Error: Type mismatch in parameter 'this' at (1). Passing INTEGER(4) to
TYPE(myt
ype)

C:\MinGW\source>gfortran -Wall cliff4.f03 -o x.exe
cliff4.f03:5.17:

procedure(set_int_value), pointer :: seti
1
Error: Fortran 2003: Procedure components at (1) are not yet implemented in
gfor
tran
cliff4.f03:27.13:

use mymod
1
Fatal Error: Can't open module file 'mymod.mod' for reading at (1): No such
file
or directory
gfortran: Internal error: Aborted (program f951)
Please submit a full bug report.
See <http://gcc.gnu.org/bugs.html> for instructions.

C:\MinGW\source>
--
George

Only a liberal senator from Massachusetts would say that a 49 percent
increase in funding for education was not enough.

George

unread,
Dec 16, 2008, 12:36:37 AM12/16/08
to


http://i429.photobucket.com/albums/qq15/george196884/fortran36.jpg

Pure has a very circular definition in the standard. I think it would be a
good candidate for inclusion in the glossary. I would call it that which
has no side effects.

I found a new way to get information out of the standard. On adobe reader,
the find capability is ham-handed while the search capability is dynamite.
It goes through all 586 pages to find your terms and creates another window
with clickable entries. So you can bounce around the standard without
losing your place, as you might going after terms manually. This might be
old hat to persons who have more experience, but readability is a big issue
for some.
--
George

Free nations are peaceful nations. Free nations don't attack each other.
Free nations don't develop weapons of mass destruction.

Reply all
Reply to author
Forward
0 new messages