2

In a function with optional parameters, I want to print all the parameters passed in, not just value but also identifier of each parameter. For example, below is a function with optional named parameters. I print out both arg name and its value using a map instance, that's what I need.

Map temp = new Map();

argumentTest({String a, String b, String c: "DDD"}){
  if (a != null) {
    temp.addAll({"a":a});
  }

  if (b != null) {
    temp.addAll({"b":b});
  }

  if (c != null) {
    temp.addAll({"c":c});
  }
}

void main() {
  argumentTest(a:"AAA");
  print('$temp'); //{a: AAA, c: DDD}

}   

Though ugly, it works. But, is there anything which looks like;

Map temp = new Map();

argumentTest({String a, String b, String c: "DDD"}){
  arguments.forEach((arg) => temp.addAll({"arg": arg}));
}

void main() {
  argumentTest(a:"AAA");
  print('$temp'); //of course, not working.
}

Thank you all and always.

2
  • You could use the reflect library. Commented Sep 14, 2014 at 13:02
  • Actually dart:mirrors but I have never seen a method to access local variables using mirrors only fields and methods/functions. There is no way to know which parameters where passed. You can only check if a parameter value is null or the default value you specified but this value could also have been passed by the caller. Commented Sep 14, 2014 at 13:17

3 Answers 3

4

You could simplify your workaround

Map temp = new Map();

argumentTest({String a, String b, String c: "DDD"}) {
  ({'a': a, 'b': b, 'c': c}).forEach((k, v) {
    if(v != null) temp[k] = v;
  });
}

to support default values different from null you need to extend a little - for example like

argumentTest({String a, String b, String c: "DDD"}) {
  ({
      'a': [a, null], 'b': [b, null], 'c': [c, "DDD"]
  }).forEach((k, v) {
    if(v[0] != v[1]) temp[k] = v[0];
  });
}
Sign up to request clarification or add additional context in comments.

5 Comments

I have shortened it further. You should be aware that it doesn't check whether c was passed because c gets the value DDD if it is omitted by the caller but you had the same code in your workaround.
much more elegant. Thank you Mr.Zöchbauer. But, could you explain the second version a bit more?
If a parameter is omitted by the caller the default value is assigned to the parameter (if no default value is specified the default value is null). My second version creates a map with the parameter name as key and as value a list containing the actual parameter value and the default value. The forEach doesn't simply compare to null but checks if the parameter value differs from the default value (v[0] is the actual value, v[1] is the default value). It is bit cumbersome because the default value has to be maintained twice - in the argument list and in the list assigned to the map.
I see, the second one removes default value from the map being created. :)
The second one only adds these arguments to temp where the actual value differs from the default value (I thought this was the intention of the example in your question). This is the closest you can get when you try to figure out which parameters were passed - at least as far as I know.
2

There is no such thing in Dart. However, if you want to build mock objects for automated tests, you can take a look at this article, which shows how to use the mock package

Comments

1

I don't believe this is possible (even using mirrors). The names of your variables will be changed during minification. The "messy" coder you have is really the only way of preserving them in a way you can access like this.

1 Comment

Minification doesn't prevent reflection but the mirrors library doesn't provide any functionality for this use case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.