[RFC] Constructor patch

Janus Weil janus@gcc.gnu.org
Wed Oct 27 16:03:00 GMT 2010


Hi Tobias,

> 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.

Great!

Could you say a few words on the general idea of your implementation?
I remember a previous draft patch where you were dressing symbol names
with '@' etc. How do you handle the collisions of symbol names now?

Also, I remember that Damian was extremely keen to see this feature
implemented. I'm sure he has some test cases and might want to try out
the patch ...


> 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.

What's the order of magnitude of the failures?

Cheers,
Janus



> 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
>



More information about the Fortran mailing list