How can I fix my code under this text?
//puncts = puncts ?? new List<Vector2>() { new Vector2(position.X, position.Y) };
if (Vector2.Distance(position, puncts[indexpunkt] = puncts[indexpunkt] ?? new Vector2(position.X, position.Y) ) < 1)
indexpunkt++;
Error:
Error 1 Operator '??' cannot be applied to operands of type 'Microsoft.Xna.Framework.Vector2' and 'Microsoft.Xna.Framework.Vector2'
I wish create new puncts if it is null and add first element to its list.
Can I use operator ?? and how can I use it in if statement?
Vector2 is a Struct and therefore cannot be null, so coalescing operators don't apply.
Related
The code Dart is complaining about:
Map<String,int> get_frequency(String text) {
Map<String,int> retval = {};
for (int i = 0; i<text.length; ++i) {
retval[text[i]] = retval[text[i]] ?? 0;
retval[text[i]]++; //<---- this is where dart is complaining.*
}
return retval;
}
void main() {
const paragraphOfText = 'Once upon a time there was a Dart programmer who '
'had a challenging challenge to solve. Though the challenge was great, '
'a solution did come. The end.';
var data = get_frequency(paragraphOfText);
print(data);
}
Obviously the line marked with (*) can not be null, so how do I tell that to Dart? I tried the null assertion operator (!), but that didn't work.
Null Safety is enabled.
Error message:
challenge2.dart:5:20: Error: Operator '+' cannot be called on 'int?' because it is potentially null.
retval[text[i]]++;
^
challenge2.dart:5:20: Error: A value of type 'num' can't be assigned to a variable of type 'int'.
retval[text[i]]++;
^
A possible solution is to change the line
retval[text[i]]++; //<---- this is where dart is complaining.*
into
retval[text[i]] = retval[text[i]]! + 1;
Just figured it out.
Does Dart support a syntax to have an inline if without an else case? I sometimes find myself in a situation to create a flutter layout where this might be really helpful
new Row(children: <Widget>[
new Text(item.name),
item.name2 != null ? new Text(item.name2) : new Container(),
]
In this example the empty container is unnecessary so I was hoping for something like this:
new Row(children: <Widget>[
new Text(item.name),
item.name2 != null ? new Text(item.name2),
]
there is no inline if without else
but in your case you're using if only to check only if it's null or not
dart have :
x = someVar ?? 0
here dart checks if someVar == null ? if true sets x value to 0, if false sets it to someVar value
but flutter will never let you add null to it's widget tree, so your can't use it in row/col
You can do this:
myVar == null ? 0 : myVar
If myVar is null then is 0, else then myVar
If you need to call a function you can use like this:
String s; //null
s == null ? print("if shorthand working") : {};
For variable assignment:
String c = "not null value";
String b; //null
var a = b ?? c;
Now a = c.
Visual Studio Code tip: add this to ignore linting for a line
// ignore: unnecessary_statements
In general, the "inline if" can be written in darts as follows:
<condition> ? <expression 1> : <expression 2>
So that if <condition> evaluates to True, then <expression 1> is returned and otherwise <expression 2>.
For example:
result = myVar == null ? 0 : myVar ;
// if myVar is null -> result = 0
// if myVar is not null -> result = myVar
There is actually plans to include a similar feature into the Dart language itself.
For a preview of how it would look like, take a look at this article by Remi
https://medium.com/flutter-community/quick-tip-sync-a-taste-of-the-future-9be4cd6993f4
And some accompanying Github issues
https://github.com/flutter/flutter/issues/17862
https://github.com/dart-lang/language/issues/47
https://github.com/dart-lang/language/issues/78
I having an issue when trying to update a SF stateful service after updating a child object of the service state. The upgrade fails to pass the 1st upgrade domain with a 'package activation' error. Digging around in the event viewer on the offending node gives the below exception:
Errormsg=TStore.OnApplyAddAsync: Unexpected exception System.NullReferenceException: Object reference not set to an instance of an object.
at xxx.DataChildObject.GetHashCode(IEqualityComparer comp)
at xxx.Data.GetHashCode(IEqualityComparer comp)
at xxx.Data.GetHashCode() in C:\Users\xxx\Source\Repos\xxx\xxx\xxx.Core\Domain.fs:line 17
at System.Fabric.Store.TStore`5.OnApplyAdd(TransactionBase txn, MetadataOperationData metadataOperationData, RedoUndoOperationData operationRedoUndo, Boolean isIdempotent, String applyType) Assert=System.Exception: at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
The change to the 'DataChildObject' was to add a new field that is a tuple of 2 doubles.
I understand that F# is automatically generating equality methods and these must be triggered during SF state and that due to datacontact serialisation these are null during the GetHashCode check.
I don't understand exactly when this check is being performed or why though?
As a test i tried overriding the GetHasCode method on my DataChildObject, but this didn't change the error i get when trying to upgrade my service.
[<DataContract>]
type DataChildObject =
class
[<DataMember(IsRequired=false,Name="Value1")>] val mutable Value1 : float * float
[<DataMember(IsRequired=false,Name="Value2")>] val mutable Value2 : float * float
[<DataMember(IsRequired=false,Name="NewValue")>] val mutable NewValue : float * float
new (v1,v2) = {Value1=v1;Value2=v2;NewValue=1.0,1.0}
override this.GetHashCode() =
let value1 = if (box this.Value1 = null) then 1 else this.Value1.GetHashCode()
let value2 = if (box this.Value2 = null) then 1 else this.Value2.GetHashCode()
let newValue = if (box this.NewValue = null) then 1 else (this.NewValue).GetHashCode()
value1+value2+newValue
end
I expect these domain objects to change more in the near future so any help in understanding exact how to get past or avoid this issue is helpful.
Thanks
It looks as if operator+= is not something that can be user defined (from Dart Language Specification):
The following names are allowed for user-defined operators: <, >, <=, >=, ==, -, +, /, ̃/, *, %, |, ˆ, &, <<, >>, []=, [], ̃.
However, if you provide an operator+(...) then that is used when operator+= is called.
The question I have is, how do you effect an efficient operator+=() without requiring the creation of a new instance?
class DividendBreakdown {
DividendBreakdown(this.qualified, this.unqualified, this.capitalGainDistribution);
bool operator==(DividendBreakdown other) =>
identical(this, other) ||
qualified == other.qualified &&
unqualified == other.unqualified &&
capitalGainDistribution == other.capitalGainDistribution;
num qualified = 0.0;
num unqualified = 0.0;
num capitalGainDistribution = 0.0;
DividendBreakdown.empty();
DividendBreakdown operator +(DividendBreakdown other) {
return new DividendBreakdown(qualified + other.qualified,
unqualified + other.unqualified,
capitalGainDistribution + other.capitalGainDistribution);
}
}
You can't, unless you also alter the behaviour of the + operator. The += operator and other compound assignment operators are simply shorthand for a normal operator and then an immediate assignment.
There is no way to extract extra information in the + operator to determine whether an assignment will be performed immediately or not, and therefore you must always return a new instance in case assignment is not being performed.
Instead, use your own method:
DividendBreakdown add(DividendBreakdown other) {
//...
}
You can solve efficiency problem from the another perspective. For example, if you have two huge complex collections and + operator will combine them into to the new one, it could be slow. But instead of directly combine those collections and creating new one, you can implement special collection view that will represent combination of them and perform heavy tasks 'on-demand' in a lazy style.
You can return this (or other) if you'd like to:
class Point {
int x;
int y;
Point(this.x, this.y);
Point operator +(Point other){
this.x = x + other.x;
this.y = y + other.y;
return this;
}
String toString(){
return "Point: x: $x, y: $y";
}
}
And usage:
Point p1 = new Point(1,1);
Point p2 = new Point(2,2);
print((p1+p2));
Result with: Point: x: 3, y: 3
My group is currently working with Lua,creating an android game. One thing we've run into is the appearant inability to create overload constructors.
I'm used to having an object set up with default values, that then get overloaded if need be.
ex:
apples()
{
taste="yum";
amount = 0;
}
apples(string taste, int num)
{
taste=taste;
amount=num;
}
However, with the inability to do that, we have these lare if/else sections for initialization that look like this
if velX ~= nil then
self.velX = velX
else
self.velX = 0
end
if velY ~= nil then
self.velY = velY
else
self.velY = 0
end
Is there a better way to set this up in Lua?
Instead of using if/else statements, you can initialize your variables with a condition providing the default value.
function apples(taste, num)
taste = taste or "yum"
amount = num or 0
-- ...
end
Lua's or operator evaluates and returns its first operand unless it is nil or false, otherwise it evaluates and returns its second operand. That leads to the above idiom for default values.