Skip to content

Support for method overloading when using typed arguments #49

@DartBot

Description

@DartBot

This issue was originally filed by [email protected]


I saw mention in documentation that Dart does not support method overloading because it is a dynamic language. However, Dart does support optional type annotations, which, as far as I can tell, opens the door slightly for the possibility of having method overloading when using typed argument values. I am proposing support for this behavior.

Currently when a programmer wants to support multiple types in a dynamic language using a single method, they will perform the instance checks manually as follows:

  class Displayer {
    void display(element) {
      if (element is String) { print(element); }
      else if (element is Image) { /* display image on screen */ }
      else { throw "Invalid argument"; }
    }
  }

This is cumbersome to the programmer and not easily translated in documentation-- not to mention easy to get wrong. Given support for optional types, I think we should be able to offload this dispatch to the compiler, which would make it much easier to write such methods.

For example, with overloading we could simplify the above snippet to:

  class Displayer {
    void display(String text) { print(text); }
    void display(Image image) { /* display image on screen */ }
  }

I am no compiler expert, but it seems plausible to me that given type annotations, a compiler could easily handle dispatching to the correct method at runtime. In addition, when cross-compiling to JS, the compiler could insert the manual checks as per the initial snippet above so that it would be equivalent to existing code that we see today. In fact, in the worst case, the Dart compiler could just always perform this translation directly in the method when it sees such an overload. That way there would not even be a need to modify the method dispatch rules in the VM-- it would simply be a completely transparent type check / dispatch that the user no longer has to do manually.

A couple of obvious restrictions on the functionality would be:

  1. Return types for overloaded methods would have to match. This is no different from other languages with overloading support, so nothing new here.
  2. All types would have to be declared on each variant when declaring an overloaded method. The compiler could easily check for this. I don't think users would be surprised by such an error if they ran into it-- seems intuitive enough.

All in all, this would be a great way to display the power of optional typing in the dynamic language world. It simplifies an idiom that is commonly used in dyn langs to circumvent the lack of overloading, and it would save developers from having to use separate method names depending on the input types. What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-not-plannedClosed as we don't intend to take action on the reported issuetype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions