如何从属性破坏的属性中获取价值?
使用
getProperty()我可以
以这种方式解开我的
struct
:
struct Person
name::String
age::Int32
end
amber = Person("Amber",22)
name = getproperty(amber,:name)
println(name) # Output: Amber
当我这样做时我无法捕获结果:
(; name, age) = amber
如何仅打印 name
?我在文档中找不到它。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
或者您可以使用参数软件包:
总有最简单的方法:
You could do:
Or there you could be
using Parameters
package:And there is always the easiest way:
您所做的是破坏
Amber
的属性 name 和age
,所以直接使用它们:
它不同于
getProperty()
方法,分2种方法:当您将
name
传递到getProperty()
时,您'经过一个符号
:您不必创建不必要的变量即可获得结果:
它是在GitHub上,该手册要更新以包括属性
破坏分配和多个返回值。
这很幽默,因为阅读了链接的文档后,我决定尝试
使用
struct
而不是Itable
进行破坏的示例,它起作用了!我进行了一些搜索,发现此 example 在github上。
What you've done is destructure the properties of
amber
intoname
andage
, so justuse them directly:
It differs from the
getproperty()
method in 2 ways:When you pass
name
intogetproperty()
, you're passing asymbol
:You don't have to create an unnecessary variable to get the result:
It was requested on Github that the manual be updated to include property
destructuring right after Destructuring Assignment and Multiple Return Values.
This is humorous, because after reading the linked documentation, I decided to try
the example of destructuring using a
struct
instead of aiterable
, and it worked!I did some searching and found this example on Github.