Home > Software design >  Strange behaviour in return type for getter problem when returning enum
Strange behaviour in return type for getter problem when returning enum

Time:07-09

This is continuation of return type for getter problem when returning enum.
When I am using enum (enum.enum_value.name) I am getting an error however when I use static const String the code builds fine.

I created a variable of enum.enum_value.name (SerialPortParity.none.name), it is creating a variable of type String.

final returnVal = SerialPortParity.none.name;

I am unable to understand this behavior.
https://dart.dev/tools/diagnostic-messages?utm_source=dartdev&utm_medium=redir&utm_id=diagcode&utm_content=getter_not_subtype_setter_types#getter_not_subtype_setter_types

If I am getting the error The return type of getter 'parity' is 'String' which isn't a subtype of the type 'SerialPortParity' of its setter 'parity'. then why I am not getting the same error when I return static const String

Running Code :

  static const String parityNone = 'None';
  static const String parityEven = 'Even';
  static const String parityOdd = 'Odd';
  static const String parityMark = 'Mark';
  static const String paritySpace = 'Space';

  int _parity = UsbPort.PARITY_NONE;
  String get parity {
    switch (_parity) {
      case UsbPort.PARITY_NONE:
        return SerialPort.parityNone;
      case UsbPort.PARITY_EVEN:
        return SerialPort.parityEven;
      case UsbPort.PARITY_ODD:
        return SerialPort.parityOdd;
      case UsbPort.PARITY_MARK:
        return SerialPort.parityMark;
      case UsbPort.PARITY_SPACE:
        return SerialPort.paritySpace;
      default:
        return SerialPort.parityNone;
    }
  }

  set parity(String parity) {
    switch (parity) {
      case SerialPort.parityNone:
        _parity = UsbPort.PARITY_NONE;
        break;
      case SerialPort.parityEven:
        _parity = UsbPort.PARITY_EVEN;
        break;
      case SerialPort.parityOdd:
        _parity = UsbPort.PARITY_ODD;
        break;
      case SerialPort.parityMark:
        _parity = UsbPort.PARITY_MARK;
        break;
      case SerialPort.paritySpace:
        _parity = UsbPort.PARITY_SPACE;
        break;
      default:
        //No Change
        break;
    }
  }

Error Code :

enum SerialPortParity {
  none,
  even,
  odd,
  mark,
  space,
}

 int _parity = UsbPort.PARITY_NONE;
  String get parity {
    switch (_parity) {
      case UsbPort.PARITY_NONE:
        return SerialPortParity.none.name;
      case UsbPort.PARITY_EVEN:
        return SerialPortParity.even.name;
      case UsbPort.PARITY_ODD:
        return SerialPortParity.odd.name;
      case UsbPort.PARITY_MARK:
        return SerialPortParity.mark.name;
      case UsbPort.PARITY_SPACE:
        return SerialPortParity.space.name;
      default:
        return SerialPortParity.none.name;
    }
  }

set parity(SerialPortParity parity) {
    switch (parity) {
      case SerialPortParity.none:
        _parity = UsbPort.PARITY_NONE;
        break;
      case SerialPortParity.even:
        _parity = UsbPort.PARITY_EVEN;
        break;
      case SerialPortParity.odd:
        _parity = UsbPort.PARITY_ODD;
        break;
      case SerialPortParity.mark:
        _parity = UsbPort.PARITY_MARK;
        break;
      case SerialPortParity.space:
        _parity = UsbPort.PARITY_SPACE;
        break;
      default:
        //No Change
        break;
    }
  }

Error :

The return type of getter 'parity' is 'String' which isn't a subtype of the type 'SerialPortParity' of its setter 'parity'.
Try changing the types so that they are compatible.

CodePudding user response:

Setter and Getter must use the same Type.

In your case, either a String or an Enum.

One cannot mix these:

String get parity {} // STRING

set parity(SerialPortParity parity) {} // ENUM

Consider the following, disfunctional code, which also uses both types for the same variable.

This is how it looks to Dart when one defines a Getter and Setter with not-matching Types:

// Will not compile, as parity can be of one Type only.
class Abc {
  String parity;
  SerialPortParity parity;
}


var abc = Abc();
abc.partity = x;
var z = abc.partity;

  • Related