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

User defined operator applied to array element component in output list of write statement in the action stmt of an if stmt

IanH
Contributeur émérite III
906 Visites

How's that for a descriptive title?

I like user defined operators, perhaps a bit too much at times.

MODULE M
  IMPLICIT NONE
  PRIVATE
  TYPE, PUBLIC :: Thing
    ! Rewind to F95 just for old times sake.
    CHARACTER(10) :: name
  END TYPE Thing
  INTERFACE OPERATOR(.NameOf.)
    MODULE PROCEDURE name_of
  END INTERFACE OPERATOR(.NameOf.)
CONTAINS
  FUNCTION name_of(object)
    TYPE(Thing), INTENT(IN) :: object
    CHARACTER(10) :: name_of
    !****
    name_of = object%name
  END FUNCTION name_of
END MODULE M

PROGRAM TroublesomeDots
  USE m
  IMPLICIT NONE
  
  TYPE Container
    TYPE(Thing) :: item
  END TYPE Container
  
  TYPE(Container) :: array(3)
  
  array(1)%item%name = 'Thomas'
  array(2)%item%name = 'Percy'
  array(3)%item%name = 'James'
  
  IF (.TRUE.) WRITE (*,*) .NameOf. array(1)%item
END PROGRAM TroublesomeDots

 

>ifort /check:all /warn:all /standard-semantics /stand:f95 "2014-09-24 operator.f90"
Intel(R) Visual Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.0.108 Build 20140726
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

2014-09-24 operator.f90(34): error #5082: Syntax error, found '.' when expecting one of: ( , <END-OF-STATEMENT> ; <IDENTIFIER> <CHAR_CON_KIND_PARAM> <CHAR_NAM_KIND_PARAM> ...
  IF (.TRUE.) WRITE (*,*) .NameOf. array(1)%item
--------------------------^
2014-09-24 operator.f90(34): error #5082: Syntax error, found '%' when expecting one of: * ) :: , <END-OF-STATEMENT> ; . (/ + - : ] /) . ' ** / // > .LT. ...
  IF (.TRUE.) WRITE (*,*) .NameOf. array(1)%item
-------------------------------------------^
compilation aborted for 2014-09-24 operator.f90 (code 1)

 

0 Compliments
3 Réponses
jimdempseyatthecove
Contributeur émérite III
906 Visites

How about: IF (.TRUE.) WRITE (*,*) .NameOf. (array(1)%item)

It may be that the operator .NameOf. has equal or greater precedence than %.

Defined unary operators have highest precedence of "operators". I do not know if % (member operator) is classified as an operator. If it is, it is not listed in the precedence of operators table in the document.

Jim Dempsey

0 Compliments
Steven_L_Intel1
Employé
906 Visites

% is not an operator.

Putting parentheses around the reference works, but there's an error in Ian's code in that .NameOf. is private to the module and can't be seen by the program. If you fix that, and use the parentheses, it works. 

Congratulations! I hadn't seen a "dot" issue in several years now, but you found another one. Escalated as DPD200361374.

0 Compliments
IanH
Contributeur émérite III
906 Visites

Well that's what I get for trying to be clever.  Originally I had the operator as a generic binding, but I thought I'd see if I could make this look like F95.

0 Compliments
Répondre