Dart Lambda / Shortland函数混淆 [英] Dart lambda/shortland function confusion
问题描述
我对Dart还是很陌生,=>(胖箭头)的语法仍然让我感到困惑(我来自C#背景)。
所以在C#中粗箭头(=>)说:进入,例如:
Action< string> action1 =(str)=> {System.Diagnostic.Debug.WriteLine(接收的参数: + str.ToString()); }
action1( Some parameter);
是指:作为参数发送给 action1
的任何内容(如果可以将其强制转换为 string
)转到内部范围(在我们的示例中,它只是打印在 Debug.WriteLine中()
,但在Dart中则有所不同....(?)
例如在 Future.then
ClassWithFutures myClass = new ClassWithFutures();
myClass.loadedFuture.then(
(str)=> {print(类已加载信息:$ str),
onError :(exp)=> {print(在类加载中发生错误。错误是:$ exp);}
);
Dart编辑器警告我,第一和第二张打印
是:地图输入键的预期字符串文字
。我认为在C#中, str
只是为将由 Future.then
用于调用 onValue
或 onError
我在做什么错了吗?
您需要选择块语法或单表达式语法,但不能全部选择。
您不能将=>与{}
您的两个示例如下所示:
ClassWithFutures myClass = new ClassWithFutures();
myClass.loadedFuture.then(
(str)=> print(类已加载信息:$ str),
onErrro:(exp)=> print( Error错误是:$ exp)
);
或
ClassWithFutures myClass = new ClassWithFutures();
myClass.loadedFuture.then(
(str){print(类已加载信息:$ str);},
onErrro:(exp){print(发生错误错误是:$ exp);}
);
在两种情况下,这都是表达匿名函数的一种方式。
通常,如果您只想运行一个表达式,则可以使用=>语法来获得更整洁的代码。示例:
someFunction.then((String str)=> print(str));
或者您可以使用带有大括号的块语法来完成更多工作,或者使用单个表达式。
someFunction.then((String str){
str = str +你好世界;
print(str);
});
但是您不能将它们组合在一起,因为这时您将使用两种函数创建语法,但会中断。 / p>
希望这会有所帮助。
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 ?
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.
这篇关于Dart Lambda / Shortland函数混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!