For Programmers: Free Programming Magazines  


Home > Archive > Fortran > April 2005 > f95 OOP question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author f95 OOP question
Phony Account

2005-04-21, 3:59 am

Hello,

It's been a long (first) day of programming in f95, and I managed to
bring the intel compiler to its knees. (I will probably be sending a
report tomorrow to them). And I wonder if I am trying to do stuff that
the f95 is just not designed to do.

(I realize that modules in f95 are not objects, but I will use the
object term, meaning a module with a type definition, and associated
routines. I also suspect the answers are in my copy of f95/2003
explained, but I am still digesting it.)

I have two issues. Here is a first one:

I am trying to have an object create another one, and link it via a
pointer to a third one. It goes something like this (I am writing from
memory, so the syntax is not quite right), and I have not managed to get
it to compile yet (as I said, first day of f95 programming):

file: current_class:
module current_class
use Target_class
use other_class
type current_class
:
end type current_class

procedure link(self,oTarget)

type (current_class) :: self
type (Target_class) :: oTarget
type (other_class) :: oOther

call Create(oOther, additinal parameters)
Target%other=>oOther
end procedure link
end module current_class

file: target_class:
module target_class
use other_class
type target_class
type (other), pointer::other
end type target_class
:
end module target_class

I have a problem with the use of the "use" statement, because it mimics
inheritance, which is really not what I want (exposes way too much of
one object to the other). I want some kind of association between objects.

I have been playing with "use" with qualifiers, but I still don't quite
get what is being "use"-d (type-defs, procedures). Especially if I use
an interface statement to rename a procedure. How do I qualify that
with "use"?

I think that instead of use, I would like to use a C-like "include
foo.h" where foo.h has all the class and method declarations, presumably
using the f95 include statement. However I have not found examples of that.

Second issue:

Another thing that puzzles me (and exposes my ignorance) is that in the
example above, for all the classes, in each module I have a routine

subroutine create(self,...)
type (xxx_class)::self.

However, if one module uses another, and both have the "create" routine
with the same names, the compiler complains. I thought that because the
argument types to the different "create"-s are of different types (each
create has as its first argument the object itself), the compiler should
have been able to discriminate between the different "create"s.

If I rename the "create"-s to "create_a" in module a, "create_b" in
module b, etc, and in each I have an interface statement to allow
"create_a" to be called by just "create", and same for "create_b", etc,
the compiler does not complain.

So I can deal with this problem, but it does introduce more typing, and
more opportynity for error.

Does anyone actually understand what I'm doing ?:-) If not, just keep
quiet, and I will re-write this post in a more careful manner.

Thanks,

Mirko
E. Robert Tisdale

2005-04-21, 3:59 am

Phony Account wrote:

> It's been a long (first) day of programming in f95
> and I managed to bring the intel compiler to its knees.
> (I will probably be sending a report tomorrow to them).


You should probably wait
until you understand Fortran 95 a little better.

> And I wonder if I am trying to do stuff
> that the f95 is just not designed to do.


Everybody does that.

> (I realize that modules in f95 are not objects
> but I will use the object term meaning a module
> with a type definition, and associated routines.


You are . An object is an instance of some type --
a Fortran 95 derived type in this case.
The term module means the same thing in Fortran 95
that it does in every other computer programming language.
It is a separately compilable piece of code
that you can link into your program later.
Modules may contain zero, one or more user type definitions
(derived types in Fortran 95, classes in Java or C++ or
structs in C or C++). An example of a module in C
is the math library archive libm.a
together with it's interface the math.h header file.

> I also suspect [that]
> the answers are in my copy of f95/2003 explained


Probably.

> but I am still digesting it.)
>
> I have two issues. Here is a first one:
>
> I am trying to have an object create another one
> and link it via a pointer to a third one.
> It goes something like this
> (I am writing from memory, so the syntax is not quite right)


It is better to include code that actually compiles
or that you have, at least, attempted to compile.

