Why does overriding negate cause static warning in Dart - dart

Having a simple class as follows is considered a static warning, why?
operator 'negate' should return numeric type
class Vector {
final int x,y;
const Vector(this.x, this.y);
Vector operator +(Vector v) { // Overrides + (a + b).
return new Vector(x + v.x, y + v.y);
}
Vector operator -(Vector v) { // Overrides - (a - b).
return new Vector(x - v.x, y - v.y);
}
Vector operator negate() { // Overrides unary negation (-a).
return new Vector(-x,-y);
}
String toString() => '($x,$y)';
}
main() {
final v = new Vector(2,3);
final w = new Vector(2,2);
assert((-v).x == -2 && (-v).y == -3); // -v == (-2,-3)
}

As of 8/21/2012 the Dart Specification Section 7.1.2 Operators, considers defining negate with a possible nullary type as bad style and should report to the user a static warning.
Defining a nullary method named negate or a binary method named equals
will have the same effect as dening an operator but is considered bad
style, and will cause a static warning.
[...]
It is a static warning if the return type of the user-declared
operator []= is explicitly declared and not void. It is a static
warning if the return type of the user-declared operator equals is
explicitly declared and is not bool. It is a static warning if the
return type of the user-declared operator negate is explicitly
declared and not a numerical type.

We have an open bug for this: http://code.google.com/p/dart/issues/detail?id=3416

Related

Overload Operator for Built-in Type in Dart

Consider the following Dart code:
class Vec2 {
num x, y;
Vec2(this.x, this.y);
Vec2 operator*(num rhs) => Vec2(x * rhs, y * rhs);
String toString() => "<$x, $y>";
}
void main() => print(Vec2(1, 2) * 3);
The output is as expected:
<3, 6>
However, this only works when the left-hand side of the expression is a Vec2 and the right-hand side is a num. In this case, I want the multiplication operator to be commutative, so I write the following extension:
extension Vec2Util on num {
Vec2 operator*(Vec2 rhs) => Vec2(rhs.x * this, rhs.y * this);
}
One might naturally expect the following code to produce identical output to the first snippet:
void main() {
num x = 3;
print("${x * Vec2(1, 2)}");
}
However, the compiler is instead reporting that The argument type 'Vec2' can't be assigned to the parameter type 'num'. It looks as though the compiler is resolving the multiplication to num operator*(num rhs) in this case and then complaining that my Vec2 can't be passed in as a num operand. Why does the compiler apparently disregard my extension? What is the correct way to create custom commutative operators like this?
You cannot do what you want.
Dart user-definable binary operators are all methods on the first operand. Doing e1 + e2 is kind-of like doing e1.+(e2) where + is the name of the method, except you can't normally call a method +.
In order to be able to do 3 * vector, you need the method to be on 3.
You can't add methods to other people's classes, not without fiddling with their source code, and int is a system class so even that is not possible.
You cannot use extension methods because extension methods do not apply when the receiver type already has an instance method with the same name.
And int defines all the operators.
(It's like, and definitely not coincidentally, that the user-definable operators were chosen from exactly what int needs. That's not new in Dart, the operators go back to C and Java.)
So, you can define an extension method on int, but you can't call it anyway, not without an override:
extension MyInt on int {
Vector operator *(Vector other) => other.scalerMultiplication(this);
}
... MyInt(3) * vector ...
That's more complication than just swapping the operands.

What is the difference between 'is' and '==' in Dart?

