简体   繁体   English

Dart覆盖一元减号运算符

[英]Dart overriding unary minus operator

As per Language specs (10.1.1 Operators) I am trying to override some operators. 根据语言规范(10.1.1运算符)我试图覆盖一些运算符。

I get an analyzer error when overriding the 'minus' and 'unary minus' operators - one that I don't get: 当覆盖'减'和'一元减'运算符时,我得到一个分析器错误 - 一个我没有得到的:

'The operator "-" is not defined on class Indentation' '运算符' - “未在类缩进上定义”

but in the class I have defined it: 但在课堂上我定义了它:

  Indentation operator -() {
    level--;
    return this;
  }

and I use it like myInstance--; 我像myInstance--;一样使用它myInstance--; and it actually does work, but still the analyzer complains and I cannot submit the code 'clean' because of the error. 它实际上工作,但仍然分析器抱怨,我不能提交代码'干净'因为错误。

I have looked up an old thread ( Why does overriding negate cause static warning in Dart ) but I think it is not relevant here. 我查了一个旧线程( 为什么覆盖否定导致Dart中的静态警告 )但我认为这与此无关。

Any advise is welcome. 任何建议都是受欢迎的。

--x is the same as x -= 1 . --xx -= 1相同。 To use it you have to define the operator -(p) (not operator -() ) 要使用它,你必须定义operator -(p) (不是operator -()

Indentation operator -(n) => new Indentation(level - n);

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

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