Skip to content

main can take up to two arguments #18812

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

Closed
peter-ahe-google opened this issue May 14, 2014 · 9 comments
Closed

main can take up to two arguments #18812

peter-ahe-google opened this issue May 14, 2014 · 9 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@peter-ahe-google
Copy link
Contributor

From https://www.dartlang.org/docs/spec/latest/dart-language-specification.html#h.ey3rvr2kjush:

"Then, the top level function main that is in the exported namespace of S is invoked with no arguments. It is a run time error if S does not declare or import a top level function main."

However, dart2js and the VM agree that the method can be passed up to two arguments. I'm not sure exactly what they are, which is why I looked at the specification.

@peter-ahe-google
Copy link
Contributor Author

I suggest a rule along these lines:

Given these typedefs:

typedef ZeroParameters();

typedef OneParameter(a);

If main is a top-level function and a subtype of OneParameter, it is invoked with one argument whose runtime type is a subtype of List<String>.

Otherwise, if main is a top-level function and a subtype of ZeroParameters, it is invoked with no arguments.

Otherwise, it is a static (type?) warning and a runtime error.

This leads to this test case:

main(

a /// 01: ok
a, b /// 02: static type warning, runtime error
a, b, c /// 03: static type warning, runtime error
a, b, {c} /// 04: static type warning, runtime error
a, b, [c] /// 05: static type warning, runtime error

[a] /// 20: ok
a, [b] /// 21: ok
[a, b] /// 22: ok

{a} /// 41: ok
a, {b} /// 42: ok
{a, b} /// 43: ok
[a, b, c] /// 44: ok
{a, b, c} /// 45: ok

) {
}

Right now, dart2js fails most of these cases (it reports compile time errors).
The VM fails cases 02-05.

I have a CL which makes dart2js behave as described above.


cc @larsbak.
cc @iposva-google.
cc @bwilkerson.

@peter-ahe-google
Copy link
Contributor Author

Update: the VM doesn't fail 03.

@peter-ahe-google
Copy link
Contributor Author

cc @johnniwinther.

@gbracha
Copy link
Contributor

gbracha commented May 14, 2014

Ok, the latest spec has a fix for this.


Added Done label.

@peter-ahe-google
Copy link
Contributor Author

I took a look at the latest spec, it says:

"First, $S$ is compiled as a library as specified above. Then, the top-level function \code{main} that is in the exported namespace of $S$ is invoked. If \code{main} has no formal parameters, it is invoked with no arguments. Otherwise \code{main} is invoked with a single actual argument whose type implements \code{List$<$String$>$}. It is a run time error if $S$ does not declare or import a top-level function \code{main}. It is a static warning if \code{main} has more than one required parameter."

That seems to match what I said in comment #­1.

Since then I have learned why dart2js and the VM accepts a second parameter to main.

This is used from Isolate.spawnUri https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-isolate.Isolate#id_spawnUri

My comment #­1 can be extended to handle the second parameter:

Given these typedefs:

typedef ZeroParameters();

typedef OneParameter(a);

typedef TwoPwarmeters(a,b);

If main is a top-level function and a subtype of TwoParameter, it is invoked with two arguments, the first argument's runtime type is a subtype of List<String>, the second argument depends on the environment.

Rationale: see Isolate.spawnUri.

Otherwise, if main is a top-level function and a subtype of OneParameter, it is invoked with one argument whose runtime type is a subtype of List<String>.

Otherwise, if main is a top-level function and a subtype of ZeroParameters, it is invoked with no arguments.

Otherwise, it is a static (type?) warning and a runtime error.


cc @lrhn.
cc @vsmenon.
cc @sigmundch.
Added Triaged label.

@peter-ahe-google
Copy link
Contributor Author

Marked this as blocking #5683.

@gbracha
Copy link
Contributor

gbracha commented May 15, 2014

Ok, I've revised the spec to account for the second arguments as well. See

https://codereview.chromium.org/280473004/


Added Accepted label.

@peter-ahe-google
Copy link
Contributor Author

Unmarked this as blocking #5683.

@gbracha
Copy link
Contributor

gbracha commented Jul 14, 2014

Added Done label.

@peter-ahe-google peter-ahe-google added Type-Defect area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jul 14, 2014
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).
Projects
None yet
Development

No branches or pull requests

4 participants