为什么(返回)和返回不同?

猫娘

我正在测试一个函数,并且在尝试解决我return遇到的问题,在PowerShell 5.1和PwSh 7.1中偶然发现,该returncmdlet在一组中似乎不起作用:

PS C:\Users\Neko> return "test"
test
PS C:\Users\Neko> (return "test")
return : The term 'return' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:1 char:2
+ (return "test")
+  ~~~~~~
    + CategoryInfo          : ObjectNotFound: (return:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

如果我return加入一个小组,PowerShell似乎无法识别它。

据我所知,回报打印到控制台,并没有“产出”每说一样Write-Host

PS C:\Users\Neko> $test = return "test"
test
PS C:\Users\Neko> $test
PS C:\Users\Neko> $test = Write-Host "test"
test
PS C:\Users\Neko> $test
PS C:\Users\Neko>

但我无法理解为什么return在分组表达式中出现会导致此类问题。


为什么return在组中使用会导致奇怪的错误,我该如何解决?

在子表达式中不会发生这种情况,

PS C:\Users\Neko> $(return "test")
test
成为

@Neko Musume-根据您的要求

因为按照关于该主题的MS Docs ...

About_Return | 微软文件

...只能按定义使用。意思是“退出当前范围,可以是函数,脚本或脚本块。”,因此,在尝试使用parens时,不符合该条件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么$ env:username和[environment] :: username返回不同的用户?

为什么to_char和to_date返回不同的结果

微调器,为什么返回不同的间隙和错误的值

为什么JodaTime和Calendar返回不同的结果

为什么哪个和Sys。哪个返回不同的路径?

为什么mean()和mean(aggregate())返回不同的结果?

为什么print_r和return返回不同的值?

为什么Virtuoso SPARQL端点和Jena返回的结果不同?

为什么malloc和sbrk从不同的段返回地址?

为什么any()和pd.any()返回不同的值?

为什么[]和bool返回[]?

为什么 tput 行返回不同的值?

为什么gethostbyaddr()返回不同的结果?

为什么这些查询返回不同的结果?

为什么不同的查询返回重复的值?

为什么AES函数返回不同的值?

为什么bcrypt总是返回不同的结果?

为什么strpos返回不同的结果?

为什么此函数返回不同的值?

为什么Dig返回不同的结果

为什么mysql查询返回不同的结果?

为什么java返回的编码值不同

为什么此Haskell函数的返回与定义的函数的返回不同?

为什么offsetX和offsetY在firefox和chrome中返回不同的值

为什么使用存储的嵌套命令的返回码退出会导致Dash和Bash中的返回码不同?

为什么查询集[0]和queryset.first()返回不同的记录?

为什么Controller.Json和Newtonsoft Json返回不同的结果?

当从函数以'const'返回时,为什么原始类型和用户定义类型的行为不同?

为什么Scala地图上的.map.flatten和flatMap返回不同的结果?