Problems with assert in function definition #109

Closed
opened this Issue Aug 10, 2016 · 1 comment

Projects
None yet
1 participant

Alexander-Schiendorfer commented Aug 10, 2016

 Hi, I would like to add some assertions to a function that returns "array[int,int] of int" (in reality, an adjacency matrix). ``````function array[int, int] of int: doSomething(array[int, int] of int: edges) = assert(true, "This should work", edges); output[ "\(doSomething([| 5, 3, 5 | 1, 2, 3 |]))" ]; solve satisfy; `````` which does not work and returns: ``````MiniZinc: type error: no function or predicate with this signature found: `assert(bool,string,array[int,int] of int)' `````` However, when doing the same with just an int, everything's fine: ``````function int: doSomething(int: e) = assert(true, "This should work", e); output[ "\(doSomething(5))" ]; solve satisfy; `````` Is there a workaround? Cheers, Alex

Alexander-Schiendorfer commented Aug 10, 2016

 For anyone running into a similar issue, here is a (not really pretty) workaround that gets the job done: ``````function bool: preassert(bool: expr, string: message) = assert(expr, message, expr); function array[int, int] of int: doSomething(array[int, int] of int: edges) = let { bool: cons = preassert(false, "Should be consistent") } in if cons then edges else [| |] endif; output[ "\(doSomething([| 5, 3, 5 | 1, 2, 3 |]))" ]; solve satisfy; ``````