Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

User types + interface in the same module

0 views
Skip to first unread message

Dennis Wassel

unread,
Jun 17, 2008, 10:36:06 AM6/17/08
to
Hi!

Following your comments on the "allocatable components and mixed-
language" thread, I have come across another problem, though this one
surely is easier to tackle:

I define something like

MODULE MyMod
TYPE :: MyType
! stuff
END TYPE MyType

INTERFACE
SUBROUTINE Foobar(mt)
TYPE(MyType) :: mt
END SUBROUTINE Foobar
END INTERFACE

END MODULE MyMod

i.e. a user type and some functions* that work on it. Both the GNU and
Intel compilers complain they don't know about MyType, which I could
mend with USE MyMod, if only this wouldn't be recursive...

Do I really need to define my interfaces in a separate module so I can
USE MyMod?

*Foobar is written in C, that's why I define this explicit interface
together with BIND(C) and friends.

Thanks!
Dennis

Steve Lionel

unread,
Jun 17, 2008, 10:59:52 AM6/17/08
to
On Tue, 17 Jun 2008 07:36:06 -0700 (PDT), Dennis Wassel
<dennis...@googlemail.com> wrote:


>MODULE MyMod
>TYPE :: MyType
> ! stuff
>END TYPE MyType
>
>INTERFACE
> SUBROUTINE Foobar(mt)
> TYPE(MyType) :: mt
> END SUBROUTINE Foobar
>END INTERFACE
>
>END MODULE MyMod
>
>i.e. a user type and some functions* that work on it. Both the GNU and
>Intel compilers complain they don't know about MyType, which I could
>mend with USE MyMod, if only this wouldn't be recursive...

That's the correct behavior. Fortran 2003 adds IMPORT, which can be used to
make declarations in the host visible inside the interface. Intel Fortran
supports this, the GNU compilers probably do too. See
http://softwareblogs.intel.com/2006/10/05/domestic-or-imported/ for an article
I wrote on this topic.
--
Steve Lionel
Developer Products Division
Intel Corporation
Nashua, NH

For email address, replace "invalid" with "com"

User communities for Intel Software Development Products
http://softwareforums.intel.com/
Intel Fortran Support
http://support.intel.com/support/performancetools/fortran
My Fortran blog
http://www.intel.com/software/drfortran

Tobias Burnus

unread,
Jun 17, 2008, 11:04:41 AM6/17/08
to
On Jun 17, 4:36 pm, Dennis Wassel <dennis.was...@googlemail.com>
wrote:

> I define something like
>
> MODULE MyMod
> TYPE :: MyType
>   ! stuff
> END TYPE MyType
>
> INTERFACE
>   SUBROUTINE Foobar(mt)

Add here:
IMPORT
or
IMPORT MyType

>     TYPE(MyType) :: mt


Using IMPORT works with newer compilers. (IMPORT is a Fortran 2003
feature.) If you need to be compatible to Fortran 90 or 95, you need
either put MyType in a separate module or you can use a SEQUENCE type
and define the type both in the interface and in the module.

> *Foobar is written in C, that's why I define this explicit interface
> together with BIND(C) and friends.

If you use BIND(C), your compiler will almost to 100% also support
IMPORT. However, you should then also use "TYPE, BIND(C) :: MyType".

Tobias

Dennis Wassel

unread,
Jun 17, 2008, 11:16:55 AM6/17/08
to
On 17 Jun., 16:59, Steve Lionel <Steve.Lio...@intel.invalid> wrote:
> On Tue, 17 Jun 2008 07:36:06 -0700 (PDT), Dennis Wassel
>
>
>
> <dennis.was...@googlemail.com> wrote:
> >MODULE MyMod
> >TYPE :: MyType
> > ! stuff
> >END TYPE MyType
>
> >INTERFACE
> > SUBROUTINE Foobar(mt)
> > TYPE(MyType) :: mt
> > END SUBROUTINE Foobar
> >END INTERFACE
>
> >END MODULE MyMod
>
> >i.e. a user type and some functions* that work on it. Both the GNU and
> >Intel compilers complain they don't know about MyType, which I could
> >mend with USE MyMod, if only this wouldn't be recursive...
>
> That's the correct behavior. Fortran 2003 adds IMPORT, which can be used to
> make declarations in the host visible inside the interface. Intel Fortran
> supports this, the GNU compilers probably do too. Seehttp://softwareblogs.intel.com/2006/10/05/domestic-or-imported/for an article

> I wrote on this topic.
> --
> Steve Lionel
> Developer Products Division
> Intel Corporation
> Nashua, NH
>

Cheers Steve,
nicely written article, btw!

FYI: Works a treat with gcc 4.3.1

Thanks!
Dennis

James Van Buskirk

unread,
Jun 17, 2008, 1:42:50 PM6/17/08
to
"Tobias Burnus" <bur...@net-b.de> wrote in message
news:82c867b5-3ef2-40b7...@f63g2000hsf.googlegroups.com...

> If you use BIND(C), your compiler will almost to 100% also support
> IMPORT. However, you should then also use "TYPE, BIND(C) :: MyType".

Not to mention "SUBROUTINE Foobar(mt) BIND(C)" or
"SUBROUTINE Foobar(mt) BIND(C,NAME='Foobar')"

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


Arno

unread,
Jun 23, 2008, 8:32:40 AM6/23/08
to
> >MODULE MyMod
> >TYPE :: MyType
> > ! stuff
> >END TYPE MyType
>
> >INTERFACE
> > SUBROUTINE Foobar(mt)
> > TYPE(MyType) :: mt
> > END SUBROUTINE Foobar
> >END INTERFACE
>
> >END MODULE MyMod
>
> That's the correct behavior. Fortran 2003 adds IMPORT, which can be used to
> make declarations in the host visible inside the interface. Intel Fortran
> supports this, the GNU compilers probably do too. Seehttp://softwareblogs.intel.com/2006/10/05/domestic-or-imported/for an article

> I wrote on this topic.

I have to admit that I haven't read the article yet, but I use the
following structure all the time:

MODULE MyMod
TYPE :: MyType
! stuff
END TYPE MyType

CONTAINS

SUBROUTINE Foobar(mt)
TYPE(MyType) :: mt
! stuff
END SUBROUTINE Foobar

END MODULE MyMod

So I do have type declarations and functions in the same module (and
no explicit interfaces, which I find a bonus). Is this something
different? I think it is what Dennis was looking for, or am I missing
something?

Arno

Steve Lionel

unread,
Jun 23, 2008, 9:51:38 AM6/23/08
to
On Mon, 23 Jun 2008 05:32:40 -0700 (PDT), Arno <arnoi...@hotmail.com> wrote:

>I have to admit that I haven't read the article yet, but I use the
>following structure all the time:
>
>MODULE MyMod
>TYPE :: MyType
> ! stuff
>END TYPE MyType
>
>CONTAINS
>
>SUBROUTINE Foobar(mt)
> TYPE(MyType) :: mt
>! stuff
>END SUBROUTINE Foobar
>
>END MODULE MyMod

This is not what is being discussed. The issue at hand is using
host-associated declarations in an INTERFACE body. I suggest you read my
article for more background.

0 new messages