Re: compilation problem with module function interface definition
- From: Phony Account <phaccount@xxxxxxxxxxxx>
- Date: Thu, 28 Apr 2005 00:19:46 GMT
Richard E Maine wrote:
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)
As a Donald nicely exposed, the other cardinal rule that I broke (I have the f95/03 explained book) is lack of reading and comprehension skills. I re-read Section 5.18 with care, and sure enough it all makes sense now.
Moving on to Richard E Main's note, I need to come out clean and expose the ``dirty laundry'' regarding the reason as to why I wanted to re-define the function interface.
... Richard's note deleted.
My experience in OOP comes from the IDL programming language (see www.rsinc.com) where objects are brought into existence thus:
oFoo=create_obj("object name", argument list ...)I was trying to mimick this behavior by
- declaring an object type:
> type (class) obj
- and bringing it into existence:
> obj=create_obj(argument list)where in the class definition (module) I would have (a full example is included below):
interface create_obj module procedure create_obj_foo end interface
contains
function create_obj_foo(args) result (obj) type (class) obj
end function end module
This works if I have only one class that I am trying instantiate, but not if I have more than one and the instantiation procedure has arguments of identical type. Here is a listing of two modules and of a main program.
MODULE O1
type O1_class
integer foo
end type O1_class interface create_obj
module procedure create_obj1
end interfaceCONTAINS
FUNCTION create_obj1(param) RESULT(obj)
![function_header_comments]
type (O1_class) obj
integer,intent(in)::paramobj%foo=param
END FUNCTION create_obj1
END MODULE O1
MODULE O2
![module_header_comments]
type O2_class
real fee
end type O2_class interface create_obj
module procedure create_obj2
end interfaceCONTAINS
FUNCTION create_obj2(param) RESULT(obj) ![function_header_comments]
type (O2_class) obj
integer,intent(in)::paramobj%fee=param
RETURN END FUNCTION create_obj2
END MODULE O2
PROGRAM O12_test
![main_header_comments] use O1 use O2
type (O1_class) o1_obj type (O2_class) o2_obj
o1_obj=create_obj(5) o2_obj=create_obj(10)
print *,o1_obj%foo,o2_obj%fee
END PROGRAM O12_test
The compilation of O12_test fails because the arguments to the two "new_obj" are identical. I was hoping that the compiler will be able to distinguish between the two functions based on the result type. But I guess not. It looks only at the arguments types, which in this case are identical.
This failure is not a show-stopper. Instead of functions, I can use appropriately defined subroutines and their interfaces:
call create_obj(o1_obj,5)
But I wonder as to the reason why the compiler does not include the function type when dealing resolving polymorphisms? Maybe because the result is not allways assigned to a typed variable. This could be dealt with with a more obscure invocation of the function which would also specify the desired result type. But that is a completely different thread.
Thanks,
Mirko .
- Follow-Ups:
- Re: compilation problem with module function interface definition
- From: Richard E Maine
- Re: compilation problem with module function interface definition
- References:
- compilation problem with module function interface definition
- From: Phony Account
- Re: compilation problem with module function interface definition
- From: Richard E Maine
- compilation problem with module function interface definition
- Prev by Date: Re: strange linking problem
- Next by Date: Re: why doesn't this compile ?
- 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
|