Actually no. There is hardly any good use case for dummy arguments of CLASS(*)
unlimited polymorphic type in Fortran. CLASS(*) is a pathway toward vulnerabilities in a library.
Instead one can argue this is a passable use case for the other type of unlimited polymorphic entity, TYPE(*)
i.e., assumed-type
dummy argument facility introduced in the language starting Fortran 2018: This is given the guardrails provided in the standard with assumed-type dummy arguments - there isn’t much a Fortranner inclined heavily toward domain expertise as opposed to sound software engineering can do with this type.
And also assumed-rank
facility can be employed here.
module m
contains
pure function conform(mold, var)
! Argument list
type(*), intent(in) :: mold(..) !<-- assumed-type, assumed-rank dummy
type(*), intent(in), optional :: var(..) !<-- -ditto-
! Function result
logical :: conform
conform = .true.
if ( present(var) ) then
conform = all( shape(mold) == shape(var) )
end if
end function conform
end module
use m, only : conform
blk1: block
integer :: foo, bar
print *, "Block: scalar integers"
print *, "conform(foo,bar)? ", conform(foo, bar), "; expected is T"
end block blk1
print *
blk2: block
character(len=1), dimension(2) :: foo, bar
print *, "Block: shape=[2] character variables"
print *, "conform(foo,bar)? ", conform(foo, bar), "; expected is T"
end block blk2
print *
blk3: block
real :: foo(1,2), bar(2,1)
print *, "Block: rank-2 real variables of different shape"
print *, "conform(foo,bar)? ", conform(foo, bar), "; expected is F"
end block blk3
print *
blk4: block
type :: t
end type t
type(t), dimension(2,3,4) :: foo, bar
print *, "Block: rank-3 objects of type(t) with same shape"
print *, "conform(foo,bar)? ", conform(foo, bar), "; expected is T"
end block blk4
end
See below with both Intel Fortran and gfortran:
C:\temp>ifort /standard-semantics /warn:all /stand:f18 p.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.2.0 Build 20210228_000000
Copyright (C) 1985-2021 Intel Corporation. All rights reserved.Microsoft (R) Incremental Linker Version 14.27.29112.0
Copyright (C) Microsoft Corporation. All rights reserved.-out:p.exe
-subsystem:console
p.objC:\temp>p.exe
Block: scalar integers
conform(foo,bar)? T ; expected is TBlock: shape=[2] character variables
conform(foo,bar)? T ; expected is TBlock: rank-2 real variables of different shape
conform(foo,bar)? F ; expected is FBlock: rank-3 objects of type(t) with same shape
conform(foo,bar)? T ; expected is TC:\temp>gfortran -Wall p.f90 -o gcc-p.exe
C:\temp>gcc-p.exe
Block: scalar integers
conform(foo,bar)? T ; expected is TBlock: shape=[2] character variables
conform(foo,bar)? T ; expected is TBlock: rank-2 real variables of different shape
conform(foo,bar)? F ; expected is FBlock: rank-3 objects of type(t) with same shape
conform(foo,bar)? T ; expected is TC:\temp>