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

Lowering the requirements for default const parameters #429

Open
wrozwad opened this issue Jun 26, 2019 · 1 comment
Open

Lowering the requirements for default const parameters #429

wrozwad opened this issue Jun 26, 2019 · 1 comment

Comments

@wrozwad
Copy link

wrozwad commented Jun 26, 2019

Documentation says that

The default values must be compile-time constants

I'm curious why const values are required. I think it would be better if this requirement will be lowering a bit. It'll be awesome to be able to use previous passed arguments as default parameters or every time create a new modifiable default list in place of using const one:

void read(byte[] bytes, {int offset = 0, int length = bytes.size, byte[] avoid = []}) { 
  ...
}
@lrhn
Copy link
Member

lrhn commented Jun 27, 2019

It would be nice if an omitted parameter forced a computation instead of just inserting a constant value.
However, that comes with some complication too. The computation needs to have a scope (can it only see previous parameters? What if they are named parameters, which one are "previous" then? Is it just source order?). They will be executed at a point where you cannot be inside a try/catch, so there is no way to catch errors. You can tell whether a parameter was passed or not by having side effects:

abstract class Foo {
   int foo(int x, {int y});
}
class _SmartFoo implements Foo {
  static _yWasOmitted = false;
  int foo(int x, {int y = ($yWasOmitted = true) ? 0 : throw "unreachable"}) {
    if (_yWasOmitted) {
      _yWasOmitted = false;
      // do something special
    }
  }
}

That's something we currently do not allow, you cannot detect a difference between passing the default value directly or not passing anything. The ability to do that is problematic for methods that forward invocations.

So, to have this feature, I'd prefer if you couldn't tell the difference between omitting an optional parameter and explicitly passing null, and then binding the parameter to the default value would effectively be computed as parameter = argument ?? defaultExpression.

See #140 for a specific request.

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