简体   繁体   English

有什么区别?? 和=?? 在 Dart?

[英]What is the difference between ?? and =?? in Dart?

In the code below to implement the Singleton design pattern, we approach two different ways.在下面实现 Singleton 设计模式的代码中,我们采用了两种不同的方法。 One of them creates Singleton but the other creates an object each time we call it.其中一个创建Singleton ,而另一个在我们每次调用它时创建一个 object。 What's the difference between ??有什么区别?? and =??=?? in the code below written in dart?在下面的代码中写成 dart?

class ExampleStateByDefinition extends ExampleStateBase {
  static ExampleStateByDefinition? _instance;

  ExampleStateByDefinition._internal() {
    initialText = 'A new "ExampleStateByDefinition" instance has been created.';
    stateText = initialText;
    print(stateText);
  }

  static ExampleStateByDefinition? getState() {
    return _instance ?? ExampleStateByDefinition._internal();     //here we use ??
  }
}

The above code creates an object each time we call it, so let us look at another variant:上面的代码在我们每次调用它时都会创建一个 object,所以让我们看看另一个变体:

class ExampleStateByDefinition extends ExampleStateBase {
  static ExampleStateByDefinition? _instance;

  ExampleStateByDefinition._internal() {
    initialText = 'A new "ExampleStateByDefinition" instance has been created.';
    stateText = initialText;
    print(stateText);
  }

  static ExampleStateByDefinition? getState() {
    _instance ??= ExampleStateByDefinition._internal();        //Here we use ??=
    return _instance;
  }
}

The above code implements Singleton by definition in Design Patterns: Elements of Reusable Object-Oriented Software上面的代码通过设计模式中的定义实现了 Singleton:可重用面向对象软件的元素

Both ??两者?? and ??= are null-aware operators .??=null-aware operators

Difference区别

The difference between the two operators is that the former only evaluates an expression while the latter also assigns the result of the expression.这两个运算符的区别在于前者只对表达式求值,而后者还对表达式的结果进行赋值

This is why your first sample returns a new instance each time since you never assign it to your static variable.这就是为什么您的第一个示例每次都返回一个新实例,因为您从未它分配给 static 变量。

??

Use??利用?? when you want to evaluate and return an expression IFF another expression resolves to null.当你想计算并返回一个表达式时,IFF 另一个表达式解析为 null。

The following expression:以下表达式:

exp ?? otherExp;

Is similar to this expression:类似于这个表达式:

((x) => x == null ? otherExp : x)(exp);

??=

Use??= when you want to assign a value to an object IFF that object is null. Otherwise, return the object.当你想给 object IFF 赋值时使用 ??= object 是 null。否则,返回 object。

The following expression:以下表达式:

obj ??= value;

Is similar to this expression:类似于这个表达式:

((x) => x == null ? obj = value : x)(obj);

See Null-aware operators in Dart for reference.请参阅Dart 中的 Null-aware operators 以供参考。

The ??= operator is a compound assignment operator. ??=运算符是复合赋值运算符。 Just like target += 1 is equivalent to target = target + 1 (but with target only evaluated once, if it's a complicated expression), target??= expression is equivalent to target = target?? expression就像target += 1等价于target = target + 1 (但target只计算一次,如果它是一个复杂的表达式), target??= expression等价于target = target?? expression target = target?? expression (but with target only evaluated once and the assignment not even happening if target is non- null ). target = target?? expression (但target只评估一次如果target不是null则赋值甚至不会发生)。

So, the difference is that the first code probably doesn't work, the second one does.所以,不同之处在于第一个代码可能不起作用,而第二个代码可以。

The code:代码:

 return _instance ?? ExampleStateByDefinition._internal();

checks whether _instance is non- null , and if so, it returns the value of _instance .检查_instance是否为非null ,如果是,则返回_instance的值。 If it is null , it evaluates and returns ExampleStateByDefinition._internal() .如果是null ,它会计算并返回ExampleStateByDefinition._internal() No-where in that does it assign to _instance .它没有分配给_instance的地方。 So, _instance is always going to be null , and the code is probably failing to do what it intended to do—caching a value.因此, _instance始终为null ,代码可能无法执行其预期的操作——缓存一个值。

The code:代码:

 _instance ??= ExampleStateByDefinition._internal();  
 return _instance;

or its more streamlined version:或其更精简的版本:

 return _instance ??= ExampleStateByDefinition._internal();  

will also check if _instance is null and return its value if it isn't.还将检查_instance是否为null ,如果不是,则返回其值。 If _instance is null , it also evaluates ExampleStateByDefinition._internal();如果_instancenull ,它还会评估ExampleStateByDefinition._internal(); , and then it assigns it to _instance , and returns the value. 然后将其分配给_instance ,并返回值。

The next time you come around, _instance will be non- null , and the lazy caching works.下次你回来时, _instance将不是null ,并且惰性缓存起作用。

Based on dart tour .基于dart 游览

To assign only if the assignment to the variable is null we use ??= operation.仅当对变量的赋值是 null 时才赋值,我们使用??=操作。

expr1?? expr2 expr1?? expr2 if expr1 is not null, return its value; expr1?? expr2如果 expr1 不是 null,返回它的值; otherwise, evaluates and return the value of expr2.否则,计算并返回 expr2 的值。

Cause ??原因?? operation evaluates each time we call the object, it creates a new object.每次我们调用 object 时,操作都会评估,它会创建一个新的 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM