1

Is there a statically typed programming language that implements Polymorphism without using a superclass or an interface (if such implementation is even possible)?

  • Are you looking for any language in which you can do this (and want to know how) or a language that only does this? – candied_orange Jul 11 at 1:49
  • Are you interested in languages with high type-safety, languages with low type-safety, or both? C can "emulate" polymorphism with: direct access to untyped memory allocation (via malloc and free), function pointers (to emulate member function overriding), and pointer type casts. – rwong Jul 11 at 1:55
  • 1
  • 1
    Why do you think it is impossible? The most widely-used statically typed programming languages all have parametric polymorphism, e.g. Java, C#, C++. – Jörg W Mittag Jul 11 at 8:37
  • pretty much all statically tyoed functional languages fit this description too – jk. Jul 13 at 14:56
5

Most of the statically typed OOP languages are build on the concept of class and of interface to make polymorphism happen at runtime:

  • Sometimes it’s not called "class" in the language, but it is nevertheless a class in the broader OOP sense (e.g. struct in C++ are classes; struct in C# are a like classes but with value semantic).

  • Sometimes it’s not called “interface” in the language, but nevertheless provides all the features you’d expect from an interface (e.g. protocols in Swift, which offers more than interfaces in other languages).

Some statically typed languages provide other features for polymorphism, which could match your criteria:

  • some languages offer generics (e.g. C# and Java). Generics allow polymorphism, but they rely on interface-like mechanisms, so I’m not sure if it fully meets your expectations;

  • one language offers template meta-programming (i.e C++ templates). It’s a kind of generic programming in which you are not required to use generic types. It allows compile-time polymorphism without relying neither on classes nor on explicit interfaces. It qualifies for your criteria, provided compile-time polymorphism is acceptable;

  • Some languages natively provide multiple dispatch (e.g. C# miltimethods). This is a special form of polymorphism based on the combination of run-time types of arguments. This mechanism could qualify for your criteria, since the polymorphism is implemented at the level of the method through a dispatch table.

  • Ada offers a type based polymorphism. Type-based means that you focus on abstract types for which you define the operations in which these intervenes. I’m not an Ada expert, but to me it looks nevertheless very much like a class from an OOP point of view. However polymorphism seems to be defined via the definition of parameter types in the operations. I’m not sure how it is implemented, but I suspect it could be a dispatch table at method level, similar to what is used in multimethods (maybe an Ada expert could comment on that hypotheses). So it could qualify to your criteria, since the polymorphism would be implemented without relying on subtyping.

| improve this answer | |
2

TypeScript's and Flow's interfaces use compile-time checked duck typing.

| improve this answer | |
  • 2
    I don't know Flow, but TypeScript uses structural typing (does a member with the right name and type exist?) rather than duck typing (does a member with the right runtime behavior exist?) since static duck typing is equivalent to solving the Halting Problem in the general case, and thus impossible. – Jörg W Mittag Jul 11 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.