questions about type-bound procedures 
Author Message
 questions about type-bound procedures

Hello everyone,

I have a module containing a public abstract data type:

MODULE Lens_Object
  IMPLICIT NONE
  PRIVATE
  TYPE, ABSTRACT, PUBLIC :: Lens_Object_T
  END TYPE Lens_Object_T
END MODULE Lens_Object

I then have a module in which I define a data type that extends the
abstract type:

MODULE Element

  USE Lens_Object, ONLY : Lens_Object_T
  IMPLICIT NONE
  PRIVATE

  TYPE, EXTENDS (Lens_Object_T), PUBLIC :: Element_T
    PRIVATE
    INTEGER :: number
    REAL :: radii(2), thickness
  CONTAINS
    PROCEDURE :: Get_Number_Element
    PROCEDURE :: Set_Number_Element
  END TYPE Element_T

CONTAINS

  ELEMENTAL FUNCTION Get_Number_Element (this) &
      RESULT (return_Value)
    CLASS (Element_T), INTENT (IN) :: this
    INTEGER (KIND (this % number)) :: return_Value

    return_Value = this % number

  END FUNCTION Get_Number_Element

  SUBROUTINE Set_Number_Element (this, passed_Value)

    CLASS (Element_T), INTENT (IN OUT) :: this
    INTEGER, INTENT (IN) :: passed_Value

    this % number = passed_Value

  END SUBROUTINE Set_Number_Element

END MODULE Element

Note that there is a global PRIVATE statement, that the new type,
Element_T, has the PUBLIC attribute, and that there is a PRIVATE
statement in type definition.

Here is a second module, in which a second type, Lens_T, extends type
Lens_Object_T. It should be noted that type Lens_T DOES NOT extend
type Element_T.

MODULE Lens

  USE Element, ONLY : Element_T
  USE Lens_Object, ONLY : Lens_Object_T
  IMPLICIT NONE
  PRIVATE

  INTEGER, PARAMETER :: PROJECT_NAME_LEN = 80

  TYPE, EXTENDS (Lens_Object_T), PUBLIC :: Lens_T
    PRIVATE
    CHARACTER (PROJECT_NAME_LEN) :: project_name
    TYPE (Element_T), ALLOCATABLE :: elements(:)
  CONTAINS
    PROCEDURE :: Get_Number_Lens
    PROCEDURE :: Set_Number_Lens
  END TYPE Lens_T

CONTAINS

 ELEMENTAL FUNCTION Get_Number_Lens (this, &
     element_Index) RESULT (return_Value)

    CLASS (Lens_T), INTENT (IN) :: this
    INTEGER, INTENT (IN) :: element_Index
    INTEGER :: return_Value

    TYPE (Element_T) :: one_element

    ! don't do anything if the elements component isn't allocated,
    ! or, if it is, the element index is out of bounds.

    ASSOCIATE (one_element => this % &
        elements(element_Index))
      IF ( ALLOCATED (this % elements) ) THEN
        IF (      LBOUND (this % elements, DIM = 1) <= &
                   element_Index &
            .AND. element_Index <= &
                      UBOUND (this % elements, DIM = 1) ) &
        THEN
          return_Value = one_element % Get_Number_Element ()
        ELSE
          return_Value = 0
        END IF
      ELSE
        return_Value = 0
      END IF
    END ASSOCIATE

  END FUNCTION Get_Number_Lens

  SUBROUTINE Set_Number_Lens (this, element_Index, &
       passed_Value)

    CLASS (Lens_T), INTENT (IN OUT) :: this
    INTEGER, INTENT (IN) :: element_Index
    INTEGER, INTENT (IN) :: passed_Value

    TYPE (Element_T) :: one_element

    ! don't do anything if the elements component isn't allocated,
    ! or, if it is, the element index is out of bounds.

    ASSOCIATE (one_element => this % &
         elements(element_Index))
      IF (ALLOCATED (this % elements)) THEN
        IF (      LBOUND (this % elements, DIM = 1) <= &
                   element_Index &
            .AND. element_Index <= &
                      UBOUND (this % elements, DIM = 1) ) &
          CALL one_element % &
                    Set_Number_Element (passed_Value)
      END IF
    END ASSOCIATE

  END SUBROUTINE Set_Number_Lens

END MODULE Lens

I have several questions:

As best I understand the fortran 2003 standard, the two type-bound
procedures of type Element_T have the PUBLIC attribute as the module
is currently written, i.e., it is NOT necessary to list them in a
separate PUBLIC statement, nor is it necessary to add a PUBLIC
attribute to them in the type definition. Is that correct?

In module Lens, the statement

  USE Element, ONLY : Element_T

is sufficient to grant access to not only type Element_T, but also to
its two type-bound procedures. Is that correct?

In module Lens there are two references to the type-bound procedures
of type Element_T. One is

   return_Value = one_element % Get_Number_Element ()

and the second is

   CALL one_element % Set_Number_Element (passed_Value)

Are they correct as they are written?

I'll appreciate any help anyone can give me on these points.

Norm Clerman



Fri, 13 May 2011 07:14:01 GMT  
 questions about type-bound procedures

Quote:
> I have several questions:

> As best I understand the Fortran 2003 standard, the two type-bound
> procedures of type Element_T have the PUBLIC attribute as the module
> is currently written, i.e., it is NOT necessary to list them in a
> separate PUBLIC statement, nor is it necessary to add a PUBLIC
> attribute to them in the type definition. Is that correct?

I haven't read it in the standard myself, but my understanding is that
you are correct and my experience with the IBM compiler corroborates
it.  In fact, the compiler doesn't even allow a global PUBLIC such as

type sometype
contains
  public ! not allowed
  procedure :: someprocedure
end type

I would prefer to be able to make it explicit as above, but it's a
minor point.

Quote:

> In module Lens, the statement

> ? USE Element, ONLY : Element_T

> is sufficient to grant access to not only type Element_T, but also to
> its two type-bound procedures. Is that correct?

Yes. I really like this feature. It allows me to use the 'only' clause
in a greatly simplified manner.

Quote:
> In module Lens there are two references to the type-bound procedures
> of type Element_T. One is

> ? ?return_Value = one_element % Get_Number_Element ()

> and the second is

> ? ?CALL one_element % Set_Number_Element (passed_Value)

> Are they correct as they are written?

> I'll appreciate any help anyone can give me on these points.

I have no experience with the ASSOCIATE statement that precedes these,
so I'll let someone else answer this part.

Damian



Fri, 13 May 2011 21:27:14 GMT  
 questions about type-bound procedures
P.S. Out of curiosity, what compiler are you using?  The last time I
checked (a year ago), IBM XL Fortran was the only compiler I found
that supported these features, but I suspect the Cray compiler does
also (I haven't tried it yet) and I suspect NAG probably does by now
as well (they were missing a few of the object-oriented features in
2007, but they were so close that I'm guessing they have everything by
now).


Fri, 13 May 2011 21:30:47 GMT  
 questions about type-bound procedures

Quote:
> P.S. Out of curiosity, what compiler are you using?  The last time I
> checked (a year ago), IBM XL Fortran was the only compiler I found
> that supported these features, but I suspect the Cray compiler does
> also (I haven't tried it yet) and I suspect NAG probably does by now
> as well (they were missing a few of the object-oriented features in
> 2007, but they were so close that I'm guessing they have everything by
> now).

Damian,

  Thanks for replying. I'm using the NAG compiler, version 5.2. The
reason I posted this inquiry is that it is, unfortunately, failing to
compile this code. I wasn't sure it (the code) was correct, so I
thought it would be a good idea to ask everyone's opinion.

Norm



Fri, 13 May 2011 22:13:21 GMT  
 questions about type-bound procedures

Quote:

> > P.S. Out of curiosity, what compiler are you using? ?The last time I
> > checked (a year ago), IBM XL Fortran was the only compiler I found
> > that supported these features, but I suspect the Cray compiler does
> > also (I haven't tried it yet) and I suspect NAG probably does by now
> > as well (they were missing a few of the object-oriented features in
> > 2007, but they were so close that I'm guessing they have everything by
> > now).

> Damian,

> ? Thanks for replying. I'm using the NAG compiler, version 5.2. The
> reason I posted this inquiry is that it is, unfortunately, failing to
> compile this code. I wasn't sure it (the code) was correct, so I
> thought it would be a good idea to ask everyone's opinion.

> Norm

I just verified that IBM XL Fortran 12.1 (on both AIX and on Linux)
compiles your code with no errors or warnings.

Damian



Sat, 14 May 2011 00:08:55 GMT  
 questions about type-bound procedures

Quote:


> > > P.S. Out of curiosity, what compiler are you using?  The last time I
> > > checked (a year ago), IBM XL Fortran was the only compiler I found
> > > that supported these features, but I suspect the Cray compiler does
> > > also (I haven't tried it yet) and I suspect NAG probably does by now
> > > as well (they were missing a few of the object-oriented features in
> > > 2007, but they were so close that I'm guessing they have everything by
> > > now).

> > Damian,

> >   Thanks for replying. I'm using the NAG compiler, version 5.2. The
> > reason I posted this inquiry is that it is, unfortunately, failing to
> > compile this code. I wasn't sure it (the code) was correct, so I
> > thought it would be a good idea to ask everyone's opinion.

> > Norm

> I just verified that IBM XL Fortran 12.1 (on both AIX and on Linux)
> compiles your code with no errors or warnings.

> Damian

Damian,

  You're a prince! Many thanks.

Norm



Sat, 14 May 2011 00:21:08 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. procedure variables vs. type-bound procedures

2. Explicit interface of a type-bound procedure bounded to an instance (F2003)

3. type-bound procedures and type extension

4. Procedure types and dynamic binding

5. Passing type-bound procedures as arguments?

6. Passing module procedures to external procedures -- type issues

7. Question about the Source Procedure and Return types

8. Is there a FAQ and a question about procedure types

9. question about record type parameter in procedure

10. Oberon-2: Type Bound Proc question

11. Oberon-2: Type Bound Proc question

12. Access types vs. Record types as procedure parameters

 

 
Powered by phpBB® Forum Software