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

Need clarification on optional parameters without an explicit default value #352

Closed
DartBot opened this issue Nov 5, 2011 · 3 comments
Closed
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). P1 A high priority bug; for example, a single project is unusable or has many test failures

Comments

@DartBot
Copy link

DartBot commented Nov 5, 2011

This issue was originally filed by [email protected]


I may just need to be pointed to the right area in the spec - but I couldn't figure this out on a quick read this morning. I'm trying to understand the precise meaning of "default value is specified in the signature".

What is the difference between:

  void m([a]) and void m([a=null])?

In dart code and from observing the VM, these two methods appear to have the same runtime behavior of marking 'a' as optional and using a default value of null when calling 'm' with zero arguments.

However, these two declarations appear to be treated differently by the VM for errors. Specifically, the rules in the spec of the form: "It is a compile-time error if any default values are specified in the signature of an abstract method." and similar ones appear to treat the two signatures above differently where void m([a]) is not considered to be specifying a default value, but void m([a=nlll]) is.

Is the VM's current behavior correct? If so, what is the meaning of the optional parameters on an abstract method? Also, is the current behavior (which feels very useful to me) or treating an optional parameter without an explicit default value as having a default value of null correct?

tests/language/src/NamedParameters8NegativeTest is a good example of this. As written, it produces the expected error from the VM. However, if the '= null' is removed, it will compile and run - producing instead the runtime error on Expect.equals(true, false);

Thanks for helping to clarify this - Jim

@gbracha
Copy link
Contributor

gbracha commented Nov 8, 2011

The VM's behavior is the intended one. A default is specified if an optional parameter is followed by = e for some (compile-time constant) expression e. Otherwise it is not specified, and an implicit default of null is given. I'll check to see if the wording could be improved.

@gbracha
Copy link
Contributor

gbracha commented Nov 9, 2011

I've added a bit of text that clarifies this (I hope).


Set owner to @gbracha.
Added Done label.

@DartBot
Copy link
Author

DartBot commented Nov 9, 2011

This comment was originally written by [email protected]


Thanks for clarifying! I think I've got this now and will let you know if I hit further issues.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). P1 A high priority bug; for example, a single project is unusable or has many test failures
Projects
None yet
Development

No branches or pull requests

3 participants