Re: a history question

From: Arjen Markus (arjen.markus_at_wldelft.nl)
Date: 09/29/04


Date: Wed, 29 Sep 2004 15:53:24 +0200

Rich Townsend wrote:
>
> Arjen Markus wrote:
> > Richard Edgar wrote:
> >
> >>Arjen Markus wrote:
> >>
> >>
> >>>Well, in my library I keep track of the proper types myself (it is
> >>>done as a string indicating the data type), but the thing is that the
> >>>compiler still has access to the interfaces of all routines (they are
> >>>contained in a module) and I can still store different types of data
> >>>with one version of my source code.
> >>
> >>So it's a runtime check to stop (say) REALs being added to a tree which
> >>is supposed to be holding INTEGERs?
> >>
> >>Richard
> >
> >
> > If you want a tree that holds only integers, yes, but you can use it to
> > store reals and integers at the same time ...
> >
> > Regards,
> >
> > Arjen
>
> Well, what happens when I try to read a REAL from the tree, and actually
> end up with an INTEGER? More specifically, do you implement reads as
> functions or subroutines? With functions, you need an extra argument as
> a 'marker type', to indicate the type of value you are expecting; you
> can't use the return type of the function as this marker, since the it
> is not part of the routine's signature.
>
> With subroutines, you can get around this problem; but you are still
> left with the fundamental difficulty of mismatch between the type of
> value you are expecting, and the type of value actually returned. How is
> this handled?
>

Oh well, there is nothing magical involved:
- I use subroutines like:

     subroutine tree_get_data_int( treenode, data, error )
     type(TREE_DATA), pointer :: treenode
     integer, intent(out) :: data
     logical, intent(out) :: error
     !
     ! Check that the type string is "INTEGER", otherwise set
     ! the error argument to true
     !
     end subroutine

(leaving out all the horrible details)

- Using subroutines also avoids another problem:
 
  iarray => tree_get_data( .... ) ! Get a pointer to allocated memory

  It would be easy to forget the => and use = instead. With subroutines
  you can control it.

Regards,

Arjen