Re: Are generic linked lists possible in Fortran95?



Paul Van Delst wrote:
Hi there,

This question has, I'm sure, been asked before (and answered quite recently by, I think, Arjen Markus?) but a google search of clf didn't find anything useful (or recent). So here goes....

I converted a linked list module I put together for a specific node data structure definition to use a generic node data name. The derived type definitions are in a module, LinkedList.f90:

! -- Node definition
TYPE, PRIVATE :: LinkedList_Node_type
TYPE( NodeData_type ) :: NodeData ! Node data
TYPE( LinkedList_Node_type ), POINTER :: Previous => NULL() ! Pointer to previous node
TYPE( LinkedList_Node_type ), POINTER :: Next => NULL() ! Pointer to next node
END TYPE LinkedList_Node_type


! -- Linked list definition
TYPE, PUBLIC :: LinkedList_type
PRIVATE
INTEGER :: n_Nodes = 0 ! The number of nodes
TYPE( LinkedList_Node_type ), POINTER :: First => NULL() ! Pointer to the first node
END TYPE LinkedList_type



Now, what I would usually do to define the NodeData_type derived type would be to USE its definition module, e.g.


USE NodeData_Define

which would contain

  TYPE, PUBLIC :: NodeData_type
    ...stick whatever you want in here
  END TYPE NodeData_type

But let's say I have a structure definition, Apples_type, in a module file Apples_Define.f90. The definition in my LinkedList.f90 module should now be,

USE Apples_Define, NodeData_type => Apples_type

Is there a way to avoid doing this in the linked list module? That is, how does one "generic-ify" the "Apples" name?

One thing I thought of was, for every instance where I use my linked list I have a "dummy" NodeData_Define.f90 that would contain:

MODULE NodeData_Define
  USE Apples_Define, NodeData_type => Apples_type
  PRIVATE
  PUBLIC :: NodeData_type
END MODULE NodeData_Define

But then, I thought, what if I want two separate linked lists in the same program, say one to handle apple structures (via Apples_Define.f90) and one for banana structures (via Bananas_Define.f90) ?

Is this doable in Fortran95 ?


In a word, no. I'm sure the brains of the group can explain specifically why not, but this is just to give a quick answer. It really comes down to the fact that Fortran is strongly typed, IMHO.


However, it might be sort of possible in F2003, where (IIRC) you can have typless pointers or somesuch.

cheers,

Rich
.