Skip to content
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

Closed
tex23bm opened this issue Jun 2, 2015 · 29 comments
Closed

Walkthrough inheritance example bug #3343

tex23bm opened this issue Jun 2, 2015 · 29 comments
Assignees
Labels
Docs The issue relates to how you learn TypeScript

Comments

@tex23bm
Copy link

tex23bm commented Jun 2, 2015

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

@DickvdBrink
Copy link
Contributor

It is because the compiler thinks tom is an Animal (not a Horse) because of the type annotation.

If you remove : Animal and try to call tom.move it will show the move method without parameters.

@DanielRosenwasser
Copy link
Member

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.

@sophiajt sophiajt self-assigned this Jun 4, 2015
@mhegazy mhegazy added Bug A bug in TypeScript Docs The issue relates to how you learn TypeScript labels Jun 10, 2015
@mhegazy mhegazy added this to the TypeScript 1.6 milestone Jun 10, 2015
@mhegazy mhegazy removed the Bug A bug in TypeScript label Jul 1, 2015
@sant123
Copy link

sant123 commented Aug 4, 2015

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?

@HuanhuanSunMSFT
Copy link

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.

@tex23bm
Copy link
Author

tex23bm commented Aug 14, 2015

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?

@danquirk danquirk added the Bug A bug in TypeScript label Aug 20, 2015
@danquirk
Copy link
Member

Seems like we could make this less confusing either with changes to the code or some additional prose.

@mhegazy mhegazy removed this from the TypeScript 1.6 milestone Aug 28, 2015
@mhegazy mhegazy assigned mhegazy and unassigned sophiajt Sep 2, 2015
@mhegazy mhegazy added this to the TypeScript 1.8 milestone Sep 18, 2015
@mhegazy mhegazy modified the milestones: TypeScript 1.7, TypeScript 1.8 Oct 9, 2015
@mhegazy mhegazy assigned DanielRosenwasser and unassigned mhegazy Dec 1, 2015
@mhegazy mhegazy removed the Bug A bug in TypeScript label Dec 1, 2015
@noggin182
Copy link

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.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 2, 2015

@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.

@tex23bm
Copy link
Author

tex23bm commented Dec 2, 2015

@mhegazy

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?

@mhegazy
Copy link
Contributor

mhegazy commented Dec 2, 2015

Is question why Snake.move has no parameters where as Animal.move has one?

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 move on an object of type Snake as if it was an Animal. this is identical to Snake declaring an parameter and never using it.

@tex23bm
Copy link
Author

tex23bm commented Dec 2, 2015

@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?

@DanielRosenwasser
Copy link
Member

Instead, the Horse.move function, which takes no parameters is executing.

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.

@tex23bm
Copy link
Author

tex23bm commented Dec 4, 2015

@DanielRosenwasser

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.

@sant123
Copy link

sant123 commented Dec 12, 2015

Let me explain this:

var tom: Animal = new Horse("Tommy the Palomino");
tom.move(34);

The result....

"Tommy the Palomino moved 45 m." 

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.

@tex23bm
Copy link
Author

tex23bm commented Dec 29, 2015

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.

Program.zip

@sant123
Copy link

sant123 commented Dec 31, 2015

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.

Program.zip

@mhegazy
Copy link
Contributor

mhegazy commented Feb 20, 2016

should be fixed in the handbook.

@mhegazy mhegazy closed this as completed Feb 20, 2016
@tex23bm
Copy link
Author

tex23bm commented Feb 22, 2016

Can we get a link to this explanation in the handbook?

IITFM isn't exactly a good resolution to this.

@RyanCavanaugh
Copy link
Member

We still need to have the Playground not have confusing examples in it

@RyanCavanaugh RyanCavanaugh reopened this Feb 22, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Mar 17, 2016

This issue was moved to Microsoft/TypeScript-Docs#143

@mhegazy mhegazy closed this as completed Mar 17, 2016
@tex23bm
Copy link
Author

tex23bm commented Mar 17, 2016

This Issue should not have been closed. The link to the supposed docs issue is a dead link.

image

@tex23bm
Copy link
Author

tex23bm commented Mar 17, 2016

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.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 17, 2016

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.

@tex23bm
Copy link
Author

tex23bm commented Mar 17, 2016

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.

@RyanCavanaugh
Copy link
Member

@tex23bm There's no need to be aggressive.

I'm going to contend that there was a bug in typescript,

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.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 18, 2016

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.

@sant123
Copy link

sant123 commented Sep 16, 2016

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

@noggin182
Copy link

If you specify 34, you will get 45, if you specify nothing, you'll get a transpiler error

@sant123
Copy link

sant123 commented Sep 16, 2016

I see it @noggin182, I think the problem is by design of JavaScript... Even any weakly typed language.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

No branches or pull requests

10 participants