Is there another function in Fortran like `merge` that does not evaluate its arguments?

That’s been my contention for a while now. If it’s not pure (at least), you have no idea where you can put calls to it safely. I.e

integer :: i
i = 0
print *, foo() + foo()
contains
function foo()
  integer :: foo
  print *, i
  i = i + 1
  foo = i
end function
end

Will explode but,

integer :: i, tmp
i = 0
tmp = foo()
tmp = tmp + foo()
print *, tmp
contains
function foo()
  integer :: foo
  print *, i
  i = i + 1
  foo = i
end function
end

Will work just fine. Seeing the latter code (especially if the actual definition of foo is far away) you’d naively think, “why can’t I simplify this and rewrite it like the former?” and then scratch your head for a while when it didn’t work.

1 Like