Working with derived types

Arjen Markus arjen.markus895@gmail.com
Sun May 31 10:34:00 GMT 2015


Hi Christopher,

this is more a question for comp.lang.fortran, as it concerns the
Fortran language in general, not the gfortran compiler.

However, I would like to point out that there are at least two possibilities:
- Use parametrised derived types - this is a feature introduced in F2003
- Use the renaming feature of the use statement and the include statement.

The first:
type interval(real_kind)
    integer, kind :: real_kind
    real(real_kind) :: inf, sup
end type

This is not possible in gfortran yet, as far as I know. (I tried 4.9.2
and 5.1.0 under Cygwin)

The second possibility is less elegant, but it works from F90 onwards:

File precision.f90:
module precision
     integer, parameter :: sp = kind(1.0)
     integer, parameter :: dp = kind(1.0d0)
end module precision

File interval_impl.inc:
contains
       type interval
            real(wp) :: inf, sup
       end interval
       ... interface definitions
contains
 ... all routines

File intervals.f90:
module intervals_real32
       use precision, only :: wp => sp

       include 'interval_impl.inc'
end module_intervals_real32
module intervals_real32
       use precision, only :: wp => sp

       include 'interval_impl.inc'
end module_intervals_real32
module intervals
      use intervals_real32
      use intervals_real64
end module intervals

Note: be careful to provide interfaces for all routines that you want
to be accessible outside the module and make the actual routines
private.
That way you can use the interface names and avoid conflicts.

Regards,

Arjen

2015-05-29 18:52 GMT+02:00 Christopher Dimech <dimech.christopher@gmail.com>:
> Good evening,
>
> I want to implement an interval derived type. It will have two member
> objects, inf and sup.
>
> One can easily implement the thing in this way.
>
> Type (Interval)
>   Real :: inf
>   Real :: sep
> End Type
>
> However I want to allow having intervals of different
> types, with Real (Real64) :: inf ...
>
> One can think op perhaps having Interval_Real32, Interval_Real64 ...
>
> Would there be a neat way for how this functionality can be accomplished?
>
>
>
> --
> Christopher Dimech



More information about the Fortran mailing list