> and I have not managed to get it to compile yet
> (as I said, first day of f95 programming):


> cat module.f95

! file: other_class.f95
module other_module
type other_class
integer:: i
end type other_class
contains
function otherCreate(i) result(o)
integer, intent(in):: i
type (other_class):: o
o%i = i
end function otherCreate
subroutine otherDestroy(o)
type (other_class), intent(in):: o
end subroutine otherDestroy
end module other_module

! file: target_class.f95
module target_module
use other_module
type target_class
type (other_class), pointer:: other
end type target_class
contains
function targetCreate(o) result(t)
type (other_class), intent(in), target:: o
type (target_class):: t
t%other => o
end function targetCreate
subroutine targetDestroy(t)
type (target_class), intent(in):: t
end subroutine targetDestroy
end module target_module

! file: current_class.f95
module current_module
use target_module
type current_class
integer:: i
end type current_class

contains
subroutine link(self, oTarget)
type (current_class) :: self
type (Target_class) :: oTarget
type (other_class) :: oOther

oOther = otherCreate(13)
oTarget = targetCreate(oOther)
targetDestroy(oTarget)
otherDestroy(oOther)
end subroutine link
end module current_module

> f95 -c module.f95
> ls *.mod

CURRENT_MODULE.mod OTHER_MODULE.mod TARGET_MODULE.mod

> I have a problem with the use of the "use" statement
> because it mimics inheritance


It does *not*.

> which is really not what I want
> (exposes way too much of one object to the other).
> I want some kind of association between objects.
>
> I have been playing with "use" with qualifiers
> but I still don't quite get what is being "use"-d (type-defs, procedures).


The use statement causes the compiler to search for and read the
corresponding module interface (the *.mod file).
You would still need to link in
the corresponding object file (module.o in this case)
which actually implements the interface.

> Especially if I use an interface statement to rename a procedure.
> How do I qualify that with "use"?
>
> I think that instead of use,
> I would like to use a C-like "include foo.h"
> where foo.h has all the class and method declarations,
> presumably using the f95 include statement.


No! Using a header file as module interface is a *kludge*
that C programmers use to compensate for the lack of true
module support in the C programming language.
The Fortran 95 module interface in *.mod files
is a superior and much safer design.

> However, I have not found examples of that.
>
> Second issue:
>
> Another thing that puzzles me (and exposes my ignorance) is that
> in the example above, for all the classes, in each module
> I have a routine
>
> subroutine create(self,...)
> type (xxx_class)::self
>
> However, if one module uses another
> and both have the "create" routine with the same names,
> the compiler complains. I thought that
> because the argument types to the different "create"-s
> are of different types
> (each create has as its first argument the object itself),
> the compiler should have been able to discriminate
> between the different "create"-s.


It can. But, unlike C++, Fortran 95 compilers don't automatically
mangle the subroutine name to produce a symbol
that the link editor can recognize.
The Fortran 95 programmer must do this explicitly --
look up *overloading* in your Fortran 95/2003 explained.

> If I rename the "create"-s to
> "create_a" in module a, "create_b" in module b, etc.
> and in each I have an interface statement to allow
> "create_a" to be called by just "create"
> and same for "create_b", etc,
> the compiler does not complain.


Don't use subroutines.
Use functions as [pseudo] constructors as I have shown above.
And define and call destructors even if they don't do anything.
You will be glad that you did later when you decide to change
the representation of an object to include a pointer
to dynamically allocated memory.

> So I can deal with this problem
> but it does introduce more typing and more opportunity for error.
>
> Does anyone actually understand what I'm doing ?:-)
> If not, just keep quiet
> and I will re-write this post in a more careful manner.


There are lots of object oriented Fortran 90/95 programmers now.
We can only *guess* at what you are attempting to do.
Fortran 95 does *not* support inheritance or run-time polymorphism.
Do you actually have access to a Fortran 03 compiler?
Gerald F. Thomas

2005-04-21, 3:59 am


"Phony Account" <phaccount@nycap.rr.com> wrote in message
news:tdE9e.4122$Bc7.2244@twister.nyroc.rr.com...

Self professed phony, get a life, a self, a hold, a help.
--
You're Welcome,
Gerry T.
______
"God is not willing to do everything and thereby take away our free will
and that share of glory that rightfully belongs to us." -- Machiavelli.


Walter Spector

2005-04-21, 3:59 pm

"E. Robert Tisdale" wrote:
> Phony Account wrote:
> [lots of good stuff deleted]
>
> It does *not*.
>

One of the major advantages of Fortran-90 modules over simple
'include' files is that you can explicitly specify which items
the USEr needs. To do this, use the ONLY clause:

USE my_module, ONLY: this, that, theother
[color=darkred]

The *definitions* of anything that the module has within it.
As Mr Tisdale states:
[color=darkred]
> The use statement causes the compiler to search for and read the
> corresponding module interface (the *.mod file).
> You would still need to link in
> the corresponding object file (module.o in this case)
> which actually implements the interface.
>

The INTERFACE capability is used, for one thing, to create a 'generic'
name for several related procedures. Simple 'renaming' is probably
better done with the USE statement. But of course this all depends on
what you are trying to accomplish.
[color=darkred]
>
> No! Using a header file as module interface is a *kludge*
> that C programmers use to compensate for the lack of true
> module support in the C programming language.
> The Fortran 95 module interface in *.mod files
> is a superior and much safer design.


Agreed. There are only rare cases where INCLUDE is really needed
in Fortran-95. One example is replicating the body of a procedure
when attempting to write 'template-like' code.

> There are lots of object oriented Fortran 90/95 programmers now.
> We can only *guess* at what you are attempting to do.
> Fortran 95 does *not* support inheritance or run-time polymorphism.


Sometimes there are ways to accomplish the job...

In any event, in addition to Metcalfs book (I've not seen the latest
edition, but do own an earlier one), the OP may also wish to consider
Ed Akins book "Object-Oriented Programming in Fortran 90/95". Akin
discusses a lot of this sort of thing. Also check out the papers at
the following web site:

http://www.cs.rpi.edu/%7Eszymansk/oof90.html

Hope this helps.

Walt
-...-
Walt Spector
(w6ws at earthlink dot net)
Phony Account

2005-04-22, 8:57 am

Phony Account wrote:
> Hello,
>
> It's been a long (first) day of programming in f95, and I managed to
> bring the intel compiler to its knees. (I will probably be sending a
> report tomorrow to them). And I wonder if I am trying to do stuff that
> the f95 is just not designed to do.


much irrelevant stuff deleted.

I want to thank everyone that read and responded.

First, I'd like to apologise for the pseudo-code that I wrote in the
original post. It was not fair to expect people to try to understand
what I was getting to.

Second, I deleted all my "include" commands, and stuck to "use".

Third, I do have the book on oo fortran programming, as well as the
articles from the web. While I found the articles illuminating, the
book was a bit of a dissapointment (to me), even though I hate to put
down the large amount of work put in by the author.

Fourth, I am still a bit about why I have to re-define some of
the interfaces for object methods, because
....
but here I was goig to put some more pseudo-code, and decided against
it. I will therefore post a more coherent message later on :-)

Thanks again,

Mirko

PS The Phony Account is my attempt of directing un-solicited email away
from my regular in-box.
Clive Page

2005-04-22, 8:57 pm

In message <tdE9e.4122$Bc7.2244@twister.nyroc.rr.com>, Phony Account
<phaccount@nycap.rr.com> writes
>I am trying to have an object create another one, and link it via a
>pointer to a third one. It goes something like this (I am writing from
>memory, so the syntax is not quite right), and I have not managed to
>get it to compile yet (as I said, first day of f95 programming):


There is a lot of useful information, indeed the draft of a whole book
on object-oriented Fortran95, at http://www.owlnet.rice.edu/~mech517/

Look under OOP via F90 in the links on the left...

--
Clive Page
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com