I am using Grails 2.4.3
I have a property named value of type BigDecimal which is defined like this:
#BindUsing({ obj, source ->
if (source['value']) {
source['value']?.toBigDecimal()
} else {
null
}
})
BigDecimal value
Now if I use this value in a constraint, I get the following strange behaviour:
if (obj.value)
evaluates to false if value is set to 0.0
It evaluates to true for any other value (e.g. 0.1)
0.0 is actually stored as 0.0 in the database table, but the validator obviously thinks it is null.
Or is there maybe a problem using "value" as the name for a property?
Actually the issue is how Groovy evaluates truth. The documentation has the following example:
assert !0 //yeah, 0s are false, like in Perl
assert 1 //this is also true for all other number types
Numbers, 0.0 in your case, evaluate to false when they are zero. If you are trying to check for null values, then you should be checking for null values in this case.
if (obj.value == null)
Related
I want to return the count of users that that have a boolean value, show, not set to true. show is a boolean but is initialized to nil.
Because of this I'm getting some results that I did not expected. For example,
[{show: nil}].where.not(show: true).count -> 0
[{show: nil}].where(show: [nil, false]).count -> 1
Shouldn't this query's return the same thing?
This is a general misunderstanding of NULL (which is not uncommon by the way).
NULL is not comparable, it is neither true or false, nor equal or not equal, nor less than or greater than, etc., NULL cannot even be compared to NULL. e.g. NULL = NULL is false but NULL != NULL is also false.
Your First case (.where.not(show: true)) is show != true (or NOT(show =true)) which is the equivalent of show = false.
Your second example (.where(show: [nil, false])) is show IS NULL OR show = false. While this syntax show: [x,y] usually results in an IN() clause Arel is smart enough to realize that one of these values is nil and converts the SQL generation accordingly because show IN (NULL,false) would be equivalent to show = NULL OR show = false (this is how IN() works) but as mentioned NULL = NULL is false thus why IS NULL and IS NOT NULL exist.
The result of your query is because there are no rows where show = false but there is one row where show IS NULL.
So I have a class Cache which has a method compare that returns a bool.
I have an instance of this class that is nullable.
Cache? recent;
I want to executed a piece of code when recent is not null and compare returns false
Without null safety I would have just done
if(recent!=null && !recent.compare()){
//code
}
How to do the same with null safety enabled?
When I try above with null safety I get
The method 'compare' can't be unconditionally invoked because the receiver can be 'null'.
Try making the call conditional (using '?.') or adding a null check to the target ('!')
When I try below
if(!recent?.compare()){
//code
}
It gives me
A nullable expression can't be used as a condition.
Try checking that the value isn't 'null' before using it as a condition.
You can solve this problem either by
Using local variable (recommended)
var r = recent; // Local variable
if (r != null && !r.compare()) {
// ...
}
Using Bang operator (!)
if (!recent!.compare()) {
// ...
}
When testing code with both a predefined script and the LUA runtime environment, LUA will not take any form of string key values. However, if a numerical value key is used LUA will work with it as intended. The exception to this rule when I am using Tshark with a LUA file to parse packet captures. This allows the string key value syntax to work normally. Is there something I may be performing wrong?
I have tried creating several .lua script files with different variations including:
testArray.NewItem = "value1"
testArray["NewItem"] = "value1"
NewItemValue = "NewItem"
testArray[NewItemValue] = "value1"
These all result in an nil value or an error due to trying to call a nil value depending on the return style used to check.
> tcpstream = {}
> stream1 = tostring(14356)
> tcpstream[stream1] = "nothing"
> print(#tcpstream)
0
> print(tcpstream[1])
nil
> tcpstream[1] = "nothing"
> print(#tcpstream)
1
> print(tcpstream[1])
nothing
the output of the print(#tcpstream) after the tcpstream[stream1] = "nothing" should show 1 not zero. The subsequent print(tcpstream[1]) should also show "nothing".
From http://lua-users.org/wiki/TablesTutorial
The # operator doesn't count all the items in the table (!). Instead it finds the last integer (non-fractional number) key. Because of how it's implemented its results are undefined if all the integer keys in the table aren't consecutive. Which is why it shouldn't be used for tables used as sparse arrays[2]).
The '#' is not a good(sometimes not correct) way to count the number of elements in Lua table.
As for
> stream1 = tostring(14356)
> tcpstream[stream1] = "nothing"
> print(#tcpstream)
0
> print(tcpstream[1])
nil
Lua uses key,value pairs, not explicitly index. If you do 'arr[1] = 22', it means the value for the key '1' is 22, not the value for the first element is 22.
The length operator(#) does not work as you believe, this is a common mistake for beginners in Lua.
The default behavior for #sometable is to return the number of consecutive key starting at the number 1(or after any nil value for 5.3). String keys are never evaluated with the default # operator for a table.
In 5.3 if your sequence contains multiple nil values the behavior of # is non-deterministic.
Lua 5.3 Reference Manual: 3.4.7 – The Length Operator
Lua 5.1 Reference Manual: 2.5.5 – The Length Operator
I will include the lines from 5.1 as i feel it covers the information regarding the operator and tables well. While note identical to how 5.3 work it maybe easier to understand why you see the behavior you do.
2.5.5 – The Length Operator
The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte).
The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t1 is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).
Here are some examples of different table and their # results:
table1 = { --number keys in array
true,
true,
true,
}
table2 = { -- number keys in hash
[1] = true,
[2] = true,
[3] = true,
}
table3 = { -- only strings as key
['1'] = true,
['2'] = true,
['3'] = true,
}
table4 = { -- No key 2 defined
[1] = true,
-- [2] = true,
[3] = true,
}
table5 = { -- table with both string and number keys
[1] = true,
['2'] = true,
}
print(#table1) -- 3
print(#table2) -- 3
print(#table3) -- 0
print(#table4) -- v5.3(1 or 3) v5.1(1)
print(#table5) -- 1
When I try assigned value from params it now works incorrect.
System.out.println(params.test) // I see 0
int test = params.test
System.out.println(test) // I see 48
some my integer and float variables assigned value + 48
some boolean valiable all assigned true
I tried change grails version (2.3.7/2.3.4), rebuild project. But it works wrong.
May be I changed some settings?
Nothing is wrong here.
params.test has string value 0
params.test = "0"
when it is type casted to int it's ascii value 48 gets assigned to test
assert 48 == (int)"0"
To get int value of string "0" from params you can use primitive methods applicable on params
params.int('test') // int 0
similarly for boolean
params.boolean('test') //boolean true/false
Does Dart support == and === ? What is the difference between equality and identity?
Dart supports == for equality and identical(a, b) for identity. Dart no longer supports the === syntax.
Use == for equality when you want to check if two objects are "equal". You can implement the == method in your class to define what equality means. For example:
class Person {
String ssn;
String name;
Person(this.ssn, this.name);
// Define that two persons are equal if their SSNs are equal
bool operator ==(other) {
return (other is Person && other.ssn == ssn);
}
}
main() {
var bob = Person('111', 'Bob');
var robert = Person('111', 'Robert');
print(bob == robert); // true
print(identical(bob, robert)); // false, because these are two different instances
}
Note that the semantics of a == b are:
If either a or b are null, return identical(a, b)
Otherwise, return a.==(b)
Use identical(a, b) to check if two variables reference the same instance. The identical function is a top-level function found in dart:core.
It should be noted that the use of the identical function in dart has some caveats as mentioned by this github issue comment:
The specification has been updated to treat identical between doubles
like this:
The identical() function is the predefined dart function that returns
true iff its two arguments are either:
The same object.
Of type int and have the same numeric value.
Of type double, are not NaNs, and have the same numeric value.
What this entails is that even though everything in dart is an object, and f and g are different objects, the following prints true.
int f = 99;
int g = 99;
print(identical(f, g));
because ints are identical by their value, not reference.
So to answer your question, == is used to identify if two objects have the same value, but the identical is used to test for referential equality except in the case of double and int as identified by the excerpt above.
See: equality-and-relational-operators
As DART is said to be related to javascript, where the === exists, I wish not be downvoted very quickly.
Identity as a concept means that 1 equals 1, but 1.0 doesn't equal 1, nor does false equal 0, nor does "2" equal 2, even though each one evaluates to each other and 1==1.0 returns true.
It should be noted that in Dart, identical works similarly to Javascript, where (5.0 == 5) is true, but identical(5.0, 5) is false.