Is it possible to do something like the following? aka, initialize a variable inside an if statement condition?
Reasoning:
I have a network call that will fetch data, and I'd like to avoid the following options:
Calling it unless the first condition is false.
Calling it twice, once to fetch the data to check the conditional & once to use the data inside the condition
Having to nest if statements
So initializing the variable inside the conditional block seems like the cleanest solution.
if (condition1) {
// Do something
} else if ( (String foo = await getBar()) == "not bar"){
// Do something with foo
} else {
// Fallback condition
}
One option would be to declare the variable before the condition like so:
String foo; // Declare variable here
if (condition1) {
// Do something
} else if ((foo = await getBar()) == "not bar") {
// Do something with foo
} else {
// Fallback condition
}
Related
I noticed that forEach and for in to produce different behavior. I have a list of RegExp and want to run hasMatch on each one. When iterating through the list using forEach, hasMatch never returns true. However, if I use for in, hasMatch returns true.
Sample code:
class Foo {
final str = "Hello";
final regexes = [new RegExp(r"(\w+)")];
String a() {
regexes.forEach((RegExp reg) {
if (reg.hasMatch(str)) {
return 'match';
}
});
return 'no match';
}
String b() {
for (RegExp reg in regexes) {
if (reg.hasMatch(str)) {
return 'match';
}
}
return 'no match';
}
}
void main() {
Foo foo = new Foo();
print(foo.a()); // prints "no match"
print(foo.b()); // prints "match"
}
(DartPad with the above sample code)
The only difference between the methods a and b is that a uses forEach and b uses for in, yet they produce different results. Why is this?
Although there is a prefer_foreach lint, that recommendation is specifically for cases where you can use it with a tear-off (a reference to an existing function). Effective Dart recommends against using Iterable.forEach with anything else, and there is a corresponding avoid_function_literals_in_foreach_calls lint to enforce it.
Except for those simple cases where the callback is a tear-off, Iterable.forEach is not any simpler than using a basic and more general for loop. There are more pitfalls using Iterable.forEach, and this is one of them.
Iterable.forEach is a function that takes a callback as an argument. Iterable.forEach is not a control structure, and the callback is an ordinary function. You therefore cannot use break to stop iterating early or use continue to skip to the next iteration.
A return statement in the callback returns from the callback, and the return value is ignored. The caller of Iterable.forEach will never receive the returned value and will never have an opportunity to propagate it. For example, in:
bool f(List<int> list) {
for (var i in list) {
if (i == 42) {
return true;
}
}
return false;
}
the return true statement returns from the function f and stops iteration. In contrast, with forEach:
bool g(List<int> list) {
list.forEach((i) {
if (i == 42) {
return true;
}
});
return false;
}
the return true statement returns from only the callback. The function g will not return until it completes all iterations and reaches the return false statement at the end. This perhaps is clearer as:
bool callback(int i) {
if (i == 42) {
return true;
}
}
bool g(List<int> list) {
list.forEach(callback);
return false;
}
which makes it more obvious that:
There is no way for callback to cause g to return true.
callback does not return a value along all paths.
(That's the problem you encountered.)
Iterable.forEach must not be used with asynchronous callbacks. Because any value returned by the callback is ignored, asynchronous callbacks can never be waited upon.
I should also point out that if you enable Dart's new null-safety features, which enable stricter type-checking, your forEach code will generate an error because it returns a value in a callback that is expected to have a void return value.
A notable case where Iterable.forEach can be simpler than a regular for loop is if the object you're iterating over might be null:
List<int>? nullableList;
nullableList?.forEach((e) => ...);
whereas a regular for loop would require an additional if check or doing:
List<int>? nullableList;
for (var e in nullableList ?? []) {
...
}
(In JavaScript, for-in has unintuitive pitfalls, so Array.forEach often is recommended instead. Perhaps that's why a lot of people seem to be conditioned to use a .forEach method over a built-in language construct. However, Dart does not share those pitfalls with JavaScript.)
👋 jamesdin! Everything you have shared about the limitations of forEach is correct however there's one part where you are wrong. In the code snippet showing the example of how you the return value from forEach is ignored, you have return true; inside the callback function for forEach which is not allowed as the callback has a return type of void and returning any other value from the callback is not allowed.
Although you have mentioned that returning a value from within the callback will result in an error, I'm just pointing at the code snippet.
Here's the signature for forEach
Also, some more pitfalls of forEach are:
One can't use break or continue statements.
One can't get access to the index of the item as opposed to using the regular for loop
How do I trigger one block of code whenever any of a set of SignalProducers change? In other words, how do I get rid of my current redundant code:
property1.producer.startWithValues { (value) in
// do stuff with property1.value and property2.value
}
property2.producer.startWithValues { (value) in
// do the same stuff with property1.value and property2.value
}
You can use combineLatest to create a new property that contains both values:
let prop = property1.combineLatest(with: property2)
prop.producer.startWithValues { (val1, val2) in
// do stuff here
}
If either value changes, the block will be triggered.
You can save the block of code as a variable, then you would simply assign that variable to property1.producer.startWithValues.
Is there a way to write some sort of catch statement around ([MyArray ObjectAtIndex:myindexpath.row])so that I can run this without throwing an exception?
In other words, I want to write this sort of expression:
if ([MyArray ObjectAtIndex:myindexpath.row]) {
// do some stuff if the object is in the array
else {
// do some other stuff
}
Sure: use logic & maths.
if (index < myArray.count) {
// ...
}
I have two named data sources in my Grails app (Grails 2.0.3)...
dataSource_a {
// ...
}
dataSource_b {
// ...
}
I'd like the ability to dynamically change what datasource I'm accessing, based on some kind of parameter. I could do something like this...
def findPeople(datasource) {
if (datasource == 'a') {
return Person.a.list()
} else if (datasource == 'b') {
return Person.b.list()
}
}
What I was really hoping to be able to do, though, is something like this...
def findPeople(datasource) {
return Person."$datasource".list()
}
Unfortunately, I get an error when I try and do that. "Fatal error occurred apply query transformations: null 1 error".
Any thoughts on how to accomplish this? Or am I just stuck with if/switch blocks?
I figured it out, this is how you have to do it.
def findPeople(datasource) {
def p = People.class
p."${datasource}".list()
}
For some reason, if you call it like that, it works.
I often write something like:
def myAction{ MyActionCommand cmd ->
if( cmd.hasErrors() ){
return render(status:HttpServletResponse.SC_BAD_REQUEST );
}else{
// actual action logic
}
So, I'd like to extract that common pattern into some reusable location. Filter looks like good candidate, but I can't find the way to get command object from the filter. Tryed something like this (in filters closure):
formValidation( controller:'*', action:'*' ){
before = { cmd ->
if( cmd.hasErrors() ){
response.sendError( HttpServletResponse.SC_BAD_REQUEST );
return false;
}else{
return true;
}
}
}
Intersted in grails 1.3.7 compatible solution. Is it possible at all?
No, it isn't possible to do what you are asking. Command Objects are not full framework artifacts like Controller, Service, etc, and so they do not get their validation logic added to them, unless they are a parameter to a Controller action. To that end a Command Object in a filter wouldn't have a .validate() or .hasErrors() method to check against.
As another option you could use the #Validateable annotation:
http://grails.org/doc/latest/guide/7.%20Validation.html#7.5%20Validation%20Non%20Domain%20and%20Command%20Object%20Classes
Move your Command Object to src/groovy as a regular Groovy class and annotate it with #Validateable. Then in your filter you can do:
def validObj = new MyValidateable(params)
if (!validObj.validate()) {
response.sendError( HttpServletResponse.SC_BAD_REQUEST );
return false;
} else {
return true;
}
Make sure you add the package name of your validateable class to the grails.validateable.packages List in Config.groovy.
What about creating a service like this:
class AutoValidateService {
def onValid(def cmd, Closure onValid) {
if( cmd.hasErrors() ){
return render(status:HttpServletResponse.SC_BAD_REQUEST );
}else{
onValid()
}
}
}
The use it like so:
class FooController {
AutoValidateService autoValidateService
def myAction{ MyActionCommand cmd ->
autoValidateService.onValid(cmd) {
// do something
}
}
}