Re: compilation problem with module function interface definition
- From: Richard E Maine <nospam@xxxxxxxxxxxxx>
- Date: Wed, 27 Apr 2005 08:00:35 -0700
In article <R_Cbe.8734$Bc7.2299@xxxxxxxxxxxxxxxxxxxx>,
Phony Account <phaccount@xxxxxxxxxxxx> wrote:
> Other than lack of fortran95/03 knowledge, what other cardinal rule of
> the modern fortran language is the following snippet breaking?
>
> The module compiles cleanly with the interface block commented out. If
> I include it, ifort complains about type (foo) not being defined.
... [all but the most interesting lines elided]
> MODULE func_intrfc_test
> type foo
> end type foo
>
> !!$INTERFACE test_generic
> !!$ real FUNCTION test_particular(obj,arg) RESULT(result)
> !!$ type(foo) obj
> !!$ real,intent(in)::arg
> !!$ end FUNCTION test_particular
> !!$END INTERFACE
>
> CONTAINS
> real FUNCTION test_particular(obj,arg) RESULT(result)
Donald Fredkin gave the short version (just the fix). Let me explain a
bit. With the interface block uncommented, This has 2 separate
problems, both fairly common misunderstandings.
First, as Donald showed, you just want a module procedure statement (or,
as of f2003, the "module" keyword is optional) in the interface block,
not a complete interface body.
A universal rule relating to module procedures - never write interface
bodies for them, and for that matter never declare anything else about
them (like their type or the external attribute). If you do any of these
things, you'll mess something up. The interface of a module procedure is
always explicit; there is no need to repeat it in an interface body. If
you try to redeclare anything about a module procedure, by writing an
interface body, a type declaration (for functions) or an external
attribute, then that will tell the compiler you do *NOT* want the module
procedure, but instead want some external procedure of that name...
which probably doesn't exist.
All you need to put in the interface block is a single statement to tell
the compiler that this procedure should be one of the specifics. Don't
duplicate all the details.
Note by the way the difference between an "interface block" and an
"interface body". I fond the similarity of terms confusing (and I'm not
the only one). An interface block is everything from the INTERFACE
statement to the END INTERFACE. It might or might not include interface
bodies. An interface body is the bit that starts with a function or
subroutine statement and looks just like a function or subroutine with
the "guts" omitted.
The rule about not duplicating stuff from a module is actually more
general than module procedures. You never redeclare *ANYTHING* (well,
almost anything - the exceptions are mostly obscure and not relevant
here and this is already too long) that you use from a module. The
declarations are all in the module, and it is an error to duplicate them
elsewhere, even if the duplicate declarations match. In this case, you
aren't USEing the module, but the same idea applies in that the
interface to the procedure is already declared and you shouldn't try to
redeclare it.
About the only times you need interface bodies are for external
procedures (a module procedure is not an external one - it is a module
one) and for things like dummy procedures, procedure pointers, and
abstract interfaces (the later 2 new to f2003). External procedures have
no other mechanism defined by the language for the information declared
in the procedure to get communicated, so you need to duplicate it. Dummy
procedures, procedure pointers, and abstract interfaces don't directly
pertain to a particular procedure at compile time, so their interface
can't be automatically picked up. In my opinion, some of the Fortran
textbooks place too much emphasis on interface bodies, resulting in
their overuse. I would place the emphasis on coding in ways that don't
use interface bodies, presenting interface bodies as being for special
situations instead of the normal case.
Ok, that was error 1. If you take out the interface body, you will
bypass error 2, but let me briefly mention it anyway. It was a
controversial issue at the first J3 meeting I ever went to in 1991...
and it has since come back into the controversy (I think J3 made the
wrong decision in 1991, but that's spilt milk). Interface bodies are
special in that they do not (normally - f2003 provides a way to work
around it) get things by host association like every other kind of
contained scoping unit does. In particular, the declaration of the type
foo in your module gets into the actual subroutine by host association
from the module, but it doesn't get into the interface body. This is
awkward if it tuns out that you do want to have an interface body that
uses foo (perhaps a specific is an external procedure). There are
awkward workarounds in f90/f95, and f2003 adds what I consider to be a
hack to avoid the worst of the awkwardness. I'll not go into those in
detail, as you don't have that situation and the subject has been plenty
adequately covered here in the past. I don't see particular need to go
through all the discussion again when it isn't the current question.
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
.
- Follow-Ups:
- Re: compilation problem with module function interface definition
- From: Phony Account
- Re: compilation problem with module function interface definition
- References:
- compilation problem with module function interface definition
- From: Phony Account
- compilation problem with module function interface definition
- Prev by Date: Re: Problem when writing unformatted data to file using Intel Fortran Compiler
- Next by Date: Re: Detect EOF for direct access, unformatted files?
- Previous by thread: Re: compilation problem with module function interface definition
- Next by thread: Re: compilation problem with module function interface definition
- Index(es):
Relevant Pages
|