ATK
ATK

Reputation: 1526

Overloading with different interface procedures in Fortran

Say you have two subroutines which have different interfaces and you have two types where each corresponds to one of the procedures.

type, abstract :: base
 contains 
 procedure :: pointer_to_routine
 end type base 

 type, extends(base) :: first_case
 contains 
 procedure :: pointer_to_routine => first_case_routine
 end type first_case 

 type, extends(base) :: second_case
 contains 
 procedure :: pointer_to_routine => first_sec_routine
 end type second_case 

So this is not valid Fortran code, but it is kind of the idea I want to do. If the routines had similar interfaces I could define an abstract interface and deferred attribute in the base declared type. But since my two routines have different interfaces, I am not sure how this can work out.

Essentially, one routine needs more inputs than another, so one solution would be to add the remaining inputs as just dummy inputs, although, this could potentially cause a bit confusion, And I am wondering if there is a more convenient solution.

Upvotes: 2

Views: 320

Answers (1)

All solutions you can invent for this will be workarounds. The language is simply designed that way that all procedures with the same binding name as in the parent type should have the same interface. Note that other languages have similar problems/features Override method with different signature.

You can use dummy arguments if you know they will be needed in general but not in a specific case. They can be optional arguments. You can also make the input arguments contained in a polymorphic derived type.That will bring new issues though.

Upvotes: 3

Related Questions