# 'self' and 'this' in OO-Fortran

**URL:** https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615
**Category:** Visual Studio Code
**Created:** [October 9, 2023, 1:57pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615 "2023-10-09T13:57:03Z")
**Posts on this page:** 14
**Page:** 2

<div class="post-metadata">

### Author: ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)
#### Post date: [October 11, 2023, 1:11pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/21 "2023-10-11T13:11:15Z")

</div>

> [@ormo](#):
>
> … Do you know a tutorial that is aware of the struggle that (some) people have with ‘self/this’ and go at length to explain it. (My 4 English modern-Fortran-textbooks do not (try to) explain the issue.) …

@ormo,

Hopefully you have a better feel of `self/this` in the context of Fortran.

If you do pursue object-oriented design and programming (OOD and OOP), hopefully that will be **preceded** by thorough object-oriented analysis (OOA), and then with the aspect of your `self/this`, you may want to consider employing the `PASS` attribute in Fortran as self-evident indicator! And be consistent with your usage.

```fortran
module messenger_m
   type :: messenger_t
      private
      character(len=:), allocatable :: message
   contains
      procedure, pass(this) :: get => get_message !<-- PASSed object called as `this`
      procedure, pass(self) :: set => set_message !<-- PASSed object called as `self` 
   end type
contains
   function get_message( this ) result(message)
      class(messenger_t), intent(in) :: this
      character(len=:), allocatable :: message
      message = this%message
   end function 
   subroutine set_message( self )
      class(messenger_t), intent(inout) :: self
      self%message = "Hello World!"
   end subroutine 
end module
   use messenger_m, only : messenger_t
   type(messenger_t) :: msg
   call msg%set()
   print *, msg%get()
end 

```

```auto
c:\temp>gfortran -ffree-form p.f -o p.exe

C:\temp>p.exe
 Hello World!

```

---

<div class="post-metadata">

### Author: ![ormo](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ormo](https://fortran-lang.discourse.group/u/ormo)
#### Post date: [October 11, 2023, 1:46pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/22 "2023-10-11T13:46:54Z")

</div>

Thank you for the pep-talk, Pap. Nonetheless, when existing code one needs to work with/on is OO, thorough understanding of ‘self/this’ appears indispensable.  
As for vmagnin’s query: I taught myself Fortran from Daniel D. McCracken’s book “A guide to FORTRAN IV programming”, Wiley 1965, in 1968. BTW, this book uses flowcharts.

---

<div class="post-metadata">

### Author: ![Federchen](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@Federchen](https://fortran-lang.discourse.group/u/Federchen)
#### Post date: [October 12, 2023, 12:15pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/23 "2023-10-12T12:15:11Z")

</div>

I am using the same OOP syntax with Coarray Fortran for distributed (fragmented) objects programming ([Fragmented object - Wikipedia](https://en.wikipedia.org/wiki/Fragmented_object)).

Syntactically the only difference is the implementation of the type bound procedures where I have to combine multiple (usually 2) subroutines into a single module procedure to make kernel programming more easily feasible. All fragments that form a single fragmented object are implemented as abstract classes.

These quotes from my Github site may equally well fit here ([GitHub - MichaelSiehl/Spatial\_Fortran\_1](https://github.com/MichaelSiehl/Spatial_Fortran_1)):

> _“(Note: In the code examples I am using ‘fo %’ instead of the usual ‘this %’ object identifier to make a clear distinction between OOP codes and distributed objects codes. My actual codes are already embedded into a distributed (fragmented) objects model (fo), and I am using the traditional OOP syntax merely to define and implement such distributed objects.)_ “

> **“Transforming OOP towards Distributed Objects Programming (…):**  
> All of my kernel programming is already embedded into distributed objects programming, and I am already using the same OOP syntax (including inheritance) to define and implement distributed objects in Fortran. While the syntax for distributed objects is the same as in OOP, the meaning of the syntax does become something completely different with distributed objects. A crucial advantage of using OOP syntax for Distributed Objects Programming is the already improved compile time analysis for parallel programming in Fortran, since compile time analysis does not distinguish between OOP and Distributed Objects Programming.”

Using fo% as (fragmented) object identifier does indicate that only one (local) fragment of the (distributed) object is addressed at a time.

From what I can see yet, using such kind of (truly) distributed object model in Fortran could become a very efficient way to develop/map out sophisticates parallel codes for spatial devices (e.g. FPGAs) in the near future. It does already work on CPUs.

regards

---

<div class="post-metadata">

### Author: ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)
#### Post date: [October 12, 2023, 3:19pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/24 "2023-10-12T15:19:48Z")

</div>

> [@ormo](#):
>
> … I taught myself Fortran from Daniel D. McCracken’s book “A guide to FORTRAN IV programming”, Wiley 1965, in 1968. BTW, this book uses flowcharts.

@ormo,

McCracken’s book “A guide to FORTRAN IV programming”, Wiley 1965 is a good book for `FORTRAN IV`, an excellent place to learn the dominant dialect at the time

Re: “existing code one needs to work with/on is OO,” note if you are serious about this, I strongly urge you to thoroughly review the following books in order:

1. [**“FORTRAN 90 for Engineers and Scientists” by Sanford Leestma and Larry R. Nyhoff**](https://www.thriftbooks.com/w/fortran-90-for-engineers-and-scientists_larry-r-nyhoff_sanford-leestma/279562/#edition=4525390&idiq=5832367)
2. [**FORTRAN FOR SCIENTISTS & ENGINEERS, 4th Edition, By Stephen Chapman**](https://www.mheducation.com/highered/product/fortran-scientists-engineers-chapman/M9780073385891.html)
3. [**Modern Fortran Style and Usage by Norman S. Clerman and Walter Spector**](https://www.cambridge.org/us/universitypress/subjects/computer-science/scientific-computing-scientific-software/modern-fortran-style-and-usage?format=PB)
4. [**Modern Fortran Explained: Incorporating Fortran 2018 (5th edn) Michael Metcalf, John Reid, Malcolm Cohen**](https://academic.oup.com/book/26799)

---

<div class="post-metadata">

### Author: ![ormo](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ormo](https://fortran-lang.discourse.group/u/ormo)
#### Post date: [October 18, 2023, 3:13pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/25 "2023-10-18T15:13:05Z")

</div>

Dear Michael Siehl and FortranFan,

Thanks to your useful advices, I could obtain and test the code below (dvo = ormo)  
`Preformatted text`:

```auto
module MyModule ! Supplied by ChatGPT, adapted by dvo.
   implicit none ! dvo added.
       type :: MyType ! ChatGPT.
    integer :: ivalue ! ChatGPT, plus 'i'.
     real*4 :: rvalue ! dvo added.
     real*8 :: dvalue ! dvo added.
   contains
  procedure :: PrintValue ! Type-bound procedure.
    end type MyType ! ChatGPT.
CONTAINS
  subroutine PrintValue(self) ! ChatGPT.
! type (MyType), intent(in) :: self ! Error by ChatGPT.
    class(MyType), intent(in) :: self ! dvo adapted.
    print *, " Real*4: ", self%rvalue ! dvo added.
    print *, " Real*8: ", self%dvalue ! dvo added.
    print *, "Integer: ", self%ivalue ! ChatGPT, plus 'i'. 
  end subroutine PrintValue ! ChatGPT.
end module MyModule
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
program Main ! Supplied by ChatGPT, adapted by dvo.
  use MyModule
  implicit none ! dvo added.
  type(MyType) :: obj ! ChatGPT.
     obj%dvalue = 42.d0 ! dvo added.
     obj%rvalue = 42. ! dvo added.
     obj%ivalue = 42 ! ChatGPT, plus 'i'.
call obj%PrintValue ! Original ChatGPT-call suffices.
end program Main
,
which resulted in:
! [ormo@rolls5 more-stuff1]$ gfortran stuff1.f90 -o stuff1.x  
! [ormo@rolls5 more-stuff1]$ ./stuff1.x
! Real*4: 42.0000000    
! Real*8: 42.000000000000000     
! Integer: 42
! [ormo@rolls5 more-stuff1]$

```

There are 3 ‘instances’ and just 1 print-call.  
On purpose, I did not keep the order of the 3 instances constant, within the code.  
Only one of the tried orders is shown here.  
The matter is much clearer to me now.  
Thank you again.  
Dirk van Ormondt.

---

<div class="post-metadata">

### Author: ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)
#### Post date: [October 18, 2023, 3:42pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/26 "2023-10-18T15:42:23Z")

</div>

> [@ormo](#):
>
> `real*4 :: rvalue ! dvo added.`

As a matter of style, this is an odd mixture of old-fashioned (nonstandard) and new object-oriented coding style. ChatGPT produced this?

---

<div class="post-metadata">

### Author: ![ormo](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ormo](https://fortran-lang.discourse.group/u/ormo)
#### Post date: [October 18, 2023, 5:55pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/27 "2023-10-18T17:55:02Z")

</div>

True, a mixture of two styles. ChatGPT did NOT produce this. I did. It should not interfere with  
the issue at hand.

---

<div class="post-metadata">

### Author: ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)
#### Post date: [October 18, 2023, 6:23pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/28 "2023-10-18T18:23:56Z")

</div>

Glad you’re starting to get the hang of it. A couple of points.

> [@ormo](#):
>
> ```auto
> type :: MyType ! ChatGPT.
> integer :: ivalue ! ChatGPT, plus 'i'.
> real*4 :: rvalue ! dvo added.
> real*8 :: dvalue ! dvo added.
> contains
> procedure :: PrintValue ! Type-bound procedure.
> end type MyType ! ChatGPT.
> 
> ```

This is a very strange formatting style. I had to re-read it twice to notice the type definition. Not to start style-guide flame wars, but a formatting style should at least aid in seeing the structure of the code, and certainly avoid obscuring it.

> [@ormo](#):
>
> There are 3 ‘instances’

As a matter of terminology, there are not “3 ‘instances’”. There is one object with 3 components.

---

<div class="post-metadata">

### Author: ![ormo](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ormo](https://fortran-lang.discourse.group/u/ormo)
#### Post date: [October 19, 2023, 2:57pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/29 "2023-10-19T14:57:28Z")

</div>

Hello everythingfunctional,  
Sorry for wasting your time with my formatting style.  
Thank you for correcting my terminology.

---

<div class="post-metadata">

### Author: ![ormo](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ormo](https://fortran-lang.discourse.group/u/ormo)
#### Post date: [October 19, 2023, 3:11pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/30 "2023-10-19T15:11:46Z")

</div>

Hello hkvzjal,  
Thank you for your help. I understand better now. Admittedly, I have not yet fully digested the omnipresent phrase “the first argument is the type itself”. In non-OO code an argument is a just single thing, i.e., a real or integer number, while in OO a ‘type’ is an entire block of code. How to interpret this ?

---

<div class="post-metadata">

### Author: ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)
#### Post date: [October 19, 2023, 4:21pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/31 "2023-10-19T16:21:18Z")

</div>

> [@ormo](#):
>
> n non-OO code an argument is a just single thing, i.e., a real or integer number, while in OO a ‘type’ is an entire block of code.

So, try to think of an object (type(something)) not as a block of code, but as an array on steroids… meaning, just as you can pass as argument something like `integer, intent(in) :: a(3)` which is a collection of 3 values. An object or structure enables you to pass with a single name, a collection of things with different intrinsic types.

Don’t know if this was any clearer?

---

<div class="post-metadata">

### Author: ![ormo](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ormo](https://fortran-lang.discourse.group/u/ormo)
#### Post date: [October 19, 2023, 7:05pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/32 "2023-10-19T19:05:46Z")

</div>

After consulting ChatGPT about ‘on steroids’, yes, your clarification brought me a step further.  
Thank you!

---

<div class="post-metadata">

### Author: ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)
#### Post date: [October 20, 2023, 3:19pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/33 "2023-10-20T15:19:58Z")

</div>

> [@ormo](#):
>
> Hello hkvzjal,

@ormo, you may want to **learn** to use the `@` symbol followed by the nom de plume (e.g., @hkvzjal ) to refer to other readers who provide you with guidance.

---

<div class="post-metadata">

### Author: ![ormo](https://avatars.discourse-cdn.com/v4/letter/o/d6d6ee/32.png) [@ormo](https://fortran-lang.discourse.group/u/ormo)
#### Post date: [October 30, 2023, 3:02pm UTC](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615/34 "2023-10-30T15:02:52Z")

</div>

@FortranFan, compris.

[Previous page](https://fortran-lang.discourse.group/t/self-and-this-in-oo-fortran/6615.md?page=1)