Let's say I have:
class Test<T> {
void method() {
if (T is int) {
// T is int
}
if (T == int) {
// T is int
}
}
}
I know I can override == operator but what's the main difference between == and is in Dart if I don't override any operator.
Edit:
Say I have
extension MyIterable<T extends num> on Iterable<T> {
T sum() {
T total = T is int ? 0 : 0.0; // setting `T == int` works
for (T item in this) {
total += item;
}
return total;
}
}
And when I use my extension method with something like:
var addition = MyIterable([1, 2, 3]).sum();
I get this error:
type 'double' is not a subtype of type 'int'
identical(x, y) checks if x is the same object as y.
x == y checks whether x should be considered equal to y. The default implementation for operator == is the same as identical(), but operator == can be overridden to do deep equality checks (or in theory could be pathological and be implemented to do anything).
x is T checks whether x has type T. x is an object instance.
class MyClass {
MyClass(this.x);
int x;
#override
bool operator==(dynamic other) {
return runtimeType == other.runtimeType && x == other.x;
}
#override
int get hashCode => x.hashCode;
}
void main() {
var c1 = MyClass(42);
var c2 = MyClass(42);
var sameC = c1;
print(identical(c1, c2)); // Prints: false
print(identical(c1, sameC)); // Prints: true
print(c1 == c2); // Prints: true
print(c1 == sameC); // Prints: true
print(c1 is MyClass); // Prints: true
print(c1 is c1); // Illegal. The right-hand-side must be a type.
print(MyClass is MyClass); // Prints: false
}
Note the last case: MyClass is MyClass is false because the left-hand-side is a type, not an instance of MyClass. (MyClass is Type would be true, however.)
In your code, T is int is incorrect because both sides are types. You do want T == int in that case. Note that T == int would check for an exact type and would not be true if one is a derived type of the other (e.g. int == num would be false).
Basically, == is equality operator and "is" is the instanceof operator of Dart (If you come from Java background, if not, it basically tells you if something is of type something).
Use == for equality, when you want to check if two objects are equal. You can implement the == operator (method) in your class to define on what basis do you want to judge if two objects are equal.
Take this example:
class Car {
String model;
String brand;
Car(this.model, this.brand);
bool operator == (otherObj) {
return (otherObj is Car && other.brand == brand); //or however you want to check
//On the above line, we use "is" to check if otherObj is of type Car
}
}
Now you can check if two cars are "equal" based on the condition that you have defined.
void main() {
final Car micra = Car("Micra", "Nissan");
print(micra == Car("Micra", "Nissan")); // true
print(micra is Car("Micra", "Nissan")); // true
}
Hence, == is something you use to decide if two objects are equal, you can override and set it as per your expectations on how two objects should be considered equal.
On the other hand, "is" basically tells you if an instance is of type object (micra is of type Car here).

+= operator overloading in Dart

I trying make the following overloading inside my class:
class Array extends ListBase<double> {
List<double> l = [];
List<double> operator +=(List<double> b) {
var c = Array.length(l.length);
for(int i = 0; i < l.length; i++) {
c[i] = this[i] * b[i];
}
return c;
}
}
but the Dart compiler show the error message: the string '+=' ins't a user-definable operator. Is there some way to make the overloading of the operator += for others classes types?
Overload only operator +. Dart reuse operators that have a well known semantic meaning such as +=.
Add #override annotation if operator already defined in base class.

Colon : in Dart constructor syntax

class X extends Y {
X(int a, int b) : super(a,b);
}
Can someone give me an explanation about the syntax meaning of the colon :?
This feature in Dart is called "initializer list".
It allows you to initialize fields of your class, make assertions and call the super constructor.
This means that it is not the same as the constructor body. As I said, you can only initialize variables and only access static members. You cannot call any (non-static) methods.
The benefit is that you can also initialize final variables, which you cannot do in the constructor body. You also have access to all parameters that are passed to the constructor, which you do not have when initializing the parameters directly in the parentheses.
Additionally, you can use class fields on the left-hand of an assignment with the same name as a parameter on the right-hand side that refers to a parameter. Dart will automatically use the class field on the left-hand side.
Here is an example:
class X {
final int number;
X(number) : number = number ?? 0;
}
The code above assigns the parameter named number to the final field this.number if it is non-null and otherwise it assigns 0. This means that the left-hand number of the assignment actually refers to this.number. Now, you can even make an assertion that will never fail (and is redundant because of that, but I want to explain how everything works together):
class X {
final int number;
X(number): number = number ?? 0, assert(number != null);
}
Learn more.
It's ok to access non static member in initializer list.
class Point {
num x, y;
Point(this.x, this.y);
Point.origin(): this.x = 10, this.y = 10;
}
main() {
Point p = Point.origin();
print(p.x); // 10
}

