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

Unary vs binary overloaded operator

_helvio_
Beginner
842 Views
Hi again,
I am trying to overload the minus operator (-) with two procedures, one defining a subtraction of two objects. and the other defining the negative of a single object:
[fortran]MODULE scalars

   IMPLICIT NONE

   ! Access
   PRIVATE
   PUBLIC :: oldscalar
   PUBLIC :: OPERATOR(-)

   ! Parent type
   TYPE :: oldscalar
      COMPLEX :: a
   CONTAINS
      PRIVATE
      PROCEDURE :: eq_sca
      GENERIC, PUBLIC :: ASSIGNMENT(=) => eq_sca
   END TYPE oldscalar

   ! Overloaded operator
   INTERFACE OPERATOR(-)
      MODULE PROCEDURE neg_sca
      MODULE PROCEDURE sub_sca
   END INTERFACE OPERATOR(-)

CONTAINS

   ! Negative
   PURE ELEMENTAL FUNCTION neg_sca(u) RESULT(w)
      CLASS(oldscalar), INTENT(in) :: u
      TYPE(oldscalar) :: w
      w%a = -u%a
   END FUNCTION neg_sca

   ! Subtraction
   PURE ELEMENTAL FUNCTION sub_sca(u, v) RESULT(w)
      CLASS(oldscalar), INTENT(in) :: u, v
      TYPE(oldscalar) :: w
      w%a = u%a - v%a
   END FUNCTION sub_sca

   ! Convenient assignment for extensions of TYPE(oldscalar)
   PURE ELEMENTAL SUBROUTINE eq_sca(u, v)
      CLASS(oldscalar), INTENT(out) :: u
      TYPE(oldscalar) , INTENT(in)  :: v
      u%a = v%a
   END SUBROUTINE eq_sca

END MODULE scalars


MODULE fields

   USE scalars

   IMPLICIT NONE

   ! Extended type
   TYPE, EXTENDS(oldscalar) :: newscalar
   END TYPE newscalar

   ! Yet another new type
   TYPE :: scalarfield
      TYPE(newscalar), ALLOCATABLE :: site(:)
      !TYPE(oldscalar), ALLOCATABLE :: site(:)
   END TYPE scalarfield

   ! Overloaded operator
   INTERFACE OPERATOR(-)
      MODULE PROCEDURE sub_matf
   END INTERFACE OPERATOR(-)

CONTAINS

   ! Subtraction
   PURE ELEMENTAL FUNCTION sub_matf(A, B) RESULT(C)
      CLASS(scalarfield), INTENT(in) :: A, B
      TYPE (scalarfield) :: C
      C%site = A%site - B%site
   END FUNCTION sub_matf

END MODULE fields[/fortran]
This code fails to compile with the Intel Fortran Compiler 12.1.5,  returning the following error message:
mod.f90(78): error #6549: An arithmetic or LOGICAL type is required in this context.   [SITE]
      C%site = A%site - B%site
-----------------^
mod.f90(78): error #6549: An arithmetic or LOGICAL type is required in this context.   [SITE]
      C%site = A%site - B%site
--------------------------^
mod.f90(78): error #6355: This binary operation is invalid for this data type.   [SITE]
      C%site = A%site - B%site
-----------------^
mod.f90(78): error #6355: This binary operation is invalid for this data type.   [SITE]
      C%site = A%site - B%site
--------------------------^
compilation aborted for mod.f90 (code 1)
However, it compiles successfully if either I
  1. comment out line 21, i.e. dropping out the unary operation, or
  2. replace line 63 with line 64, i.e. using the parent type instead of the extended type
but I need to use the extended type. Am I doing something wrong?
Thanks,
_helvio_
0 Kudos
2 Replies
Steven_L_Intel1
Employee
842 Views
I believe this is a compiler bug. I have escalated it as issue DPD200235328 and will update this thread when I have any news.
0 Kudos
Steven_L_Intel1
Employee
842 Views
This error has been fixed in the compiler sources.  I expect the fix to appear in an update in the October timeframe.
0 Kudos
Reply