Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

constructor for empty arrays

kolber__Michael
New Contributor I
831 Views

I am working on an example using classes from the Guide to Fortran 2008 Programming.  I want to see if classes will help in what I am trying to do.  I have worked with classes in other languages .

Here is the part of the code that I get an error from.

module v_q_module
use vehicle_module
implicit none
private

type :: node_type
class(vehicle_type), allocatable :: v
end type node_type

type, public :: q_type
private
type(node_type), dimension(:), allocatable :: vehicles
contains
procedure :: empty, is_empty, insert, remove, print_licenses
end type q_type
contains
subroutine empty(q)
class(q_type), intent(out) :: q
q%vehicles = [ node_type :: ]
end subroutine empty
end module v_q_module

The error is the following:

C:\Michael\traffic_simulation\traffic_simulation\module2.f90(19): error #5082: Syntax error, found '::' when expecting one of: , : (/ ]
C:\Michael\traffic_simulation\traffic_simulation\module2.f90(19): error #6478: A type-name must not be used as a variable. [NODE_TYPE]
compilation aborted for C:\Michael\traffic_simulation\traffic_simulation\module2.f90 (code 1)

All the reading I have done tells me that this line should be a valid statement.

q%vehicles = [ node_type :: ]

Thanks.

Michael

 

 

0 Kudos
6 Replies
FortranFan
Honored Contributor II
824 Views

It is a bug in Intel Fortran compiler, please submit a support request if you can at Intel Online Service Center.

A couple of suggestions:

  1. Try another compiler or two if you can to guide your learning of modern Fortran e.g., open-source gfortran (https://gcc.gnu.org/wiki/GFortran) and if possible, another commercial compiler - see point 2,
  2. Consider participating in Fortran language discourse at https://fortran-lang.discourse.group/.  where you can find more options and resources re: Fortran and other compilers, etc..

Note this variant below of your code in the original post.  It works as I expect using gfortran:

 

module vehicle_module
   type :: vehicle_type
   end type
end module
module v_q_module
   use vehicle_module
   implicit none
   private

   type :: node_type
      class(vehicle_type), allocatable :: v
   end type node_type

   type, public :: q_type
      private
      type(node_type), dimension(:), allocatable :: vehicles
   contains
      procedure :: empty, get_num_vehicles
   end type q_type
contains
   subroutine empty(q)
      class(q_type), intent(out) :: q
      q%vehicles = [ node_type :: ]
   end subroutine empty
   function get_num_vehicles(q) result(num)
      class(q_type), intent(in) :: q
      integer :: num
      num = 0
      if ( allocated(q%vehicles) ) num = size(q%vehicles)
   end function
end module v_q_module

   use v_q_module
   type(q_type) :: foo
   call foo%empty()
   print *, "number of vehicles in foo = ", foo%get_num_vehicles(), "; expected is 0."
end

 

 

 

 number of vehicles in foo =            0 ; expected is 0.

 

0 Kudos
kolber__Michael
New Contributor I
819 Views

FortranFan,

Thanks for the advice.  I would have to do the extra stuff at home.  We are bound to the Intel complier at work.  This project is something that I am doing to make a contribution at work when I do not have an upgrade project for the current application.

 

0 Kudos
FortranFan
Honored Contributor II
791 Views

 

@kolber__Michael wrote:

.. We are bound to the Intel complier at work.  This project is something that
I am doing to make a contribution at work when I do not have an upgrade
project for the current application

 

You can consider a workaround based on an "original" facility in Fortran for dynamic memory i.e., the ALLOCATE statement:

 

allocate( q%vehicles(0) )

 

 

0 Kudos
Ron_Green
Moderator
773 Views

This is fixed in the nightly builds here for the ifort BETA in the oneAPI HPC Toolkit.  It's still a bug in the current beta08 release of this kit.  But the nightly build which is going into the oneAPI HPC Toolkit for the next beta, beta09, has this working:

rwgreen:~/quad/u1198085$ module load comp/nightlybuild
rwgreen:~/quad/u1198085$ ifort -O0 -what -o repro repro.f90
 Intel(R) Fortran 2021.1-1898
rwgreen@orcsle156:~/quad/u1198085$ ./repro
 number of vehicles in foo =            0 ; expected is 0.


rwgreen:~/quad/u1198085$ cat repro.f90
module vehicle_module
   type :: vehicle_type
   end type
end module
module v_q_module
   use vehicle_module
   implicit none
   private

   type :: node_type
      class(vehicle_type), allocatable :: v
   end type node_type

   type, public :: q_type
      private
      type(node_type), dimension(:), allocatable :: vehicles
   contains
      procedure :: empty, get_num_vehicles
   end type q_type
contains
   subroutine empty(q)
      class(q_type), intent(out) :: q
      q%vehicles = [ node_type :: ] 
   end subroutine empty
   function get_num_vehicles(q) result(num)
      class(q_type), intent(in) :: q
      integer :: num
      num = 0
      if ( allocated(q%vehicles) ) num = size(q%vehicles)
   end function
end module v_q_module

   use v_q_module
   type(q_type) :: foo
   call foo%empty()
   print *, "number of vehicles in foo = ", foo%get_num_vehicles(), "; expected is 0."
end
0 Kudos
Steve_Lionel
Honored Contributor III
811 Views

I remember reporting a similar bug a long time ago. I wonder what's different about this case?

0 Kudos
IanH
Honored Contributor II
794 Views

The passing of roughly eight years!

0 Kudos
Reply