[RFC] Constructor patch

Tobias Burnus burnus@net-b.de
Wed Oct 27 08:01:00 GMT 2010


Attached you find a draft of a patch allowing "constructors" as one 
knows them from object-oriented programming languages. In Fortran, it 
just means that a generic name can be the same as the name of a derived 
type. And if the interface matches, the specific function belonging to 
that generic name is called instead of the structure constructor.

The patch below still has some rough edges; for instance, it still has 
some test-suite failures and needs some clean up. However, I though that 
I post it to allow for comments.

Example:

$ gfortran test.f90 && ./a.out
            9
           42

$ cat test.f90
module m
   interface cons
     procedure cons42
   end interface cons
contains
   integer function cons42()
     cons42 = 42
   end function cons42
end module m

module m2
   type cons
     integer :: j = -1
   end type cons
   interface cons
     procedure consT
   end interface cons
contains
   type(cons) function consT(k)
     integer :: k
     consT%j = k**2
   end function consT
end module m2

use m
use m2, only: cons
implicit none
type(cons) :: x
integer :: k
x = cons(3)
k = cons()
print *, x%j
print *, k
end


Tobias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: constructor-draft.diff
Type: text/x-patch
Size: 27742 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20101027/1ea33625/attachment.bin>


More information about the Fortran mailing list