question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional argument can appear before non-optional parameters

See original GitHub issue

This code produces 2 warnings:

function f(a=1,b) {}
f(undefined, 3);
JSC_OPTIONAL_ARG_AT_END: optional arguments must be at the end at line 1 character 0
function f(a=1,b) {}
^
JSC_WRONG_ARGUMENT_COUNT: Function f: called with 2 argument(s). Function requires at least 0 argument(s) and no more than 1 argument(s). at line 2 character 0
f(undefined, 3);
^

https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520ADVANCED_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540formatting%2520pretty_print%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A%250Afunction%2520f(a%253D1%252Cb)%2520%257B%257D%250Af(undefined%252C%25203)%253B

And it cannot be suppressed with @suppress {functionParams}.

It is legal ES6 for a parameter with a default value to be followed by a required one, it should not be considered optional.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dimvarcommented, Mar 14, 2017

In this case, state is a required parameter whose type includes undefined. That’s fine.

When the compiler sees an unannotated function such as function f(a=1,b) {}, it needs to decide whether a is required or optional. I think that in most cases the user wants it to be optional, so we would give spurious warnings when it was meant to be required. If we considered it to be required, there would be more spurious warnings when f is called without any arguments. It’s a tradeoff. If the user wants something that is not the default compiler assumption, they can just manually annotate their code.

But I do agree that the current situation of early warning is somewhat unsatisfactory, and we should just consider the trailing parameters optional and not warn.

0reactions
dimvarcommented, Mar 14, 2017

Right.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Named and Optional Arguments - C# Programming Guide
Named arguments in C# specify arguments by name, not position. Optional arguments can be omitted.
Read more >
Using Python Optional Arguments When Defining Functions
In this tutorial, you'll learn about Python optional arguments and how to define functions with default values. You'll also learn how to create...
Read more >
Behaviour when declaring non-optional parameter after ...
The presence or absence of default values for a parameter does not in any way influence what arguments will be assigned to what...
Read more >
C# Optional Parameters: Everything You Need to Know
If we do not pass optional argument value at calling time, it uses the default value. Inside the method definition, we define a...
Read more >
"Optional" should not be used for parameters
... you have to do in the method without really increasing the value. With an Optional parameter, you go from having 2 possible...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found