- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
7 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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. ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
One tool with which string substitution can be performed is sed.
For example, given the Fortran source bool.F90:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Steve. Not exactly what I wanted to hear but I can certainly live with it!!!!
David

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page