Quantcast
Channel: Intel® Software - Intel® Visual Fortran Compiler for Windows*
Viewing all articles
Browse latest Browse all 5691

Constructor for extended types

$
0
0

Hi, I'm not sure if this is a bug or a feature of 16-upd3, but I have a problem applying a constructor for extended types as demonstrated by this code:

module foo_def
    type, abstract :: foo
        real x
    end type foo
end module foo_def

module foo2_def
    use foo_def
    type, extends(foo) :: foo2
        real y
    end type foo2
    interface foo
        module procedure constructor
    end interface
    contains
    function constructor(x, y) result(self)
        real, intent(in)  :: x, y
        type(foo2) :: self
        self % x = 2.3
        self % y = 1.1
    end function constructor
end module foo2_def

program test
use foo_def
use foo2_def
class(foo), allocatable :: f
allocate(foo2::f)
f = foo(x, y)
print*, f%x, f%y
end program test

I get the error codes:

1>F:\test.f90(29): error #6197: An assignment of different structure types is invalid.
1>F:\test.f90(29): error #8304: In an intrinsic assignment statement, variable shall not be polymorphic.   [F]
1>F:\test.f90(30): error #6460: This is not a field name that is defined in the encompassing structure.   [Y]

Based on above error messages I guess the compiler first deallocates f in line 29. Hence it is not of type foo2 anymore which leads to type mismatch regarding the foo2 result from the constructor. Is this a bug or a feature? Are assignments prohibited for allocated polymorphic objects? If I statically declare f to be of type(foo2) the code works as expected.

I know that constructors are not a part of Fortran but the trick of introducing a generic interface with the same name as the type name seems to be a standard trick working for all cases I have encountered except for allocated polymorphic variables. Any good work arounds?

Best regards,

Jens B. Helmers

 

 

 


Viewing all articles
Browse latest Browse all 5691

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>