Impossible to use returned value from function in if-statement?

Tarik Barri's icon

I must be overlooking something insanely obvious, but I can't figure out what.
This gen code:

getOne() { return 1.0; }
getOneIndirect() { return getOne(); }
if (getOneIndirect() == 1.0) {
out1 = 1.0;
}

...fails to compile, and gives me the error message:gen_domain: dsp.gen: [string "gen2.dsp.Model"]:1687: attempt to index local 'instance' (a nil value)

(EDIT - this refers to the 'gen' object, not the 'jit.gen' object)
I've reproduced on several max versions and two computers, but can not find this issue mentioned anywhere online.

Any hints as to what's going on would be greatly appreciated
-t

soundyi's icon

Hi Tarik,

I just added an else block and it works fine.

cheers
soundyi

Tryout.maxpat
Max Patch
else block added

Tarik Barri's icon

I see, it's the "jit." that made it compile, not the 'else' statement.

The moment I remove the "jit." prefix and it becomes a regular 'gen' object, it stops compiling. So without wanting to jump to conclusions, I am tempted to go with the hypothesis that 'gen' is basically broken for now?

Tarik Barri's icon

gen~ (with the tilde) seems to be slightly more robust than gen, but also this fails to compile when I get a bit deeper into conditionals:

genbug.maxpat
Max Patch

Tarik Barri's icon

and here's another amazing bug. Once this empty for loop is present, the output simply stops functioning (in both gen and gen~), so that this two line code doesn't work.

for(index=0; index < 1; index+=1) {}
out1 = 1.;

genbug2.maxpat
Max Patch


I can't possibly be the first one to run into all this weirdness, so maybe someone who's gone through this can tell me how they deal with these issues?

Federico Foderaro's icon

Hi Tarik!

I copy pasted your code into a codebox in jit.gen and it gives me exactly the output you would expect, without compilation errors.

In "gen" instead it gives me the error you mentioned.

Sounds like a bug, I will report it

👽'tW∆s ∆lienz👽's icon

Hey Tarik, so great to see this discussion here, my 2cents:
They do indeed need to work on making the max-window error messages from gen~/gen/jit.gen a bit more useful/helpful to us 🙏

seems like your code should work, i even tried adding a superfluous argument to the functions like the 'p' in the functions for Gregory's article here, as well as tried saving to a .genexpr file and instantiating using 'require'(thinking maybe it might compile differently when in an extraneous .genexpr file), still didn't work, even though Gregory's functions seem to have the same basic structure:
https://cycling74.com/tutorials/gen~-for-beginners-part-7-creating-reusable-tools

(also tried all those steps in both gen and gen~, couldn't get it to work)

i do know that 'gen'(without the tilde) handles scheduling and memory differently('gen' needs to be triggered from outside, or have internal metro on using '@active 1'... also cannot use things like 'buffer' but still can use 'data' instead), so maybe better off trying things in gen~ first.

i dunno... i tried, hopefully it at least helps whittle out some testing(maybe also posting Gregory's article can help you find some thread of similarity to follow).

Tarik Barri's icon

Thanks both!!

I've now found that if I take the functions of which I want to test their outputs in an 'if' statement, and store the results of these functions in a 'History', I can use these functions. So:

getOne() { return 1.0; }
getOneIndirect() { return getOne(); }

History enableFunction(0);
enableFunction = getOneIndirect(); 
    
if (getOneIndirect() == 1.0) {
  out1 = 1.0;
}

...and if I want to do this with functions which take variable arguments, I need to use these same variable arguments when storing the results in the 'History', otherwise it will still fail.

The thing that most confuses me about this all though is not the bug itself, but the fact that it seems as if I'm the first one to ever try using returned function values in an 'if' statement. Which surely can't be true. I'm not that totally unique and special, or...?

soundyi's icon

Hi Tarik,

the reason why your genbug2 patch creates silence is due to the code generated from your GenExpr code inside the Codebox - whereby its not yours code fault.

If you open the "code" sidebar of the gen (gen~, jit.gen, jit.gl.pix) patcher - the stylish C in the right hand toolbar - you can see that the "code generator" has set out1 & out2 to 0 ... hence silencio ;-).

I also currently experience "gen code generation" weirdness, where an else block is just "optimised away", but that happens in the jit.gen.

But I guess, wheter audio or video domain, there is only 1 code generator that not only translates the "normal" gen operator patches, but also the GenExpr Code we type into Codebox into an intermediate GenExpr Code that its visible in the "code sidebar".

So if you experience weird stuff, the code sidebar is the place where you might detect THAT something weired / wrong was generated from your code.

generated intermediate code reveals silence

Tarik Barri's icon

Yes actually I noticed this too and this can help indeed. Not with the other errors unfortunately. But this 'history' hack I described above seems to make things work... for now...

Shared Houses's icon

Buffers in gen(without the tilde) seem to be working fine on my end

gen_writebuffer.maxpat
Max Patch