I'm unable to use assertion

An AssertionError raises in my code, when i want to assert count(!iszero,[1 3;2 2])to be equal to zero: @assert count(!iszero,[1 3;2 2])==0 but, everytime i do that the error occurs.
Perhaps i can do:

a= count(!iszero,[1 3;2 2])
a=0
a==0

it will return True, but i don’t know if it’s the proper way to do that!!!

Not sure what you are expecting, since

julia> count(!iszero,[1 3;2 2])
4

So basically you are doing

@assert 4 == 0

which will of course raise an error.

1 Like

i want that first output because it will be used in other lines of the code, and after that i want to make that count(!iszero,[1 3;2 2])==0 it’s like an initialization

I have to admit that I don’t understand what you are trying to do here.

Are you sure you want assertions? If you are using the result of count, just save it in a variable.

I have the feeling you don’t understand what @assert does: it basically raises an error if the expression is not true. So it is not “asserting” that it “will be magically true” afterwards.

…but correct me if I am wrong :wink:

I think that marouane1994 wants the code

count(!iszero,[1 3;2 2])

to return the value of zero everytime and assign the value of zero to another variable

I think he wants

function myassert(expr,value)
    idontcare = expr
    return idontcare == value ? idontcare : value
end

a = myassert(  count(!iszero,[1 3;2 2]), 0 )

I think a smart compiler can optimise this away to

myassert(expr,value) = value
1 Like