Re: TYPE overloading?

From: Arjen Markus (arjen.markus_at_wldelft.nl)
Date: 03/30/05


Date: Wed, 30 Mar 2005 13:20:39 +0200

Thomas Lang wrote:
>
> hi all,
>
> one and the same code can run with COMPLEX or REAL type - depending on
> what type of simulation I need. So far, one can switch between these two
> versions with the help of a sed-script which simply exchanges all
> declarations.
>
> What I want now, is to get rid of the sed-script and to simply change a
> type definition in a module to switch from one type to the other.
>
> Functions can be handled via interfaces (wrappers). In principle I guess
> I could change all definitions to a new type but I would have to adapt
> all assignments of variables too. Is there a way to avoid the '%' in
> type definitions, or to overload an intrinsic type?
>
> br,
> Tom

Not sure if this fits your requirements, but:

type AHA
   real :: xx
end type

type(AHA) :: a, b

b%xx = 1.0
a = b

is legal code: the assignment is automatically taken care of.

If you want:

b = 1.0

to work, define a subroutine that assigns a real to the component xx
of a type(AHA) variable:

interface assign(=)
   module procedure aha_is_real
end interface

subroutine aha_is_real{a,r)
   type(AHA) :: a
   real :: r

   a%xx = r
end subroutine

(Roughly, I was too lazy to check the standard for the exact syntax)
Regards,

Arjen



Relevant Pages