简体   繁体   中英

Call super method from Xcore operation

I have the following declarations in Xcore:

class ValueBase { ... }

class ValueArray extends ValueBase
{
  int size
  double [] values
  String valueUnit

  op boolean isValueEqual(Value v) 
  {
    if (!(v instanceof IValueArray))
    {
      return false
    }
    val other = v as IValueArray;
    return Iterables.elementsEqual(this.values, other.values);
  }

  op boolean equals(Value v)
  {
    return super.equals(v) && isValueEqual(v) && 
      (v instanceof IValueArray) &&
      Objects.equals(valueUnit, (v as IValueArray).valueUnit)
  }
}

ValueBase implements its own equals() method. In the concrete class ValueArray , I want to call super.equals() to compare the base class' common fields and then do comparisons specific to the concrete class.

But Xcore complains about that code that it " Couldn't resolve reference to JvmIdentifiableElement super ".

How can I call the equals() -method from the super class?

I had to make some assumptions about the code that you don't show here, but the short answer is that it works. You're calling super.equals correctly. But as I said, I had to make some assumptions. So here is what I have that seems to be fine with Xcore.

package soxcore

import com.google.common.base.Objects
import com.google.common.collect.Iterables

class ValueBase {
    op boolean equals(Value v)
    {
        return true;
    }
}

class ValueArray extends ValueBase, IValueArray
{
  int size

  op boolean isValueEqual(Value v) 
  {
    if (!(v instanceof IValueArray))
    {
      return false
    }
    val other = v as IValueArray;
    return Iterables.elementsEqual(this.values, other.values);
  }

  op boolean equals(Value v)
  {
    return super.equals(v) && isValueEqual(v) && 
      (v instanceof IValueArray) &&
      Objects.equal(valueUnit, (v as IValueArray).valueUnit)
  }
}

class Value extends ValueBase
{

}

interface IValueArray {
    double[] values
    String valueUnit
}

No complaints from Xcore. So am I missing something?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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