dart-lang / language

Design of the Dart language
Other
2.67k stars 205 forks source link

Runtime-evaluated pattern matching function signatures. #3368

Open water-mizuu opened 1 year ago

water-mizuu commented 1 year ago

This shouldn't allow overloading, and should just be treated as syntactic sugar. Of course, functions that use patterns should be consistent and have a bottom-definition / should be exhaustive. Example below:

int fibonacci(int v case 0 || 1) => 1;
int fibonacci(int v) => fibonacci(v - 1) + fibonacci(v - 2);

This would be sugar for:

int fibonacci(int v) {
  switch (v) {
    case 0 || 1:
      return 1;
    default:
      return fibonacci(v - 1) + fibonacci(v - 2);
  }
}

An example of function of potentially unsafe overloading:

void process(Object _ case String v) => print("It is a string");
void process(Object _ case int v) => print("It is an int");
void process(Object _) => print("It's neither a string nor an int.");

This would mean that the functions must have the same signature, and is not fully fledged function overloading, but it's worth a discussion in my opinion.

modulovalue commented 1 year ago

@water-mizuu with switch expressions your example could be much shorter, and the signature of the function wouldn't have to be duplicated for each case, as is the case in your proposal:

int fibonacci(int v) => switch (v) {
  0 || 1 => 1,
  _ => fibonacci(v - 1) + fibonacci(v - 2),
};
julemand101 commented 1 year ago

I think a problem with this solution is going to be that the order of your function declarations now matter, which is not something we have today and is also not something I really want to start introduce to the language since it is going to be a major pain with e.g. imports.

I also agree with modulovalue that is is limited how much space you are going to save. But the main point is really that with a switch statement, the order of the checks are easy to observe compared to this new syntax where it becomes a lot harder.

Another problem is then also going to be IDE support where you today can click on a method and go to the declaration. How would that work with this new syntax?

water-mizuu commented 1 year ago

I think a problem with this solution is going to be that the order of your function declarations now matter, which is not something we have today and is also not something I really want to start introduce to the language since it is going to be a major pain with e.g. imports.

I also agree with modulovalue that is is limited how much space you are going to save. But the main point is really that with a switch statement, the order of the checks are easy to observe compared to this new syntax where it becomes a lot harder.

Another problem is then also going to be IDE support where you today can click on a method and go to the declaration. How would that work with this new syntax?

Honestly, this wasn't really a well thought idea, I didn't even consider the fact that declarations now have order. However, I think the syntax would be a nice basis for future destructuring of parameters?

julemand101 commented 1 year ago

@water-mizuu Not sure about the syntax is good for parameter destructuring but I would recommend having that discussion in the following issue: https://github.com/dart-lang/language/issues/3001