Hi
f90 the compiler does seem to distinguish between methods from user
defined types. I have two type one "has a" instance of the other. Both
have a methods named Print (with different typed arguments). I want
both user defined type to have the same method Print and be callable
by each other. How can I do what I want?
module BoxModule
type Box
....
end type
contains
subroutine Print(b)
type(Box) b
....
end subroutine
end module
module OtherModule
type Other
....
type(Box) b
....
end type
contains
subroutine Print(o) ! <----------------------------- compiler doesn't
like this
type(Other) o
....
Print(o%b)
....
end subroutine
end module
subroutine Print(c)
1
use BoxModule
2
Error: Procedure 'print' at (1) is already defined at (2)
|
|
0
|
|
|
|
Reply
|
bampingting (1)
|
3/17/2010 11:40:40 PM |
|
Bam Ting <bampingting@gmail.com> wrote:
> f90 the compiler does seem to distinguish between methods from user
> defined types. I have two type one "has a" instance of the other. Both
> have a methods named Print (with different typed arguments). I want
> both user defined type to have the same method Print and be callable
> by each other. How can I do what I want?
Your terminology makes the question confusing. Fortran doesn't have
"methods". It does have procedures. While procedures can bear some
relationship with what are sometimes called methods, they aren't
identical and you'll likely cause great confusion, both to yourself and
others, if you refer to procedures as methods. The type-bound procedures
introduced in f2003 are more like "methods", but that's not an f90
feature.
And talking about "methods from user defined types" just makes the
confusion worse. You can't define a procedure in a type - not in f90.
You can, as your code does, define a type and a procedure that operates
on that type in the same module. But the procedure is not in any sense
"from" the type. It is from the module and happens to have an argument
of the type.
> module BoxModule
> type Box
> ...
> end type
> contains
> subroutine Print(b)
> type(Box) b
> ...
> end subroutine
> end module
>
> module OtherModule
> type Other
> ...
> type(Box) b
> ...
> end type
> contains
> subroutine Print(o) ! <----------------------------- compiler doesn't
> like this
> type(Other) o
> ...
> Print(o%b)
> ...
> end subroutine
> end module
The closest thing in f90 to what I think you are asking for is a generic
procedure. You give the specific procedures all different names (perhaps
something like print_box and print_other), but then put them all in a
generic named print. Multiple specific procedures can share the same
generic name - that's what generics are about. You cannot, however, have
two specific procedures of the same name in the same scope. The compiler
wouldn't know which one you are talking about; if you expect it to tell
based on the argument types, well, that's what generic procedures do.
That would address the problem you illustrate above, but your
description hints at another problem when you talk about having them
call "each other". Module USE statements cannot be recursive. That is,
if module OTHER used module BOX, then module BOX cannot also use module
OTHER. Your code doesn't illustrate that problem, but your description
alludes to it. If indeed you do need them to be able to call each other,
that can be tricky in f90. The solutions tend to be a bit awkward and
involve some restructuring.
--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
|
|
0
|
|
|
|
Reply
|
nospam
|
3/18/2010 3:40:34 AM
|
|
On Mar 17, 8:40=A0pm, nos...@see.signature (Richard Maine) wrote:
> Bam Ting <bampingt...@gmail.com> wrote:
> > f90 the compiler does seem to distinguish between methods from user
> > defined types. I have two type one "has a" instance of the other. Both
> > have a methods named Print (with different typed arguments). I want
> > both user defined type to have the same method Print and be callable
> > by each other. How can I do what I want?
>
> Your terminology makes the question confusing. Fortran doesn't have
> "methods". It does have procedures. While procedures can bear some
> relationship with what are sometimes called methods, they aren't
> identical and you'll likely cause great confusion, both to yourself and
> others, if you refer to procedures as methods. The type-bound procedures
> introduced in f2003 are more like "methods", but that's not an f90
> feature.
>
> And talking about "methods from user defined types" just makes the
> confusion worse. You can't define a procedure in a type - not in f90.
> You can, as your code does, define a type and a procedure that operates
> on that type in the same module. But the procedure is not in any sense
> "from" the type. It is from the module and happens to have an argument
> of the type.
>
>
>
> > module BoxModule
> > type Box
> > ...
> > end type
> > contains
> > subroutine Print(b)
> > type(Box) b
> > ...
> > end subroutine
> > end module
>
> > module OtherModule
> > type Other
> > ...
> > type(Box) b
> > ...
> > end type
> > contains
> > subroutine Print(o) ! <----------------------------- compiler doesn't
> > like this
> > type(Other) o
> > ...
> > Print(o%b)
> > ...
> > end subroutine
> > end module
>
> The closest thing in f90 to what I think you are asking for is a generic
> procedure. You give the specific procedures all different names (perhaps
> something like print_box and print_other), but then put them all in a
> generic named print. Multiple specific procedures can share the same
> generic name - that's what generics are about. You cannot, however, have
> two specific procedures of the same name in the same scope. The compiler
> wouldn't know which one you are talking about; if you expect it to tell
> based on the argument types, well, that's what generic procedures do.
>
> That would address the problem you illustrate above, but your
> description hints at another problem when you talk about having them
> call "each other". Module USE statements cannot be recursive. That is,
> if module OTHER used module BOX, then module BOX cannot also use module
> OTHER. Your code doesn't illustrate that problem, but your description
> alludes to it. If indeed you do need them to be able to call each other,
> that can be tricky in f90. The solutions tend to be a bit awkward and
> involve some restructuring.
>
> --
> Richard Maine =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| Good judgment come=
s from experience;
> email: last name at domain . net | experience comes from bad judgment.
> domain: summertriangle =A0 =A0 =A0 =A0 =A0 | =A0-- Mark Twain
Thanks for the reply!
It's not recursive sorry for the confusing language. I have one type
that has as its members instances of another type. I want a
specialized subroutine Print associated with both types that knows how
to print all of its members. In the type that contains I need to call
Print on the contained instances during Print. I hope that's clearer.
I'm not very good with fortran so I don't know all of the terminology.
I thought the compiler should be able to differentiate between
functions with different typed arguments.
I guess I'm after a generic procedure. But I had hoped to keep the
user defined types implementations separate and self contained where
possible, eg. Box doesn't need to know anything about Other.
Thanks again
|
|
0
|
|
|
|
Reply
|
Bam
|
3/18/2010 6:02:19 AM
|
|
On Mar 18, 12:02=A0am, Bam Ting <bampingt...@gmail.com> wrote:
> I guess I'm after a generic procedure. But I had hoped to keep the
> user defined types implementations separate and self contained where
> possible, eg. Box doesn't need to know anything about Other.
What about this:
module BoxModule
implicit none
private
type, public :: Box
....
end type
interface Print
module procedure Print
end interface
public Print
contains
subroutine Print(b)
type(Box), intent(IN) :: b
....
end subroutine
end module
module OtherModule
implicit none
use BoxModule
private
type, public :: Other
....
type(Box) :: b
....
end type
interface Print
module procedure PrintOther
end interface
public Print
contains
subroutine PrintOther(o)
type(Other), intent(IN) :: o
....
call Print(o%b)
....
end subroutine
end module
program main
use OtherModule
use BoxModule
implicit none
type(Box) :: a
type(Other) :: b
....
call Print(a)
call Print(b)
....
end program
|
|
0
|
|
|
|
Reply
|
jwm
|
3/18/2010 6:58:07 AM
|
|
Bam Ting <bampingting@gmail.com> wrote:
> I guess I'm after a generic procedure. But I had hoped to keep the
> user defined types implementations separate and self contained where
> possible, eg. Box doesn't need to know anything about Other.
They don't have to know about each other to make a generic. Each
separately gives the generic name to its own specific.
--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
|
|
0
|
|
|
|
Reply
|
nospam
|
3/18/2010 8:03:38 AM
|
|
|
4 Replies
233 Views
(page loaded in 0.082 seconds)
Similiar Articles: Agena - a new procedural programming language - comp.unix.solaris ...... any of the above would let you avoid mixed-language programming. ... waning to pass and array of values to a new GUI ... Basic support OO technicques Fortran is procedural. ... Preferred way to get multiple returns from a function - comp.lang ...FWIW, I'm dealing with the same issue in Fortran. Ever since it went OO with the Fortran2003 standard, I'm writing all new code with an OO bent. Where did Fortran go? - comp.lang.fortranFortran failed to grow during the critical 1980s and was eclipsed by C++. ... Ok, let debate it then :) Ok, Matlab has added OO about 2 years ago? 2008 I ... OOP performance issues - comp.soft-sys.matlabI do have an object oriented framework where inheritance and polymorphism is done ... for ... Down-cast a polymorphic pointer? - comp.lang.fortran I'm delving into Fortran2003 ... My Font Manager Wishlist - comp.fontsWhere did Fortran go? - comp.lang.fortran (That said, on top of my personal F2003 implementation wishlist would be allocate-on-assignment and ... use OO matlab seem to ... Copying rows in a two dimensional array. - comp.lang.ada ...> Fortran 90 (and later) has rectangular but optionally non-unit-stride slices; X(1:5:4 ... In contrast F03, with adds features mostly in the areas of OO and 'parameterized ... Singleton base class - comp.lang.c++.moderatedWhere did Fortran go? - comp.lang.fortran I still use the Singleton FFT (using numbers containing ... The few who use OO matlab seem to just use the class ... Use of MATLAB fftshift - comp.dspIn Fortran for example, one can do that, and it can make implementing DSP ... wanted to write a classdef-based version of this object then the > Object-Oriented Programming ... not implemented: namespace - comp.sys.sun.adminWhere did Fortran go? - comp.lang.fortran If you consider mere conformance (where a lot of features need not be implemented, or ... The few who use OO matlab seem to just ... error "too few operands in path" - comp.text.pdfUnfortunately (and IMHO carelessly), OO does not appear to have a commandline ... trouble with Windows gfortran - comp.lang.fortran... the wrong linker is picked up from ... Object Oriented Programming via Fortran 90Copyright © 1999, 2000 J. E. Akin. All rights reserved. Page 1 of 23 Object Oriented Programming via Fortran 90 (Preprint: Engineering Computations, v. 16, n. 1, pp ... Object Oriented Fortran 90 Programming - Computer Science at RPIThis page summarizes much of our current work in object-oriented programming using Fortran 90 on scalar workstations and distributed-memory supercomputers. 7/24/2012 12:43:57 AM
|