I'm trying to make a custom data-type constructor by overloading the type name. However, when making the call, the default constructor gets called instead. I cannot understand what I am doing wrong.
Here's a code snippet that has the problem.
module test
type, public :: foo
character(512) :: str
logical :: flag1
logical :: flag2
end type
! overload foo data-type
interface foo
module procedure make_foo
end interface
contains
! custom constructor
function make_foo(str, flag1) result(self)
implicit none
type(foo) :: self
character(512), intent(in) :: str
logical, intent(in), optional :: flag1
self % str = str ! this needs to be passed
self % flag1 = .false. ! this needs to be optional, and is false by default
if (present(flag1)) self % flag1 = flag1
self % flag2 = .false. ! this cannot be passed and is always false by default
end function
end module
program tmp
use test
implicit none
type(foo) :: a
a = foo("hello") ! error here
end program
I want to have a custom constructor that requires str to be passed, that allows for the optional specification of flag1 and that deals with flag2 always by itself.
When testing the data-type using its constructor, it uses the default constructor and it complains for missing components.
No initializer for component 'flag1' given in the structure constructor at (1)
I am using gfortran 10.2.0