Using num, one can assign a double to an integer without error or
warning as in the following code:
http://try.dartlang.org/s/ZtJA
I would have expected assignment to floor and cast to int. Is this
intentional?
Can we cast to any type, not just downcast? For example, we might have
an object implement two irrelevant interfaces, can we cast between
them? For example,
I1 a = xxx;
I2 b = a; //compiler warning since I2 is not a subtype or supertype of
I1
class ClassTest {
int i;
}
void a(Object x) {
b(x); // ClassTest inherits Object, but that doesn't mean it has the same interface
}
void b(ClassTest x){
x.i = 2; // a() can pass a non type safe class to make this fail
}
I'd love to have the option to see potential type errors flagged without me having write if (x is ClassTest) or carry out reflection.
Does anyone know if this is in the future for Dart?
--
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.
I mean, I don't understand what this feature gives.
CanvasElement canvas = querySelector("canvas#thing");
CanvasElement canvas = querySelector("canvas#thing") as CanvasElement;
CanvasElement canvas = querySelector("canvas#thing");
canvas.requestFullscreen();
querySelector("canvas").requestFullscreen();
var canvas = CanvasElement.queryFrom(document, "#thing");
var canvas = querySelector<CanvasElement>("#thing");
And actually, its a upcast at the call site of b(x), where x is upcast to ClassTest
Well of course you need to cast in those cases justbas you would have to cast in java.
So the argument is convenience? I mean why don't we throw all typing away then and just use JavaScript style rules since that is even more convenient.
As it is I cast anyways because of the warnings, so what is the point?
--
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.
On Mon, Jun 29, 2015 at 12:24 PM, Daniel Joyce <daniel....@gmail.com> wrote:I mean, I don't understand what this feature gives.The motivating example that I heard was querySelector(). That method nominally returns an Element. In practice, you often know what subclass it returns and want do treat the result as that subclass. For example:CanvasElement canvas = querySelector("canvas#thing");If Dart didn't allow implicit downcasting, this would cause a warning. You would have to do:CanvasElement canvas = querySelector("canvas#thing") as CanvasElement;Implicit downcasting addresses that.
Originally there was no cast operator, so there was no other way to do a down-cast.
Originally there was no cast operator, so there was no other way to do a down-cast.It was. The Object class used to have a getter called dynamic that returned this, but its static type was Dynamic. And IIRC, you could always assing [Dd]ynamic to anything without warning.
Or, if we had generic methods:var canvas = querySelector<CanvasElement>("#thing");
reductio ad absurdum doesn't help the argument.
Even without a down cast one can write code to access the properties and methods of the specialized class, it will run fine, and will produce no analyzer warnings if the analyzer did not have enough information to comprehend the types you are accessing. That's Dart - if that's what you want it to be. For those that wish to have strict and traditional type enforcement there are options such as GWT.
The following is okay in Dart:
void blah(SomeBaseType x) {
SomeSpecializedType x = y; // implicit cast
y.doSomethingSpecial();
// Also works in dart. No analyzer warning if x was declared "var", "dynamic",
// or had not type annotation at all
x.doSomethingSpecial();
}
... and the exact same cast happens in countless lines of Java and C++ differing only by the Java cast or C++ dynamic_cast. Java and C++ may fail on the cast at runtime. Dart may fail on the cast in check mode. In unchecked mode it may get further and fail on a unknown property access.
I mean why don't we throw all typing away then and just use JavaScript style rules since that is even more convenient.
snip