I am using following line of code which always fails, because the compiler is trying to execute TSOURCE although MASK==.FALSE.. This behaviours only occurse when the TSOURCE has some kind of modification. In this case it is SIZE. So he is trying to get the SIZE of LC and then he is checking MASK if TSOURCE or FSOURCE should be used. Is this behaviour intended? Reminds me of the non short circuting behaviour in IF statements.
CLASS(myClass), INTENT(IN) :: this INTEGER, INTENT(IN), OPTIONAL :: LC(:) INTEGER :: nLC nLC = MERGE(SIZE(LC), this%nLoadcases, PRESENT(LC))
This speculation is typical of compiler optimization levels which permit vectorization of conditional operations. If you don't wish this behavior, you can try compile options such as /fp:strict (look up implied levels of speculation). You may have the option of a novector directive, or placing an explicit IF block. You might think that the equivalent of IF block could actually be an optimization when the mask is invariant, so you could try submitting a request to online service center.
The code is non-conforming if LC is ever not present. MERGE is a function - when a function is invoked, all actual argument expressions are evaluated, then the function is executed. If LC is not present, then SIZE(LC) cannot be evaluated.
To control the execution order, use an if statement.
IF (PRESENT(LC)) THEN nLC = SIZE(LC) ELSE nLC = this%nLoadCases END IF
Thanks for the clarification.
... when a function is invoked, all actual argument expressions are evaluated, then the function is executed.
Thats what I thought. Still I like to use MERGE in similar situations because of simplicity of a one line statement.
"The programmer doth protest too much, methinks"
Hamlet, by William Shakespeare
IOW you wish to attain a preferable form over function by violating one of the language rules.
"I did it my way"
My Way by Frank Sinatra
Jim Dempsey :)