Можно ли расширить класс и перегрузить одну из его процедур процедурой с другим интерфейсом?
Я хотел бы создать несколько типов (скажем, tensor1
, tensor2
, ...) расширение базового класса tensor
, все реализующие метод sub
, аргументы которого зависят от подкласса.
Рассмотрим следующий код:
module tensor_class
implicit none
private
public :: tensor_Type
type, abstract :: tensor_Type
Contains
procedure,pass(self) :: sub
end type
Contains
subroutine sub(self,other)
class(tensor_Type),intent(inout) :: self
integer :: other
write(*,*) 'sub is not implemented in the main class'
end subroutine
end module tensor_class
module tensor1_mod
use tensor_class
type, extends(tensor_type) :: tensor1
real,dimension(2) :: val
Contains
procedure, pass(self) :: sub
end type tensor1
Contains
subroutine sub(self,other)
class(tensor1),intent(inout) :: self
real,dimension(2),intent(in) :: other
self%val = self%val + 2.0 * other
end subroutine
end module
module tensor
use tensor1_mod
end module
program testTensor
use tensor
implicit none
class(tensor_type),allocatable :: t
type(tensor1) :: t1
real,dimension(2) :: v1 = [1.0,-2.3]
integer :: i
t1 = tensor1(v1)
t = tensor1(v1)
call t1%sub(i)
call t%sub(i)
end program
Ни Intel Fortran (19.1.1.216 20200306 ), ни gfortran (9.3.0) не скомпилирует его. Компилятор Intel выводит следующее сообщение об ошибке
tensorMin.F90(25): error #8383: The dummy arguments of an overriding and overridden binding that correspond by position must have the same characteristics, except for the type of the passed object dummy arguments. [SUB]
и gfortran
>tensorMin.F90:25:15:
25 | procedure, pass(self) :: sub
| 1
Error: Argument mismatch for the overriding procedure 'sub' at (1): Type mismatch in argument 'other' (REAL(4)/INTEGER(4))
Я не могу объявить sub
как deferred
в tensor_class
, если я хочу иметь возможность создать несколько подклассов и сделать так, чтобы sub
принимал разные типы аргументов в каждом классе (например, real, dimension(2)
в tensor1
, real, dimension(2,2)
в tensor2
и c).
При чтении Фортрана 2008 стандартный раздел 4.5.7.3, кажется, что имя и номер аргументов в sub
должны совпадать, но стандарт, похоже, ничего не говорит о введите аргументов.
Я что-то упустил? Есть ли другой способ, кроме создания класса, содержащего все возможные аргументы sub
?