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

overload the .and. operator?

david_sallngc_com
923 Views
Hi!

Is it possible to overload the '.and.' operator in an IF statement such that,

if ( a .and. b)
...
end if

becomes,

if (a && b)
...
end if

Thank you for your help.

Sincerely,

David
0 Kudos
7 Replies
Steven_L_Intel1
Employee
923 Views
It is possible to overload operators to extend them to additional types, but I don't understand the effect you want to have. Isn't the C && operator the same, logically, as Fortran's .and. ?
0 Kudos
david_sallngc_com
923 Views
Hi Steve,

Yes, the && and .and. are the same. I write ALOT of Matlab and FORTRAN code. I wanted to overload the '.and.' for readability and cutting and pasting between codes. If there is an easy way of making the change, I would like to add that functionality to my code.

Thanks!

David
0 Kudos
mecej4
Honored Contributor III
923 Views
The obvious alternative is to use a preprocessor/macro processor.

I am assuming that you wanted 'a' and 'b' to be LOGICAL types variables; or, did you want to define .and. as a bitwise operator for other types?
0 Kudos
david_sallngc_com
923 Views

I did not mean to be that specific! I could have, for example,

real a, b

if (a > 1. && b /= 0.)

I just wanted to replace the '.and.' with an operator of my choice. I have successfully overloaded the ATAN2 function (in a module) to accomodate a special case of mine and just wanted to see if I could do this with this operator as well. Can this be done by defining it in a MODULE?

Thanks again.

David

0 Kudos
mecej4
Honored Contributor III
923 Views
One tool with which string substitution can be performed is sed.

For example, given the Fortran source bool.F90:
[fortran]program tlog
integer :: i=3, j=7
k=(i+j)/2
if(k > i & k < j)write(*,*)' In between'
end program tlog
[/fortran]
the command
[bash]sed -e "s/&/.and./g" bool.F[/bash]
produces the version

[fortran]program tlog
integer :: i=3, j=7
k=(i+j)/2
if(k > i .and. k < j)write(*,*)' In between'
end program tlog
[/fortran]
which is ready for a Fortran compiler.
0 Kudos
Steven_L_Intel1
Employee
923 Views
Oh, I see. You don't want "overload", which at least the way I define it is extending the operator (or function) name to provide additional capabilities (such as operating on additional types.) You want to add new syntax to the language. There is no way to do that in Fortran and I'd discourage you from going the preprocessor route to do so. Please write Fortran in Fortran - those who have to maintain your code will thank you.
0 Kudos
david_sallngc_com
923 Views

Thanks Steve. Not exactly what I wanted to hear but I can certainly live with it!!!!

David

0 Kudos
Reply