参数构造函数
我遇到了一些问题,了解朱莉娅的参数构造函数的概念。我正在查看Julia文档中的标准示例:
struct Point{T<:Real}
x::T
y::T
end
就我的理解,这意味着我可以生成一个点数,其输入是真实的,即IE,AbstractFloat,Abstractirrational,...,Integer,Integer,Rational,.. .. 。,statsbase.teststat。
但是,以下两个示例都会导致错误:
Point(Integer(12))
Point(Rational(12))
为什么上述失败鉴于整数和理性都是真实的亚型?
I am having some issues understanding the concept of parametric constructors in Julia. I am looking at the standard example in the Julia docs:
struct Point{T<:Real}
x::T
y::T
end
To my understanding, this means I can generate a Point-datatype with an input that is subtype of Real, i.e., AbstractFloat, AbstractIrrational, ..., Integer, Rational, ..., StatsBase.TestStat.
However, both of the examples below result in errors:
Point(Integer(12))
Point(Rational(12))
Why does the above fail given that both integer and rational are subtypes of real?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像在
struct
定义:所提供的参数是struct IE
x
中的值 一样和y
。如果参数已经是您想要的类型,则可以明确指定类型参数:The type parameter goes inside the curly braces in the constructor call, just like it does in the
struct
definition:The arguments supplied are the values for the fields of the struct i.e.
x
andy
. If the arguments are already of the type you want, you can leave out explicitly specifying the type parameter: