f



fortran type bound procedure global variable access?

I have defined a single type bound procedure in a module that uses several module level public global variables. When I use this type definition to declare a variable and try to access these global variables (for example: test%global), I get the error:

"This is not a field name (global) that is defined in the encompassing structure". 

I can access global variables using the method above with a normal module without any problems. I am obviously missing something.   Thanks for the help....
0
sgihal56
12/22/2016 9:58:14 PM
comp.lang.fortran 11941 articles. 2 followers. Post Follow

10 Replies
603 Views

Similar Articles

[PageSpeed] 25

<sgihal56@gmail.com> wrote:

> I have defined a single type bound procedure in a module that uses several
> module level public global variables. When I use this type definition to
> declare a variable and try to access these global variables (for
> example: test%global), I get the error:
> 
> "This is not a field name (global) that is defined in the encompassing
> structure".
> 
> I can access global variables using the method above with a normal module
> without any problems. I am obviously missing something.   Thanks for the
> help....

An important piece of advice wheneve trying to ask for help. *SHOW* the
code instead of trying to describe it. This is a *VERY* common problem.
Best to learn the lesson or you'll keep seeing the same sort of reply.

I can't parse the description above sufficiently to tell what you are
doing. It doesn't sound like it makes much sense... which is probably
why it doesn't work, but I'd need to see actual code.

-- 
Richard Maine
email: last name at domain . net
dimnain: summer-triangle
0
nospam
12/22/2016 11:24:24 PM
Richard,

I understand the need to see the code but am limited in what can be posted. Here is a sanitized version of the type bound procedure definition. I am trying to directly access the variable "global_variable".

module sample_type_bound_procedure

   use set_precision, only : i4, dp

   implicit none

   private 

   logical, public,  save :: global_variable = .false.  
!     ..
!     .. Parameter Statements ..
   type sample_table
      real(kind=dp), dimension(:), allocatable :: data_table    

      real(kind=dp)    :: field1 
      real(kind=dp)    :: field2  
      real(kind=dp)    :: field3  
      integer(kind=i4) :: field4  
      integer(kind=i4) :: field5 
      integer(kind=i4) :: field6  
      integer(kind=i4) :: field7  
      integer(kind=i4) :: field8  
      integer(kind=i4) :: field9  
      integer(kind=i4) :: field10  
      character(len=132) :: title1  
      character(len=132) :: title2  
   contains
      procedure, nopass :: read =>  read_table
      procedure, nopass :: get  =>  get_table_data
   end type sample_table
!     ..
!     .. Define Public Routines  ..
   public :: sample_table
   public :: read_table
   public :: get_table_data

   contains
      subroutine read_table(value1, value2)
         table_exist = .true.
      end subroutine read_table 
      function get_table_data(value1, value2, value3)  result(return_value)  
      end function get_table_data

end module sample_type_bound_procedure

0
sgihal56
12/23/2016 12:04:16 AM
Richard,

Here is a sanitized version of the type bound procedure definition. I am trying to access the global variable "global_variable" without the need for a "getter" function if possible.  

module sample_type_bound_procedure

   use set_precision, only : i4, dp

   implicit none

   private 

   logical, public,  save :: global_variable = .false.  

   type sample_table
      real(kind=dp), dimension(:), allocatable :: data_table    

      real(kind=dp)    :: field1 
      real(kind=dp)    :: field2  
      real(kind=dp)    :: field3  
      integer(kind=i4) :: field4  
      integer(kind=i4) :: field5 
      integer(kind=i4) :: field6  
      integer(kind=i4) :: field7  
      integer(kind=i4) :: field8  
      integer(kind=i4) :: field9  
      integer(kind=i4) :: field10  
      character(len=132) :: title1  
      character(len=132) :: title2  
   contains
      procedure, nopass :: read =>  read_table
      procedure, nopass :: get  =>  get_table_data
   end type sample_table

   public :: sample_table
   public :: read_table
   public :: get_table_data

   contains
      subroutine read_table(value1, value2)
         table_exist = .true.
      end subroutine read_table 
      function get_table_data(value1, value2, value3)  result(return_value)  
      end function get_table_data

end module sample_type_bound_procedure


0
sgihal56
12/23/2016 12:07:45 AM
<sgihal56@gmail.com> wrote:

> I understand the need to see the code but am limited in what can be
> posted. Here is a sanitized version of the type bound procedure
> definition. I am trying to directly access the variable
> "global_variable".
> 
> module sample_type_bound_procedure
> 
>    use set_precision, only : i4, dp
> 
>    implicit none
> 
>    private 
> 
>    logical, public,  save :: global_variable = .false.  
> !     ..
> !     .. Parameter Statements ..
>    type sample_table
>       real(kind=dp), dimension(:), allocatable :: data_table    
> 
>       real(kind=dp)    :: field1 
>       real(kind=dp)    :: field2  
>       real(kind=dp)    :: field3  
>       integer(kind=i4) :: field4  
>       integer(kind=i4) :: field5 
>       integer(kind=i4) :: field6  
>       integer(kind=i4) :: field7  
>       integer(kind=i4) :: field8  
>       integer(kind=i4) :: field9  
>       integer(kind=i4) :: field10  
>       character(len=132) :: title1  
>       character(len=132) :: title2  
>    contains
>       procedure, nopass :: read =>  read_table
>       procedure, nopass :: get  =>  get_table_data
>    end type sample_table
> !     ..
> !     .. Define Public Routines  ..
>    public :: sample_table
>    public :: read_table
>    public :: get_table_data
> 
>    contains
>       subroutine read_table(value1, value2)
>          table_exist = .true.
>       end subroutine read_table 
>       function get_table_data(value1, value2, value3)  result(return_value)
>       end function get_table_data
> 
> end module sample_type_bound_procedure

Well, that's still missing critical information such as exactly how you
are trying to access it. But I'll make some guesses by interpolating
this and your prior post.

Indeed, global_variable is not a component of the derived type, or of
anything for that matter. So nothing of any form like
anything%global_variable is going to access it.

It has nothing to do with the type declaration or any type-bound
procedures, other than it happens to be declared in the same module.
Being declared in the same module does *NOT* establish some other magic
relationship.

The existance of that type declaration and the type-bound procedure
has nothing to do with how you access global_variable. You access it the
same way as you would if all that other stuff was absent. USE the module
sample_type_bound_procedure and then just refer to global variable - not
anything%global_variable.

-- 
Richard Maine
email: last name at domain . net
dimnain: summer-triangle
0
nospam
12/23/2016 12:14:40 AM
Thanks Richard you really helped clarify a few things. I am still confused on what happens to global_variable if I declare multiple variables as sample_table. Does a separate copy of global_variable get created for each variable declared as sample_table?  

0
sgihal56
12/23/2016 12:28:56 AM
<sgihal56@gmail.com> wrote:

> Thanks Richard you really helped clarify a few things. I am still confused
> on what happens to global_variable if I declare multiple variables as
> sample_table. Does a separate copy of global_variable get created for
> each variable declared as sample_table?

No. I repeat that it has nothing - *NOTHING* to do with anything else in
the module. It is just an ordinary module variable.

The type sample_variable is just a type that happens to be declared in
the module. You don't get a whole new module for everything declared
with that type. There is only just one instance of the module - ever.
Modules never have multiple instances.

You are probably misleading yourself by naming the module
sample_type-bound_procedure. The module is not in fact the type bound
procedure or particularly related to the type-bound procedure. The
type-bound procedure is just one of possibly many things that happen to
be ideclared n the module. It has no special relationship with the
module. So no, declaring something of that type doesn't cause anything
to happen in regards to the module.

-- 
Richard Maine
email: last name at domain . net
dimnain: summer-triangle
0
nospam
12/23/2016 12:46:48 AM
On Thursday, December 22, 2016 at 6:28:59 PM UTC-6, sgih...@gmail.com wrote:
> Thanks Richard you really helped clarify a few things. I am still confused on what happens to global_variable if I declare multiple variables as sample_table. Does a separate copy of global_variable get created for each variable declared as sample_table?

Some of the confusion can be caused by the names that you chose. Calling a variable "global_variable" does not make it global. If you are not immune to self-deception, use a more innocuous variable name, at least until you get your program to work.
0
cyrmag7
12/23/2016 11:09:06 AM
cyrmag7 <cyrmag7@gmail.com> schrieb:

> Some of the confusion can be caused by the names that you
> chose. Calling a variable "global_variable" does not make it
> global.

To quote Richard Maine: "The compiler does not change its operation to
conform to the meanings implied by your variable names."
0
Thomas
12/23/2016 1:34:10 PM
In article <o3j94i$kiu$1@newsreader4.netcologne.de>, Thomas Koenig
<tkoenig@netcologne.de> writes: 

> cyrmag7 <cyrmag7@gmail.com> schrieb:
> 
> > Some of the confusion can be caused by the names that you
> > chose. Calling a variable "global_variable" does not make it
> > global.
> 
> To quote Richard Maine: "The compiler does not change its operation to
> conform to the meanings implied by your variable names."

True, of course.  On the other hand, one is programming not only for the 
compiler, but for a human reading the code, so names which have some 
relation to the purposes and scopes of the variables do, of course, make 
sense.

-------------------------------------------------------------------------------
Programming is the art of telling another human being what one wants the
computer to do.

                                                                ---Donald Knuth
-------------------------------------------------------------------------------
Programming is an exercise in being precise in telling the computer exactly
what to do. The first step in that is having a precise notion in your own mind.

                                                               ---Richard Maine
-------------------------------------------------------------------------------
If I dont have to deliver documentation, fix bugs, and respond to requests for
changes 3 years after I deliver the code, it's not really programming.

                                                             ---Gene Wagenbreth

0
helbig
12/23/2016 1:48:35 PM
The module name and the use of the variable name "global_variable" were used to help clarify my post and do   not reflect the actual module or variable names. I was just trying to get a better understanding on exactly how a type bound procedure works.
0
sgihal56
12/23/2016 5:47:23 PM
Reply: