Assignment overloading of the form scalar = vector(:)

Assignment overloading of the form scalar = vector(:)

Hi,

I'm having trouble to define a overloaded assigment using:

  1. a scalar variable on the left-hande side and
  2. a 1d array on the right-hand side.

In other words, I want to do scalar = vector(:).

Is this even allowed ?

Thanks.

! ifort main.f90; ./a.out

Module MyModule
  implicit none
  private
  public        ::      MyType
  
  Type          ::      MyType
    integer     ::      Var
  contains
    generic     ,public         ::      operator(+)     =>      Addition_0d, Addition_1d
    procedure   ,private        ::      Addition_0d
    procedure   ,private        ::      Addition_1d
    generic     ,private        ::      assignment(=)   =>      Assign_0d, Assign_1d
    procedure   ,private        ::      Assign_0d
    procedure   ,private        ::      Assign_1d
  End Type
  contains
  
Function Addition_0d( This, That ) result(Res)
  class(MyType)                 ,intent(in)     ::      This
  type(MyType)                  ,intent(in)     ::      That
  type(MyType)                                  ::      Res
  Res%Var       =       This%Var + That%Var
End Function 

Function Addition_1d( This, That ) result(Res)
  class(MyType)                 ,intent(in)     ::      This
  type(MyType)  ,dimension(:)   ,intent(in)     ::      That
  type(MyType)                                  ::      Res
  Res%Var       =       This%Var + sum( That%Var )
End Function

Subroutine Assign_0d( lhs, rhs )
  class(MyType)                 ,intent(out)    ::      lhs
  type(MyType)                  ,intent(in)     ::      rhs
  lhs%Var       =       rhs%Var
End Subroutine

Subroutine Assign_1d( lhs, rhs )
  class(MyType)                 ,intent(out)    ::      lhs
  type(MyType)  ,dimension(:)   ,intent(in)     ::      rhs
  lhs%Var       =       sum( rhs%Var )
End Subroutine

End Module

Program Main
  use MyModule  ,only:  MyType
  implicit none
  
  type(MyType)                  ::      Sca_1, Sca_2, Sca_3
  type(MyType)  ,dimension(3)   ::      Vec_1_2_3
  
  Sca_1%Var = 1
  Sca_2%Var = 2
  Sca_3%Var = 0
  
  write(*,"(/,'Test_1)')")
  Sca_3 = Sca_1 + Sca_2
  if ( Sca_3%Var == 3 ) then
    write(*,"('Success: Sca_3%Var = ',g0)") Sca_3%Var
  else
    write(*,"('Failure: Sca_3%Var = ',g0)") Sca_3%Var
  end if
  
  Sca_3%Var        = 0
  Vec_1_2_3(1)%Var = 1
  Vec_1_2_3(2)%Var = 2
  Vec_1_2_3(3)%Var = 3
  
  write(*,"(/,'Test_2)')")
  Sca_3 = Sca_1 + Vec_1_2_3
  if ( Sca_3%Var == 7 ) then
    write(*,"('Success: Sca_3%Var = ',g0)") Sca_3%Var
  else
    write(*,"('Failure: Sca_3%Var = ',g0)") Sca_3%Var
  end if
  
  Sca_3%Var = 0
  write(*,"(/,'Test_3')")
  Sca_3 = Vec_1_2_3                     ! COMPILER_ERROR: error #6366: The shapes of the array expressions do not conform.   [SCA_3]
  if ( Sca_3%Var == 6 ) then
    write(*,"('Success: Sca_3%Var = ',g0)") Sca_3%Var
  else
    write(*,"('Failure: Sca_3%Var = ',g0)") Sca_3%Var
  end if
  
End Program

 

3 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.
Portrait de Steve Lionel (Intel)

The assignment operator is marked private. You need to make it public otherwise it isn't usable outside the module. The first assignment works because it is assigning a scalar to a scalar and that's intrinsic.

Steve

Oooups... of course. I didn't double-checked the code.

Sorry for that.

 

Connectez-vous pour laisser un commentaire.