Why can't Dart infer the type of List.fold()?

I am trying List.fold() in Dart 2.1.0:
void main() {
final a = [1,2,3];
print(a.fold(0, (x,y) => x + y));
}
It comes back with an error:
Error: The method '+' isn't defined for the class 'dart.core::Object'.
Try correcting the name to the name of an existing method, or defining a method named '+'.
Seems that it can't infer the type of x to be int, so doesn't know how to apply the + there.
According to the method spec, x should have the same type as the initial value 0, which is obviously an int. Why can't Dart infer that?
I can make it work by explicitly tipping the type:
void main() {
final a = [1,2,3];
print(a.fold<int>(0, (x,y) => x + y));
}
But I am a little disappointed that Dart cannot infer that for me. Its type inference seems stronger in most other cases.
Dart type inference works as expected:
void main() {
final a = [1,2,3];
var sum = a.fold(0, (x,y) => x + y);
print(sum);
}
The reason it fails in your example is because type inference outcome depends on the context where the expression is evaluated:
print(a.fold(0, (x,y) => x + y));
throws an error because print parameter is expected to be an Object: inference uses Object to fill generic T type argument` of fold:
T fold <T>(T initialValue, T combine(T previousValue, T element)) :
To validate this assumption:
void main() {
final a = [1,2,3];
Object obj = a.fold(0, (x,y) => x + y);
}
Throws exactly the same error.
Take away:
Type inference works well, but care must be paid to the generic expression's surrounding context.
Is it this a Dart limitation?
I dont think this behavoir it is a limitation of dart, just an implementation choice.
Probably there are also sound theoretical reasons that I'm not able to speak about but I can works out some reasoning about that.
Considering the generic method:
T fold <T>(T initialValue, T combine(T previousValue, T element))
and its usage:
Object obj = a.fold(0, (x,y) => x + y);
There are two paths for inferring the type of T:
fold returned value is assigned to an Object obj, 0 is an Object since it is an int, then T "resolve to" Object
fold first argument is an int, the returned value is expected to be an Object and int is an Object,
then T "resolve to" int
Dart chooses the path 1: it takes for inferred type the "broadest" (the supertype) that satisfy the generic method.
Should be better if Dart implements path 2 (the inferred type is the "nearest" type)?
In this specific case probably yes, but then there will be cases that does not works with path 2.
For example this snippet would not be happy with path 2:
abstract class Sensor {
String getType();
}
class CADPrototype extends Sensor {
String getType() {
return "Virtual";
}
}
class Accelerometer extends Sensor {
String getType() {
return "Real";
}
}
T foo<T extends Sensor>(T v1, T v2, T bar(T t1, T t2)) {
if (v2 is CADPrototype) {
return v1;
}
return v2;
}
Sensor foo_what_dart_does(Sensor v1, Sensor v2, Sensor bar(Sensor t1, Sensor t2)) {
if (v2 is CADPrototype) {
return v1;
}
return v2;
}
Accelerometer foo_infer_from_argument(Accelerometer v1, Accelerometer v2, Accelerometer bar(Accelerometer t1, Accelerometer t2)) {
if (v2 is CADPrototype) {
return v1;
}
return v2;
}
void main() {
Accelerometer v1 = Accelerometer();
CADPrototype v2 = CADPrototype();
Sensor result;
result = foo(v1, v2, (p1, p2) => p1);
// it works
result = foo_what_dart_does(v1, v2, (p1, p2) => p1);
// Compilation Error: CADPrototype cannot be assigned to type Accelerometer
result = foo_infer_from_argument(v1, v2, (p1, p2) => p1);
}

Resources