What does the "AS" mean in dart? the behaviour seems different from C#.
Am 08.09.2012 17:56 schrieb "Tom" <tomto...@gmail.com>:
>
> Here is the simple example to use as to cast, however, it seems that the child behave as the child after casting. Is it a bug? How can I to make child to behave like parent?
>
I must say, I don't know C#, so I'm wondering about the question a bit.
Just to tell my intuitive model here:
void main() {
var c=new Child();
// the object ref'ed by c IS a child
var a=c as ClassA;
// the cast does not change fact that the ref'd object is a child
// only the type of the reference may change.
a.outPut();
// the method is called on a reference 'a',
// the ref'ed object is a Child.
}
A cast tells the typechecker something it doesn't know, but the developer does: That the given object, although referenced as type A can safely be used as type B.
(So I do not see the sense in an up-cast, like here, anyway. A downcast or a cast from dynamic to specific makes more sense).
A cast does not change what an object really IS.
So there is no reason why outPut() should work differently, when called on 'a' or 'c'. The concrete behaviour is defined by the object, and thus by the class that has been used to construct it (it would btw really confuse me if C# is different here, because that would break the concept of 'override').
Considering all that, I have not found any reason for warnings or runtime errors or different behaviour.
When you now drop the method from ClassA, it results in a warning because by the cast you documented that you want to TREAT the object AS a ClassA, but you call a method ClassA doesn't define. Nevertheless, the object IS a Child and indeed contains the method, which therefore can be called.
That the method call now only is a warning and not an error is due to Dart's philosophy of optional typing. The code's behaviour doesn't change by putting in types or leaving them away. You used the dynamic nature yourself by typing the variable dynamic with 'var'. Now leave the cast away too and nothing changes.
HTH
Det
Matt
Ah, thanks for that article, didn't know the C# way.
Just to learn more:
- I currently can't imagine any use case for that. Can you pls give one?
- I consider the mixed behaviour of one concrete object confusing. How often is it used, and how bug ridden is it in practice?
KR
Det
--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
Ah, thanks for that article, didn't know the C# way.
Just to learn more:
- I currently can't imagine any use case for that. Can you pls give one?
- I consider the mixed behaviour of one concrete object confusing. How often is it used, and how bug ridden is it in practice?
Furthe more it still runs like a child even though the Editor complains about "ClassA has no such method" after I comment out method in parent.
What does the "AS" mean in dart? the behaviour seems different from C#.
class Base {void method() => print("In Base");}class Derived : Base {void method() => print("In Derived");}main() {Base obj1 = new Derived();obj1.method();Derived obj2 = new Derived();obj2.method();}
To be pedantic, that C# program wouldn't compile. Make the methods public and slap a 'new' on the method in Derived, and it would have that behavior.
Default visibility is internal so as long as that's all in one assembly, you shouldn't need public. The absence of "new" should just cause a warning, not an error, I believe.