Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating function with variable number of arguments or parameters in Dart

Tags:

dart

I am looking for a way to create a function with a variable number of arguments or parameters in Dart. I know I could create an array parameter instead, but I would prefer to not do that because I'm working on a library where syntactic brevity is important.

For example, in plain JavaScript, we could do something like this (borrowed from here):

function superHeroes() {   for (var i = 0; i < arguments.length; i++) {     console.log("There's no stopping " + arguments[i]);   } }  superHeroes('UberMan', 'Exceptional Woman', 'The Hunk'); 

However, in dart, that code will not run. Is there a way to do the same thing in dart? If not, is this something that is on the roadmap?

like image 517
plowman Avatar asked Dec 05 '12 20:12

plowman


People also ask

What are required parameters in Dart?

Dart required positional parametersWe declare required positional parameters with a type and name, e.g., int: a . The name is for reference in the function, not at the call site. You specify only the value without a parameter name when calling a function. int sum(int a, int b) { // 1.

What is function type in Dart?

There are four main types of user define functions (based on arguments and return type). Function with no arguments and no return type. Function with arguments and no return type. Function with no arguments and return type. Function with arguments and with return type.

What is function keyword in Dart?

Dart function is a set of codes that together perform a specific task. It is used to break the large code into smaller modules and reuse it when needed. Functions make the program more readable and easy to debug. It improves the modular approach and enhances the code reusability.


2 Answers

You can't do that for now.

I don't really know if varargs will come back - they were there some times ago but have been removed.

However it is possible to emulate varargs with Emulating functions. See the below code snippet.

typedef OnCall = dynamic Function(List arguments);  class VarargsFunction {   VarargsFunction(this._onCall);      final OnCall _onCall;    noSuchMethod(Invocation invocation) {     if (!invocation.isMethod || invocation.namedArguments.isNotEmpty)       super.noSuchMethod(invocation);     final arguments = invocation.positionalArguments;     return _onCall(arguments);   } }  main() {   final superHeroes = VarargsFunction((arguments) {     for (final superHero in arguments) {       print("There's no stopping ${superHero}");     }   }) as dynamic;   superHeroes('UberMan', 'Exceptional Woman', 'The Hunk'); } 
like image 171
Alexandre Ardhuin Avatar answered Oct 09 '22 01:10

Alexandre Ardhuin


Dart does indirectly support var-args as long as you as you aren't too much into syntactic brevity.

void testFunction([List<dynamic> args=[]]) {   for(dynamic arg:args)   {     // Handle each arg...   } }  testFunction([0, 1, 2, 3, 4, 5, 6]); testFunction(); testFunction([0, 1, 2]); 

Note: You can do the same thing with named parameters, but you'll have to handle things internally, just in case if the user (of that function; which could be you) decides to not pass any value to that named parameter.


I would like to thank @Ladicek for indirectly letting me know that a word like brevity exists in English.

like image 33
Son of Stackoverflow Avatar answered Oct 09 '22 00:10

Son of Stackoverflow