Weird switch issue
I'm using the MacOS app, so if I put this in a formula field:
switch Number do
case Number < 5:
10
case Number > 5:
20
end
with Number being a number field in the same table.
When I type in 0 I get 20. When I type in 1 I get 10, When I type in anything larger than 1 it goes blank.
I don't know if this happens in the cloud version or not. I sent an email to Ninox and they confirmed that it is an issue.
Ninox gave the following recommendation to make something like this to work:
switch true do
case Number < 5:
10
case Number > 5:
20
end
And that worked on my computer.
So to test it futher, I did something like:
switch true do
case Number < 5:
10
case Number > 5 and Number < 8:
20
case Number > 8 and Number < 12:
30
default:
0
end
And that works as well. Not intuitive, but if you need to get it something like this work.
5 replies
-
Thanks a lot for sharing Fred, I was looking for a way to do this yesterday!
I hadn't thought of putting a boolean value in the Switch, that's just great. Did you find this in the documentation?
To understand how it works, I went to see how Ninox transforms the script and JavaScript. The result is simple on the principle, it's a series of if...then...else with nested functions, but a bit complex to read and understand :
function anonymous(ctx, db, n0, params, cb) { (function(cb) { (function(cb) { cb(true) })(function(v1) { (function(cb) { cb((n0 && n0.C) < 5) })(function(v2) { if (v1 == v2) { (function(cb) { cb(10) })(cb) } else { (function(cb) { cb((n0 && n0.C) > 5) })(function(v2) { if (v1 == v2) { (function(cb) { cb(20) })(cb) } else { cb(undefined) } }) } }) }) })(cb); }
The important thing to remember is that Ninox sets the value passed to Switch in V1. In our case, True. Then it compares each Case (V2) with this value "if V1=V2 then...". If the Case is a calculation that returns a boolean (Number > 5), then it is like writing "if true = (Number > 5) then...".
-
And it allows you to do more complex Switch Cases such as testing whether Number is part of a given set of numbers:
var n := Number; switch true do case [1, 3][= n] > 0: "option A" case [2, 4, 5][= n] > 0: "option B" default: "erreur" end
In this example: "case [1, 3][= n] > 0" :
- "[1, 3]" is an array of two values.
- "[= n]" searches n in this array.
- All that remains is to test that the result is greater than zero ">0". -
Looks like a bug to me. You should have to do coding gymnastics, or read source code, or whatever, to work around the internal Ninox implementation.
Content aside
-
3
Likes
- 2 yrs agoLast active
- 5Replies
- 764Views
-
4
Following