language icon indicating copy to clipboard operation
language copied to clipboard

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

Open projektorius96 opened this issue 1 year ago • 4 comments

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 avatar Apr 30 '24 22:04 projektorius96

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.

mmcdon20 avatar Apr 30 '24 23:04 mmcdon20

See #1295

tatumizer avatar May 01 '24 03:05 tatumizer

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).

lrhn avatar May 01 '24 06:05 lrhn

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.

tatumizer avatar May 02 '24 13:05 tatumizer