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

Dart VM lacks of "arguments" object passed to a Function #3742

Open
projektorius96 opened this issue Apr 30, 2024 · 4 comments
Open

Dart VM lacks of "arguments" object passed to a Function #3742

projektorius96 opened this issue Apr 30, 2024 · 4 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@projektorius96
Copy link

projektorius96 commented Apr 30, 2024

DISCLAIMER: If any of below does not conform to code of conduct, I am willing to adjust (edit) in order to meet the level or requirements expected;


In JavaScript we have arguments array-like object (ostensible List in Dart) that at run-time evaluates and returns list of arguments passed to a function (if any), consider the example as follows:

// Example 1

function getFullName(first, last) {
    return ()=>console.log(...arguments)
}

getFullName("John", "Doe")() // it prints # John Doe

As far as I do understand, Dart currently does not have anything similar to arguments due to its nature of fixed-length list of literal arguments passed to a function (if any) and MUST be evaluated at compile-time, yet I would like to request for comments on this matter considering the modified Example 1, but for Dart, whereas we are re-using keyword late asking Dart VM to evaluate arguments at run-time, rather than compile-time, the example as follows:

// Example 1 (modified for Dart)

Function getFullname(name, surname) {
  late List arguments;

  /// herein iterables' [.first] and [.last] works just fine in tandem with our passed [name] and [surname] respectively, however use cases can and will vary;
  /// the return statement itself is potential closure, e.g. some internal printer function we just wanted to forward [arguments] for/to.
  return () => print('${arguments.first} ${arguments.last}');
}

void main() {
  getFullname("John", "Doe")(); // expectation would be the same, i.e. it prints # John Doe
}

Some of you might write this off at first glance after reading this saying that the keyword late indicates a variable that MUST be non-nullable as per Dart language spec., and yes we could make it non-nullable enforcing the Dart's VM to assign an empty List, instead of null, in case of scenario whereas empty list of literal (none of) arguments passed whatsoever.

Thank you.

@projektorius96 projektorius96 added the feature Proposed language feature that solves one or more problems label Apr 30, 2024
@mmcdon20
Copy link

Consider this example:

void example(int a, String b, {required bool c}) {...}

How would the named parameter c be handled? Would it be included in the List somehow or would there be a separate mechanism for named parameters?

I assume arguments would be a List<dynamic> or potentially List<Object?> due to
the arguments having various unrelated types.

I think in dart if you could get an arguments object somehow it would make more sense to represent it as a record which would preserve the type information for each of the individual arguments, and support both positional and named members.

@tatumizer
Copy link

See #1295

@lrhn lrhn changed the title Dart VM lacks of "arguments" object passed to a Function [REQUEST FOR COMMENTS] Dart VM lacks of "arguments" object passed to a Function May 1, 2024
@lrhn
Copy link
Member

lrhn commented May 1, 2024

This may differ from #1295 of the goal is to get the actual arguments, not the post-default-value-assignment parameters.

I still don't see the advantage here, when one can just write

Function getFullname(name, surname) {
  late List arguments = [name, surname];

  return () => print('${arguments.first} ${arguments.last}');
}

and get the desired effect.

Unlike JavaScript, Dart doesn't allow you to pass an arbitrary number of arguments to a function with two parameters, so you always know how many arguments there will at most be.
Var-args is a different feature.

If we get a way to recognize whether an optional parameter was passed an argument or not, you can completely recreate the arguments inside the function.

So I don't see the use-case for having the arguments automatically available as a list, when you can just create the list yourself (and it should probably be s record, to not lose the typing).

@tatumizer
Copy link

If we get a way to recognize whether an optional parameter was passed an argument or not, you can completely recreate the arguments inside the function.

Yes, but the reverse is also true. It might be easier (and more straightforward) to provide $arguments as a record of the same structure as the function signature, where only the explicitly passed optional arguments have non-null values.

Which way is better, of course, depends on how "passed" flags are implemented. E.g. the method discussed in #3680 has too many limitations. Other methods might have their quirks too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

4 participants