Structure not created but no error reported

Hi folks
I’m not able to get the following to yield the structure for the instance Tst described in the REPL further below. No error however is reported by Julia.

mutable struct CvDv_ForAll_Voids
    CvDv_Trapz     ::         Array{Float64,2}
    CvDv_Trian     ::         Array{Float64,2}

    function CvDv_ForAll_Voids(CvDv_Trapz, CvDv_Trian, NTrapz, NTrian)
        Case1     =   (NTrapz != 0) & (NTrian != 0)
        Case2     =   (NTrapz != 0) & (NTrian == 0)
        Case3     =   (NTrapz == 0) & (NTrian != 0)
        Case4     =   (NTrapz == 0) & (NTrian == 0)
        if Case1==1 new(CvDv_Trapz,CvDv_Trian); end
        if Case2==1 new(CvDv_Trapz,zeros(1,2)); end
        if Case3==1 new(zeros(1,2),CvDv_Trian); end
        if Case4==1 new(zeros(1,2),zeros(1,2)); end

Inside the REPL, this is what I created and asked in order to form a strucure called Tst:

julia> A :: Array{Float64,2} = fill(0.75, (4,2))
4×2 Matrix{Float64}:
 0.75  0.75
 0.75  0.75
 0.75  0.75
 0.75  0.75

julia> B :: Array{Float64,2} = fill(0.15, (3,2))
3×2 Matrix{Float64}:
 0.15  0.15
 0.15  0.15
 0.15  0.15

julia> Tst = CvDv_ForAll_Voids(A,B,1,1)

julia> Tst

julia> typeof(Tst)

I ran the above using the Debugger on VS Code and did see that Case1 was true while Case2, Case3 and Case 4 were all false. So still why didn’t the inner constructor function return Nothing rather than create the structure fields using the contents of A and B?

Thanks in advance.

The first conditional executes and successfully constructs a new object and returns it.

But then the remaining three conditionals execute, and they all return nothing.

Most importantly, the final conditional returns nothing, and that is then the return value of your inner constructor.

Consider either:

a) using return statements
b) using mutually exclusive conditions with elseif

Note that you don’t need to compare true against true; you can simply test if Case1. Also, for boolean comparisons it is usually more convenient to use &&, which has lower precedence so the equality comparisons don’t need to be parenthesized.

1 Like

The conditionals for Case2, Case3 and Case4 should NOT execute because they’re false and NOT satisfied to create the new(…) under them.

Isn’t this how it’s supposed to be ?

Sorry, I misspoke.

The remaining three conditionals evaluate their conditions, which are false. When that occurs, they do not evaluate the code in their body, and return nothing.

The point stands though, that you either need to use return to terminate the function early, or use mutually exclusive conditionals. Otherwise the last conditional’s return value of nothing becomes the function’s return value.


Thanks, @uniment

I’ll try preceding the new(…) with return and update asap. Also noted your other suggestions.