Quantcast
Channel: Intel® Fortran Compiler for Linux* and macOS*
Viewing all articles
Browse latest Browse all 2583

merge statement for scalars

$
0
0

Hi,

I found the merge statement as a ternary operator quite useful even when using it outside of its original scope of array assignments. One pitfall I've realized is the fact that even the code that is in the "false-branch" might be evaluated (similar to the Boolean operations in Fortran which are not short circuited). I'm however not sure about the following code, which I find quite useful for setting default arguments

program test
  implicit none
  real, parameter :: a = 5.0
  real            :: x
  real, dimension(:), allocatable :: y

  ! this is invalid, even if I ensure that y(1) exists in case that y is allocated
  ! because y(1) might be evaluated in the case that y is not allocated
  x = merge(y(1),a,allocated(y))

  call hello(a)
  call hello()

contains

subroutine hello(b)
 implicit none
 real, intent(in), optional :: b
 real                       :: c
 
 ! what about this?
 c = merge(b,4.0,present(b))
 write(6,*) c

end subroutine hello


end program

 


Viewing all articles
Browse latest Browse all 2583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>