why do wave assignments for zero-length wave sometimes give indexing error?

I was surprised to discover that functions in a wave assignment for a zero-length wave are executed with a NaN or null string. Is there a reason this should be so?

From the command line, a wave assignment for a zero-length wave always generates an indexing error.

Within a function, an assignment like wave0=1 doesn't generate an error, but wave0=abs(wave0) generates an 'index out of range' error. Seems to me that it's Igor's choice to look for an out of range value when he doesn't have to? If I think of the wave assignment as an implicit loop, it's like writing for(i=0;i<0;i+=1) and finding that I enter the loop with i=NaN and generate a run-time error.

If I write wave0=f(wave0) for zero-length numeric waves in a function I get an out-of-range error, but for text waves a null string is passed to the string function f without an out-of-range error, so if my function checks for null strings I can get away with it. That seems inconsistent.

This implies that if my code has a possibility of redimensioning a wave I must check for non-zero wave length before an assignment statement, or for a text wave consider the possibility of a string function executing with a null string.

Thoughts?

[I posted this to the old IgorExchange site, but both my post and Thomas Braun's followup to ask which Igor version I'm using were lost in the transition. The old site resurfaced and I noticed my post still sitting there.]

Igor 8, mac

Well I'm all on your side!

I've poured your post into code. This uses my unit-testing-framework.

 

#include "unit-testing"

Function TestWritingIntoEmtpyNumericWave1()
    variable err
    Make/N=0 wv

    try
        wv[] = 1; AbortOnRTE
        CHECK(0)
    catch
        err = GetRTError(1)
        PASS()
    endtry
End

Function TestWritingIntoEmtpyNumericWave2()
    variable err
    Make/N=0 wv

    try
        wv[] = abs(wv); AbortOnRTE
        CHECK(0)
    catch
        err = GetRTError(1)
        PASS()
    endtry
End

Function TestWritingIntoEmtpyNumericWave3()
    variable err
    Make/N=0 wv

    try
        wv[] = abs(-1.0); AbortOnRTE
        CHECK(0)
    catch
        err = GetRTError(1)
        PASS()
    endtry
End

// _IGNORE tells the UTF that this is not a test case
Function/S TestFunc_IGNORE(str)
    string str

    CHECK_PROPER_STR(str)
    return "abcd"
End

Function TestWritingIntoEmtpyTextWave1()
    variable err
    Make/N=0/T wv

    try
        wv[] = "abcd"; AbortOnRTE
        CHECK(0)
    catch
        err = GetRTError(1)
        PASS()
    endtry
End

Function TestWritingIntoEmtpyTextWave2()
    variable err
    Make/N=0/T wv

    try
        wv[] = TestFunc_IGNORE(wv[p]); AbortOnRTE
        CHECK(0)
    catch
        err = GetRTError(1)
        PASS()
    endtry
End

 

I would expect that this generates no errors.

To replicate:

- Install UTF

- Put code into main procedure window

- Save Experiment and execute `RunTest("Procedure")`

•runtest("procedure", allowDebug=0)
  Start of test "Unnamed"
  Entering test suite "Procedure"
  Entering test case "TestWritingIntoEmtpyNumericWave1"
  0: is false
  Assertion "CHECK(0)" failed in line 12, procedure "Procedure"
  Leaving test case "TestWritingIntoEmtpyNumericWave1"
  Entering test case "TestWritingIntoEmtpyNumericWave2"
  Leaving test case "TestWritingIntoEmtpyNumericWave2"
  Entering test case "TestWritingIntoEmtpyNumericWave3"
  0: is false
  Assertion "CHECK(0)" failed in line 38, procedure "Procedure"
  Leaving test case "TestWritingIntoEmtpyNumericWave3"
  Entering test case "TestWritingIntoEmtpyTextWave1"
  0: is false
  Assertion "CHECK(0)" failed in line 59, procedure "Procedure"
  Leaving test case "TestWritingIntoEmtpyTextWave1"
  Entering test case "TestWritingIntoEmtpyTextWave2"
  Assumption that the string is a proper string is: is false
  Assertion "CHECK_PROPER_STR(str)" failed in line 49, procedure "Procedure"
  Leaving test case "TestWritingIntoEmtpyTextWave2"
  Failed with 4 errors
  Leaving test suite "Procedure"
  Test finished with 4 errors
  End of test "Unnamed"