Print

Print


Hi Stefan,

The simplest approach is to block out the name "error" in the USE 
statement in advanced_mod:

     use simple_mod, block_name=>error

That will start a new generic named "error" in advanced_mod, rather than 
extend the one in simple_mod (which is now named "block_name" in the 
scope of advanced_mod).

Cheers,
Bill


On 10/21/12 11:29 AM, Stefan Mauerberger wrote:
> Hi There!
>
> I ran again into a problem which I can not resolve. The story is that I
> wrote a module which contains - amongst other stuff - a derived-type and
> a very basic function for handling errors. I would call it
> self-contained.
>
> module simple_mod
>
>      type :: simple_cls
>          character :: c
>      contains
>          procedure :: hello
>      end type
>
>      interface error
>          procedure simple_error
>      end interface
>
> contains
>
>      subroutine hello(self)
>          class(simple_cls)::self
>          call error( self%c )
>      end subroutine hello
>
>      subroutine simple_error( msg )
>          character(*):: msg
>          write(*,*) 'simple: ', msg
>      end subroutine simple_error
>
> end module simple_mod
>
> Up to now this is perfectly fine. The thing I like about the
> self-contained approach is that this module does not have any
> dependencies and can be used 'stand alone'.
>
> Someone might want to extend its type which is also fine. However, I am
> not satisfied with its deficient error handling function. My main
> program has its own, more comprehensive error handler. So, the idea is
> to 'simply' overload it, but, I don't know weather this is possible nor
> how.
>
> My first attempt was to override the above generic interface:
>
> module advanced_mod
>      use simple_mod
>
>      type, extends(simple_cls) :: advanced_cls
>      end type
>
>      interface error
>          procedure advanced_error
>      end interface
>
> contains
>
>      subroutine advanced_error( msg )
>          character(*):: msg
>          integer :: i
>          write(*,*) 'advanced: ', msg
>      end subroutine advanced_error
>
> end module advanced_mod
>
> Unfortunately, this does not work, at all. Instead of overloading, it
> wants to extend the interface which is not possible due to ambiguities.
>
> I read a lot about interfaces, external and deferred procedures but
> can't come up with a solution.
>
> Is there a way, to put my ideas of overloading the error procedure into
> practice? (This is also a question of good coding style and best
> practice; my intention is to produce well written programs)
>
> Thank you in advance and all the best!
>
> Stefan
>

-- 
Bill Long                                           [log in to unmask]
Fortran Technical Support    &                 voice: 651-605-9024
Bioinformatics Software Development            fax:   651-605-9142
Cray Inc./Cray Plaza, Suite 210/380 Jackson St./St. Paul, MN 55101