-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Walkthrough inheritance example bug #3343
Comments
It is because the compiler thinks tom is an If you remove |
Yes, but that's weird because it's not consistent with how the Handbook has it written - maybe @jonathandturner can check this out and confirm whether that was the intention. |
The walkthrough on inheritance in the playground still has a bug. The '34' value is unused yet. I agree it's weird because if you pass a value, you expect it works right? |
Hi @jonathandturner @sant123 @DickvdBrink . In this walkthrough, Horse extends Animal, does it also inherited the move method in Animal. From the perspective of C#/JAVA, should tom.move(34) just invokes the move method defined in Animal? Just started playing around TypeScript. This walkthrough example is a little weird to me. Thanks. |
Yeah, that's essentially what I find confusing. When you pass a horse a parameter, it's calling a parameter-less function. I would expect the Horse.move function to be called if it matched the signature of the call. That appears not to be the case. Also, why was the bug label removed? What's the status on this? |
Seems like we could make this less confusing either with changes to the code or some additional prose. |
Why was the bug label removed again? I think this is quite a bad bug and is preventing me from adopting type script. I quite like #2000 as a possible solution which would make the behaviour the same as virtual functions in most languages. |
@noggin182 this issue is tracking updating the sample on the playground. if you think there is another issue please file a separate one and add some more details. |
Well that's not entirely true. While this bug was noticed while trying to use the sample on the playground. It does have to do with the inconsistent nature in which inheritance is handled within typescript. Is there a cogent explanation as to why typescript's inheritance does not behave as expected? |
Is question why If so then because the one with no arguments does not violate the is-a relationship. in JavaScript it is allowed to pass additional arguments to a function that does not expect them, they will be ignored. in other words, it is safe to call |
@mhegazy no. The question is this: Tom is a horse, which is an extension on an animal. When Tom calls his move function, and is passed a parameter, the expectation is that the Animal.move Method, which takes a parameter matches the signature of the call. Instead, the Horse.move function, which takes no parameters is executing. That doesn't make sense. Tom is an Animal. Animal has a move function that takes a parameter. If Tom calls a move function with a parameter, I would expect it to call the move function that takes a parameter. Why is Tom.Move with a parameter calling Horse.Move lacking a parameter, as opposed to Animal.Move taking a parameter? |
How is this different from if the method took the parameter and did nothing with it? It is sound for a function to accept less than it will be given. |
That's what is happening. And therein lies the problem. A class with an inherited function that takes a function, and an overload that does not take a parameter is only calling the version that takes no parameters. In that case it's not inheriting the function. This is different than taking a parameter and doing nothing, because that's not the function that's being called. |
Let me explain this:
The result....
Are you really sure that this is not a bug? I mean, tom: Animal is using the method FROM Animal, that's why the compiler offers you to pass the parameter 34. But in spite of the 34 parameter is provided, it is just ignored. So if I pass a parameter to a function I expect that my parameter works in the function. Just saying... common sense. |
I think this program illustrates why I think this typescript functionality is confusing. What is happening here doesn't work/translate to C#. In C#, Tom would move 34 meters. You'd also have to cast Tom as a Horse to call the parameter-less version of the function. |
That's right @tex23bm . Even I just re wrote the whole class to be almost equal with the TypeScript example. tom that now is an animal must include the parameter meters in the move method. As a result, the value it's being used '34'. On the other hand, if TypeScript supports overloading methods, sam the Snake would use the two methods of move. From the Animal and it's own method. But tom could not do it because it's explicitly an animal. |
should be fixed in the handbook. |
Can we get a link to this explanation in the handbook? IITFM isn't exactly a good resolution to this. |
We still need to have the Playground not have confusing examples in it |
This issue was moved to Microsoft/TypeScript-Docs#143 |
Is there any way we can get someone aside from @mhegazy looking at this? He seems only interested in closing the issue, not with addressing, explaining or resolving. |
The link is not dead. it is just private :) the issue has been resolved on the handbook, please see https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Classes.md#inheritance The playground issue, will be updated with the website refresh in the next few days. |
Ok, so, looking at the documents, it looks like you accepted that typescript was wrong. And that this was a bug. And that it shouldn't have operated in the way it did. It looks like you fixed the issue, according to these documents. Why then, did you just refuse to acknowledge for literally months that the playground was wrong, and that there was a bug? Why did you carry on that this was a problem of us the users not understanding the manual? OR Did you just change the example you demonstrate without fixing the underlying problem that was surfaced? I'm going to contend that there was a bug in typescript, in that a call to a function on a class that accepts a parameter was calling the child's parameter-less version of that function. Did you fix that bug? Also, I really don't care for the smiley face calling out that your explanation is just too private for us plebeian users to access. |
@tex23bm There's no need to be aggressive.
This is not a bug, which is why this isn't hasn't been as high of a priority as other work. This behavior of functions is described in the FAQ. |
My apologies for adding a link that is not available. it is the tool i use to move issues between repos (https://github-issue-mover.appspot.com/). I should have noticed that earlier. again sorry for the confusion this have caused. |
So, with this code... class Animal {
constructor(public name: string) { }
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Snake extends Animal {
constructor(name: string) { super(name); }
move() {
console.log("Slithering...");
super.move(5);
}
}
class Horse extends Animal {
constructor(name: string) { super(name); }
move() {
console.log("Galloping...");
super.move(45);
}
}
let sam = new Snake("Sammy the Python");
let tom: Animal = new Horse("Tommy the Palomino");
sam.move();
tom.move(34); Specifying the 34 or not... I'll still get a 45 right? @tex23bm |
If you specify 34, you will get 45, if you specify nothing, you'll get a transpiler error |
I see it @noggin182, I think the problem is by design of JavaScript... Even any weakly typed language. |
http://www.typescriptlang.org/Playground
I think the walkthrough on inheritance in the playground might have a bug.
I'm not sure why tom.move is being passed 34 if it's unused. It's not clear to me if that should have thrown a compilation error or what.
Thanks
The text was updated successfully, but these errors were encountered: