This question already has answers here:
What are the ?? double question marks in Dart?
(5 answers)
Closed 2 years ago.
I've seen this code and need an explanation for "??".
I know ternary operators like "?" and then the true-condition and after ":" the false/else condition.
But what means the double "??" ?
Thanks in advance
widget.secondaryImageTop ??
(widget.height / 2) - (widget.secondaryImageHeight / 2); ```
List of all dart operators
it's the coalesce operator.
a ?? b
means: if a is not null, it resolves to a. if a is null, it resolves to b.
SQL and a few other languages have this operator.
You example:
widget.secondaryImageTop ??
(widget.height / 2) - (widget.secondaryImageHeight / 2);
This will use widget.secondaryImageTop unless it is null, in which case it will use (widget.height / 2) - (widget.secondaryImageHeight / 2).
Source and detail, including dartpad where you can try things out with pre-populated examples:
https://dart.dev/codelabs/dart-cheatsheet
An example from that documentation, using the = sign as well.
the ??= assignment operator, which assigns a value to a variable only
if that variable is currently null:
int a; // The initial value of a is null.
a ??= 3;
print(a); // <-- Prints 3.
a ??= 5;
print(a); // <-- Still prints 3.
It means => if exists and if not null...
Related
Dart suggest me to use if null operator . Why ? and how to use it?
This is my code:
var name;
name != null ? name : 'nobody';
In other languages we can use the logical-or shortcut. If maybeSomeNumber() returns null, assign a default value of 2:
value = maybeSomeNumber() || 2
In Dart we can't do this because the expression needs to be a boolean (“the operands of the || operator must be assignable to bool”).
That's why the ?? operator exists:
var value = maybeSomeNumber() ?? 2;
Similarly, if we wanted to ensure a value argument was not-null we'd do:
value = value ?? 2;
But there's an even simpler way.
Fallback assignment operator: ??=
value ??= 2;
check the original artical: https://flutterigniter.com/checking-null-aware-operators-dart/
I don't think we need to use conditions or ternary operator here. It will increase program complexity. Try below
name = name || 'nobody';
walrus operator of python language ( := )
work:- assign the value & also return that value.
language like swift at value assign it return nothing.
how to implement walrus operator kind a thing in swift language ?
I think it done by make function, pass address of variable & value.
assign value in that address & return value.
Is this work or any other way for this?
Joakim is correct.
Swift doesn't have a unary operator like C.
In C, you could do:
b = 10;
while (b>0) {
print(b--);
}
In Swift, there isn't a unary ++ or -- operator, so you would do:
var b = 10
while (b > 0) {
print b
b -= 1
}
but, really, in Swift, you'd do this instead
for b in (0...10).reversed() {
print b
}
See Reverse Range in Swift
This question already has an answer here:
Replace c style for-loop in Swift 2.2.1
(1 answer)
Closed 6 years ago.
I wrote the following code in swift(2.2):
for var i = 2; sqrt(Double(num)) >= Double(i); i += 1 {.....}
However, I keep getting a warning message which reads "c-style for statement is deprecated and will be removed in future version of swift".
So, what is the proper way of writing the same loop is "Swift style"? Casting the value of num as double gives me error with "Swift style". Any suggestions?
Thank you.
You can refactor your deprecated C-style for loop using the for-in construct
for i in 2...Int(sqrt(Double(num))) { }
However if you really want to go essential try defining this operator to find the square root of an int rounded down to the closest int.
prefix operator √ {}
prefix func √ (number: Int) -> Int {
return Int(sqrt(Double(number)))
}
Now you can write your for loop this way
for i in 2...(√num) { }
The For loop in Swift 2.2 is re-designed for use in iterating over a sequence, such as ranges of numbers, items in an array, or characters in a string. The condition in your For loop is not easily converted into sequence or range, so it would be best to re-write it as a While loop.
This question already has answers here:
What does this caret ^ syntax, with void on either side mean? [duplicate]
(4 answers)
Closed 9 years ago.
I keep seeing lines of code with ^{ some code } in it... I thought that maybe it allowed to run a function inline similar to a lambda function. But I can not find any documentation on it. Could someone please enlighten me?
It is a block.
See the documentation.
Tis a block!
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW1
To steal Apple's example:
int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
return num * multiplier;
};
printf("%d", myBlock(3));
// prints "21"
Yes, a block indeed...
Here is a tutorial for people who are beginners to blocks!
As Apple states in their documentation:
You use the ^ operator to declare a block variable and to indicate the
beginning of a block literal. The body of the block itself is
contained within {}, as shown in this example (as usual with C, ;
indicates the end of the statement):
int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
return num * multiplier;
};
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Scala operator oddity
I'm very new to Scala and I read that in this language everything is an Object, cool. Also, if a method has only 1 argument, then we can omit the '.' and the parentesis '( )', that's ok.
So, if we take the following Scala example: 3 + 2, '3' and '2' are two Int Objects and '+' is a method, sounds ok. Then 3 + 2 is just the shorthand for 3.+(2), this looks very weird but I still get it.
Now let's compare the following blocks on code on the REPL:
scala> 3 + 2
res0: Int = 5
scala> 3.+(2)
res1: Double = 5.0
What's going on here? Why does the explicit syntax return a Double while the shorthand returns an Int??
3. is a Double. The lexer gets there first. Try (3).+(2).