Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
27 Views

up/downcasting in Fortran2003

Jump to solution

In fortran 2003, classes and OOP are defined in the standard. I would like to know how upcasting and downcasting are performed.

Is it possible to perform without using SELECT TYPE since I'm dealing with a heterogeneous collection and I can not know all the types.

Thank you

0 Kudos

Accepted Solutions
Highlighted
Valued Contributor III
27 Views

Outside of SELECT TYPE, Fortran does not really offer a mechanism for what is suggested upthread.  Depending on what a defined assignment tries to do in terms of 'downcasting', one may end up using SELECT TYPE in that assignment procedure which can then allow consuming codes to not have to employ SELECT TYPE themselves.  There may be some advantages to offering such 'syntactic sugar' for consumers of one's classes but that's about as far as one can get with the current standard in terms of minimizing SELECT TYPE in operations involving parent and extended types.

Given the snippet in Quote #3, intrinsic assignment in Fortran is often sufficient with 'upcasting' instructions such as 's = b' but where s is a polymorphic object of declared class of a parent type; defined assignments may be helpful in cases where better control of 'shallow' vs 'deep' copy is desired.  Whereas 'b = s' instruction will require a defined assignment; intrinsic assignment requires compatibility with declared types and will thus not work.  However in situations involving nested hierarchies of extended types, it can prove rather difficult to manage defined operations including assignment.  It makes sense to use great caution with such design of OO classes, especially when it comes to 'downcasting'.

View solution in original post

0 Kudos
3 Replies
Highlighted
Valued Contributor I
27 Views

This is a bit vague. I think you have to tell us a little bit more what you want to do. See some discussion here:

https://software.intel.com/en-us/forums/intel-fortran-compiler-for-linux-and-mac-os-x/topic/542833

Also if you have class_3 inherits from class_2 inherits from class_1, and all have their own (overridden) func routines, you can access a specific one by e.g.
 

class(class3) :: foo

call foo%class2%func ()

etc.

0 Kudos
Highlighted
27 Views

well i prepared a simplified example :

module test

implicit none
 type:: segment
   character(len=32) :: stype    
 end type segment

 type, extends(segment) :: user
   character(len = 32) :: uname
 end type user

 type, extends(segment) :: book
   character(len = 32) :: btitle
   integer :: bpages
   real :: budc
 end type book

 interface assignment(=)
    module procedure segment_from_book, book_from_segment
 end interface

contains
 subroutine segment_from_book (this, c)
      type(segment), intent(inout) :: this
      class(book), intent(in) :: c

      this%stype = c%stype
    end subroutine segment_from_book

    subroutine book_from_segment(this, p)
      type(book), intent(inout) :: this
      class(segment), intent(in) :: p
     

      this%stype = p%stype
      .
      .     
    end subroutine book_from_segment

and in my program main, i want to do

class(segment), allocatable :: s
 type(book) :: b
 type(user) :: u
 
  allocate(s)
  b = new_book('', 100, 40.4)
  u = new_user('')  
  s = b
  print *, b%btitle
  
  call my_print(s)
  b = s
  call my_print(s)

Well s = b and s = b am not sure if this is working correctly since am not able to see when i try to print what i get as u result .

 

0 Kudos
Highlighted
Valued Contributor III
28 Views

Outside of SELECT TYPE, Fortran does not really offer a mechanism for what is suggested upthread.  Depending on what a defined assignment tries to do in terms of 'downcasting', one may end up using SELECT TYPE in that assignment procedure which can then allow consuming codes to not have to employ SELECT TYPE themselves.  There may be some advantages to offering such 'syntactic sugar' for consumers of one's classes but that's about as far as one can get with the current standard in terms of minimizing SELECT TYPE in operations involving parent and extended types.

Given the snippet in Quote #3, intrinsic assignment in Fortran is often sufficient with 'upcasting' instructions such as 's = b' but where s is a polymorphic object of declared class of a parent type; defined assignments may be helpful in cases where better control of 'shallow' vs 'deep' copy is desired.  Whereas 'b = s' instruction will require a defined assignment; intrinsic assignment requires compatibility with declared types and will thus not work.  However in situations involving nested hierarchies of extended types, it can prove rather difficult to manage defined operations including assignment.  It makes sense to use great caution with such design of OO classes, especially when it comes to 'downcasting'.

View solution in original post

0 Kudos