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
Related
I am calling a function that expects two arguments. I use the same variable, but at the second argument I set this variable to another thing.
See below:
https://dartpad.dartlang.org/2156442de07f56d90b430bc67f3461ac
void main() {
String s = 'oi';
aa(s, s = 'oi2');
}
void aa(String buf, String buf2){
print('$buf, $buf2');
}
This will print "oi, oi2".
I want this to happen. I am using a modified notification within properties, like:
set title(String n) {
this.modified('title', _title, _title = n);
}
However, I wonder if this can be seen as a bug or it is expected.
thanks, Joe
s is a String which are passed by value, not by reference.
aa(s, s = 'oi2');
evaluates the first parameter s, which is 'oi'
next s = 'oi2' is evaluated, which means s gets 'oi2' assigned
then the result of s = 'oi2' (which is 'oi2') is passed as 2nd parameter.
After aa(s, s = 'oi2'); s has the value oi2.
See also https://gist.github.com/floitschG/b278ada0316dca96e78c1498d15a2bb9
Evaluation order of arguments is left-to-right, so you can rely on the first argument's value being found by evaluation s to "ii", and then second argument's value is the value of the assignment s = 'oi2 - which evaluates to "oi2" (and not, technically, by reading the variable, it just happens that the variable is written to with the same value before the function is called).
It is expected - if any implementation does something else, it's broken.
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)
Anyone tell me why this doesn't work?
GET_TABLE {1=ID}
key = string.format("%q", GET_TABLE[1])
RETURN_TABLE[key] = "ss"
print(RETURN_TABLE[ID])
print(GET_TABLE[1])
First print result: nil. Second print result: ID
I want the first print result to be: ss
GET_TABLE {1=ID}
key = "ID"
RETURN_TABLE[key] = "ss"
print(RETURN_TABLE[ID])
print(GET_TABLE[1])
The above works fine so I assume its due to the string.format not working right?
The %q format token returns the input as an escaped and quoted Lua string. This means that given the input ID it will return "ID" (the double quotes being part of the string!) which is a different string. (Or, represented as Lua strings, the input is 'ID' and the return value is '"ID"'.)
You have therefore set the ID key while trying to retrieve the "ID" key (which presumably does not exist).
> x = 'ID'
> =x
ID
> =string.format('%q', x)
"ID"
> =#x
2
> =#string.format('%q', x)
4
Your code does not compile (you need [] around the index), and you should use the raw string of ID, not the "quoted" string:
GET_TABLE = {[1]=ID}
key = string.format("%s", GET_TABLE[1])
Note that I had to initialize ID and RETURN_TABLE objects to the following:
ID = 'ID'
RETURN_TABLE = {}
Stylistic note: you should only use all-caps names for constants, otherwise too many makes code hard to read
I'm really brand new to Groovy and I'm trying to get something done. I've written some Groovy code (which works just fine) which receives some text. This text should be an integer (between 0 and 10). It may just happen a user enters something different. In that case I want to do some specific error handling.
Now I'm wondering, what's the best / grooviest way to test if a string-typed variable can be casted to an integer?
(what I want to do is either consume the integer in the string or set the outcome of my calculation to 0.
Thanks!
The String class has a isInteger() method you could use:
def toInteger (String input) {
if (input?.isInteger()) {
return input.toInteger()
}
return 0
}
use groovy contains
if ( x?.isInteger()) {
return (0..10).contains(x)
} else {
return false
}
Is this what you're saying?
Integer integer = 0
try {
integer = (Integer) string
assert integer > 0
assert integer < 10
catch(e) {
integer = 0
}
There are lots of ways this can be done in groovy, if you're comfortable with regular expressions, this is about as concise as you can get:
def processText(String text) {
text ==~ /(10|\d)/ ? text.toInteger() : 0
}
assert 0 == processText("-1")
(0..10).each {
assert it == processText("$it")
}
assert 0 == processText("11")
I'm a little unsure what you mean by "specific error handling" if the user does something different.
If this is a web application, I'd take a look at grails and the constraints that you can put on the fields of a domain object, that would let you easily express what you're trying to do.
You have the grails tag on your question, so if you are using Grails, you might consider making this an Integer property on a domain class. The param may come in as text, but you can bind it to an integer property with a default value of 0:
class MyDomain {
Integer whatever = 0
static constraints = {
whatever( min:0, max:10)
}
}
Here's the code:
string name = "myName";
int id = (int)_myDB.ThingTable.Where(thing => thing.ThingName == name)
.Select(thing => thing.ThingId);
I have an error saying System.Linq.IQueryable cannot be converted to int (I'm assuming it's so that I don't end up with a case where no rows are found- no id is returned)
First, how can I cast it to an int?
Second, what gets returned if no record with a ThingName == name exists?
Thanks,
Matt
You need a query expression that returns a scalar. Something like:
myCollection.Where(c => c.X > 0).FirstOrDefault();
In your example it would be:
int id = (int)_myDB.ThingTable.Where(thing => thing.ThingName == name)
.Select(thing => thing.ThingId).FirstOrDefault();
If no row is returned, the default value of the scalar is returned (generally zero in the case of a non-nullable number).
Try using FirstOrDefault() on queries such as that. It will return the default value for the selection if nothing is returned. 0 for numbers, null for objects.