Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart lambda/shortland function confusion

Tags:

dart

I'm still pretty new to Dart and the syntax of => (fat arrow) still confuses me (I come from C# background).

So in C# fat arrow ( => ) says: goes to so for example:

Action<string> action1 = (str) => { System.Diagnostic.Debug.WriteLine("Parameter received: " + str.ToString()); }  action1("Some parameter"); 

means: whatever send as parameter to action1 (if it could be casted to string) goes to inner scope (in our case it just printed in Debug.WriteLine()

but in Dart it's something different.... (?)

for example in Future.then

ClassWithFutures myClass = new ClassWithFutures(); myClass.loadedFuture.then(     (str) => { print("Class was loaded with info: $str"),    onError: (exp) => { print("Error occurred in class loading. Error is: $exp"); } ); 

Dart editor warn me that the first and second print is: Expected string literal for map entry key. I think in C# way that str it just name for parameter that will be filled by internal callback that Future.then uses to call onValue or onError

What I'm doing wrong ?

like image 754
Alex F Avatar asked Apr 04 '13 06:04

Alex F


People also ask

What does () => mean in DART?

In Dart, we have fat arrow notation ( => ). A fat arrow is used to define a single expression in a function. This is a cleaner way to write functions with a single statement.

What is lambda function in DART?

Lambda is a short and concise manner to represent small functions. Lambda functions are also called Arrow functions. But here remember that by using Lambda function's syntax you can only return one expression. It must be only one line expression.

Are arrow functions lambdas?

Arrow functions are also known as lambda functions.

What is Arrow function in DART?

Dart uses the fat arrow notation ( => ) to define a single expression in a function. It is a neat method to write a single-line function expression.


2 Answers

You need to choose either block syntax or single expression syntax, but not both.

You can't combine => with {}

Your two options are as follows using your example:

ClassWithFutures myClass = new ClassWithFutures(); myClass.loadedFuture.then(    (str) => print("Class was loaded with info: $str"),   onErrro: (exp) => print("Error occurred in class loading. Error is: $exp") ); 

or

ClassWithFutures myClass = new ClassWithFutures(); myClass.loadedFuture.then(    (str) { print("Class was loaded with info: $str"); },   onErrro: (exp) { print("Error occurred in class loading. Error is: $exp"); } ); 

In both cases, it is just a way to express an anonymous function.

Normally if you want to just run a single expression, you use the => syntax for cleaner and more to the point code. Example:

someFunction.then( (String str) => print(str) ); 

or you can use a block syntax with curly braces to do more work, or a single expression.

someFunction.then( (String str) {   str = str + "Hello World";   print(str); }); 

but you can't combine them since then you are making 2 function creation syntaxes and it breaks.

Hope this helps.

like image 112
Daegalus Avatar answered Sep 28 '22 16:09

Daegalus


In Dart => xxx is just a syntaxic sugar to avoid { return xxx; }. Thus the two following functions are equivalent :

var a = (String s) => s; var b = (String s) { return s; } ; 

You can also use => on method definitions :

String myFunc(String s) => s; String myFunc(String s) {   return s; } 
like image 20
Alexandre Ardhuin Avatar answered Sep 28 '22 16:09

Alexandre Ardhuin