I have a similar situation where I need to connect additional information with basic data types like String, int, double, ...
I didn't find a solution other than wrapping them.
What you can try is to optimize these wrapper classes by
- making them const (with a const constructor
- making the fields final where possible
- others might be possible
EDIT
- You definitely want to get rid of all those switch statements.
- 0.134285 sec: without wrappers
- 0.645971 sec: with simplified constructor, operator <= (see below)
using const constructor didn't make a notable difference (propably more important when converted to JS) - 1.449707 sec: with simplified constructor
- 3.792590 sec: your code
class $PyNum2 {
final num _value;
const $PyNum2(this._value);
factory $PyNum2.from(value) {
switch ($getType2(value)) {
case 6:
return new $PyNum2(value);
break;
case 7:
try {
return new $PyNum2(num.parse(value));
} catch (ex) {
print("Invalid string literal for num parsing");
exit(1);
}
break;
case 5:
return new $PyNum2(value.value());
break;
default:
throw "Invalid input for num conversion";
}
}
value() => _value;
toString() => _value.toString();
operator +(other) => new $PyNum2(_value + other.value());
operator -(other) => new $PyNum2(_value - other.value());
operator *(other) => new $PyNum2(_value * other.value());
operator ~/(other) => new $PyNum2(_value ~/ other.value());
operator %(other) => new $PyNum2(_value % other.value());
operator ==(other) {
switch ($getType2(other)) {
case 6:
return _value == other;
case 5:
return _value == other.value();
default:
return false;
}
}
operator <(other) {
switch ($getType2(other)) {
case 6:
return _value < other;
case 5:
return _value < other.value();
default:
return true;
}
}
operator >(other) => !(this < other) && (this != other);
operator <=(other) => this.value() <= other.value(); //(this < other) || (this == other);
operator >=(other) => (this > other) || (this == other);
}
see also: