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

Conceptual question: parametric vs ad-hoc polymorphism #713

Open
gchamon opened this issue Nov 3, 2023 · 1 comment
Open

Conceptual question: parametric vs ad-hoc polymorphism #713

gchamon opened this issue Nov 3, 2023 · 1 comment

Comments

@gchamon
Copy link

gchamon commented Nov 3, 2023

I just got through Structs, methods and interfaces and at the end of the chapter, wrapping up the concepts, it is stated that Declaring interfaces so you can define functions that can be used by different types (parametric polymorphism) linking to https://en.wikipedia.org/wiki/Parametric_polymorphism.

I was discussing what I learned with a friend of mine, which pointed out that what is done in the chapter, implementing an interface that doesn't care about specifics of the implementation, but expecting a specific set of inputs and outputs, alongside their types, is actually ad-hoc polymorphism.

I read the introduction of the wikipedia article (I have to admit I wasn't too familiarized with the concept) and indeed, since we have different function definitions for each of the types (Rectangle, Circle and Triangle), it seems like it is ad-hoc. On the other hand, the interface itself doesn't really care about the implementation, and behaves the same, regardless of the type (Rectangle, Circle and Triangle).

What is it then? Did we really implement parametric polymorphism or ad-hoc polymorphism in the chapter? Is the type indifference at the interface level or the implementation level? If it is really ad-hoc, what would constitute parametric polymorphism in Go (and vice-versa)? Also, what do generics add to this discussion?

@bryanenders
Copy link

bryanenders commented Mar 10, 2024

Indeed, what’s done in the referenced chapter is a form of function overloading, which is an application of ad-hoc polymorphism. Go does also support generics, which are an application of parametric polymorphism.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants