For better type conversion allow to override explicit conversion operator.
Example:
class Byte {
int _value;
Byte(this._value) {
//
}
operator explicit(other) {
if(other is int) {
return new Byte(other);
} else if(other is Byte) {
return this;
}
throw new ArgummnetError('other: $other');
}
int get value = _value;
}
class Integer {
int _value;
Integer(this._value) {
//
}
operator explicit(other) {
if(other is int) {
return new Integer(other);
} else if(other is Byte) {
return new Integer(other._value);
} else if(other is Integer) {
return this;
}
throw new ArgummnetError('other: $other');
}
int get value = _value;
}
int test1() {
var integer = 128 as Integer;
integer = integer + 512;
return integer.value;
}
Byte test2() {
var integer = 128 as Integer;
var byte = 128 as Byte;
byte = byte * 5;
integer = byte + byte as Integer;
return integer as Byte;
}
Also this short form would be very useful.
"(type) value"
And the same examples with shor form.
int test1() {
var integer = (Integer)128;
integer = integer + 512;
return integer.value;
}
Byte test2() {
var integer = (Integer)128;
var byte = (Byte)128;
byte = byte * 5;
integer = (Integer)byte + byte;
return (Byte)integer;
}
In both cases this works just as other overridden operators.
The same as (by example) + operator.
Code generated by the compiler. The same as for other operators.
Difference in that the used static members of type into which produced the transformation.
0 as Byte // Byte.explicit(0)
int test1() {
var integer = Integer.explicit(128);
integer = integer.add(512);
return integer.value;
}
Byte test2() {
var integer = Integer.explicit(128);
var byte = Byte.explicit(128);
byte = byte.mul(5);
integer = Integer.explicit(byte.add(byte));
return Byte.explicit(integer);
}
Issue: Allow possibility to override explicit conversion operator
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
The "as" operator does two things, a static cast and a runtime type check. It's defined in the section called "Type cast" of the specification.
P.S.
You are talking about "as" operator but in examples you use an another ("is") operator. For me "as" and "is" operators are not the same. Please correct me if I am wrong.