Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packing/Unpacking arguments in Dart [duplicate]

Tags:

dart

Is there a way in Dart to pack/unpack arguments in a function (like Python for example) ?

For packing example, being able to declare a function like this :

packArguments(*listOfArguments, **mapOfArguments) {
    listOfArguments.forEach((arg) => print(arg));
    mapOfArguments.forEach((key, val) => print("$key => $val"));
}

And then doing this :

packArguments("I", "Put", "whatever", "I", "want, arg1: "A", arg2: 1);

Would display :

I
Put
whatever
I
want
arg1 => A
arg2 => 1

As for unpacking, being able to do something like that :

functionWithLotOfArgument(a, b, c, d, e, {aa, bb = null, cc = null}) {
  // do stuff
}
var argList = [1, 2, 3, 4, 5];
var argMap = {"aa": "haha", bb: "baby"};

functionWithLotOfArgument(*argList, **argMap);

Related issue https://github.com/dart-lang/sdk/issues/29087

like image 791
aelayeb Avatar asked Oct 17 '22 15:10

aelayeb


1 Answers

This is not currently supported, but you can very easily pack yourself, by passing a list & map:

void packArguments(List listOfArguments, Map mapOfArguments) {
    listOfArguments.forEach((arg) => print(arg));
    mapOfArguments.forEach((key, val) => print("$key => $val"));
}

void main() {
  packArguments(['a', 3], {'arg1': 'a', 'arg2': 5});
}

https://dartpad.dartlang.org/98ed3a3b07a2cca049cde69ca50ca269

like image 198
rkj Avatar answered Oct 21 '22 09:10

rkj