Skip to content

Instantly share code, notes, and snippets.

@n-s-k
Last active January 20, 2025 10:40
Show Gist options
  • Save n-s-k/de4af7ce6cc8f2c85e4b33cedb51fd88 to your computer and use it in GitHub Desktop.
Save n-s-k/de4af7ce6cc8f2c85e4b33cedb51fd88 to your computer and use it in GitHub Desktop.
Object-Oriented Programming in Fortran 2003 Part 2: Data Polymorphism

Object-Oriented Programming in Fortran 2003 Part 2: Data Polymorphism

Original article by Mark Leair, PGI Compiler Engineer

Note: This article was revised in March 2015 and again in January 2016 to bring it up-to-date with the production software release and to correct errors in the examples.

@urwiramboll
Copy link

Thank you very much for your example. There is an error in your example code of abstract_list_mod. see value definition

subroutine addValue(this, value)
    class(list) :: this
    class(*), value
    class(link), pointer :: newLink

nomatter what I chose for value Intel Parallel Studio XE 2017 gives me the following error:

If the component immediately preceding the type-bound procedure is abstract, the entire data reference before the procedure name must be polymorphic.   [LIST]	 integer_list.f90	56	

@computer-wizard
Copy link

computer-wizard commented Jan 15, 2018

Replace 'class(* ), value' with ' class(* ) :: value'
(Same error is also in list.f90 at line 50)
Correct code snippet:

subroutine addValue(this, value)
class(list) :: this
class(*) :: value
class(link), pointer :: newLink

There are also (other) errors in integer_list.f90 what makes that that module doesn't compile with Intel Parallel Studio XE 2017
but it does with pgfortran.

@Dem2018
Copy link

Dem2018 commented May 13, 2018

Hi,
When you try to create a class object, fortran gives an error. This happens if the object is created not immediately after the declaration of data types, but in the text of the program. The example is taken http://fortranwiki.org/fortran/show/Object-oriented%20programming

At the end of the code, I added the lines:

type (Circle) :: c1
c1 = Circle (1.7)

When you try to create an object "c1", the compiler issues an error (including if you remove implicit none) Error: Unexpected data declaration statement statement (1).

How, using OOP fortran, to create an object in the process of executing the program?
I'm using the Gfortran 7.3 compiler in ubuntu
Thank you.

The text of the program with an attempt to create the object "C1"

module class_Circle
implicit none
private
public :: Circle, circle_area, circle_print
real :: pi = 3.1416 ! Class-wide private constant

	type Circle
	real :: radius
	end type Circle

contains
	function circle_area(this) result(area)
	type(Circle), intent(in) :: this
	real :: area
	area = pi * this%radius**2
	end function circle_area

	subroutine circle_print(this)
	type(Circle), intent(in) :: this
	real :: area
	area = circle_area(this)  ! Call the circle_area function
	print *, 'Circle: r = ', this%radius, ' area = ', area
	end subroutine circle_print
end module class_Circle

program circle_test
use class_Circle
implicit none

	type(Circle) :: c     ! Declare a variable of type Circle.
	c = Circle(1.5)       ! Use the implicit constructor, radius = 1.5.
	call circle_print(c)  ! Call a class subroutine

	!New object  
	type(Circle) :: c1		! Error: Unexpected data declaration statement at (1)
	c1 = Circle(1.7)  
	  
end program circle_test

@kernie
Copy link

kernie commented Sep 2, 2019

Hi,
When you try to create a class object, fortran gives an error. This happens if the object is created not immediately after the declaration of data types, but in the text of the program. The example is taken http://fortranwiki.org/fortran/show/Object-oriented%20programming

At the end of the code, I added the lines:

type (Circle) :: c1
c1 = Circle (1.7)

When you try to create an object "c1", the compiler issues an error (including if you remove implicit none) Error: Unexpected data declaration statement statement (1).

How, using OOP fortran, to create an object in the process of executing the program?
I'm using the Gfortran 7.3 compiler in ubuntu
Thank you.

The text of the program with an attempt to create the object "C1"

module class_Circle
implicit none
private
public :: Circle, circle_area, circle_print
real :: pi = 3.1416 ! Class-wide private constant

	type Circle
	real :: radius
	end type Circle

contains
	function circle_area(this) result(area)
	type(Circle), intent(in) :: this
	real :: area
	area = pi * this%radius**2
	end function circle_area

	subroutine circle_print(this)
	type(Circle), intent(in) :: this
	real :: area
	area = circle_area(this)  ! Call the circle_area function
	print *, 'Circle: r = ', this%radius, ' area = ', area
	end subroutine circle_print
end module class_Circle

program circle_test
use class_Circle
implicit none

	type(Circle) :: c     ! Declare a variable of type Circle.
	c = Circle(1.5)       ! Use the implicit constructor, radius = 1.5.
	call circle_print(c)  ! Call a class subroutine

	!New object  
	type(Circle) :: c1		! Error: Unexpected data declaration statement at (1)
	c1 = Circle(1.7)  
	  
end program circle_test

Hi,

in Fortran data declaration happens at the beginning of an entity (program, module, subroutine, function) in contrast to other programming languages. It isn't allowed afterwards.

@n-s-k
Copy link
Author

n-s-k commented Apr 2, 2020

Replace 'class(* ), value' with ' class(* ) :: value'
(Same error is also in list.f90 at line 50)
Correct code snippet:

subroutine addValue(this, value)
class(list) :: this
class(*) :: value
class(link), pointer :: newLink

There are also (other) errors in integer_list.f90 what makes that that module doesn't compile with Intel Parallel Studio XE 2017
but it does with pgfortran.

I finally get to fix that. It should now compile with ifort and gcc.

@GHNewbiee
Copy link

Tal for the articles.Please consider to add a destroyList procedure. Especially in the second example, where data are copied from the list to an array, list could be destroyed to release memory. Tia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment