Julia v0.6构造函数和'new'关键字

pet*_*eld 4 types julia

我对Julia中的参数化复合类型(结构)感到困惑.我正在使用v0.6.我想知道是否有人可以向我解释这两段代码之间的区别?第一个似乎工作,但第二个给出错误(ERROR: LoadError: syntax: too few type parameters specified in "new{...}").我特别困惑了:

  • 例如Point{G}(x,y) where {G<:Integer} = new(55,y),第一个Point定义中的内容是什么.它似乎不是一种方法?见println(methods(Point))几行之后.它是'构造函数'吗?如果是这样,那么构造函数和方法之间的区别是什么?
  • 关键字new真的有用吗?
  • 为什么第二块代码无效?

`

workspace()
println("\nSTART")

struct Point{T<:Real}
    x::T
    y::T
    # These are not 'methods' - see below. What are they!?
    Point{G}(x,y) where {G<:Integer} = new(55,y)
    Point{H}(x,y) where {H<:AbstractFloat} = new(x, 11)
end
println(methods(Point)) # Interesting!!!

# Are these methods? Are they 'constructors'?
Point(x::T, y::T) where {T<:Integer} = Point{T}(x,y)
Point(x::T, y::T) where {T<:AbstractFloat} = Point{T}(x,y)
println(methods(Point))

p = Point(2,3)
p2 = Point(2.0,3.0)

##########################################################################

# Errors!
workspace()
println("")

struct Point{T<:Real}
    x::T
    y::T
    Point(x::T, y::T) where {T<:Integer} = new(55, y)
    Point(x::T, y::T) where {T<:AbstractFloat} = new(x, 11)
end
println(methods(Point))

p = Point(2,3)
p2 = Point(2.0,3.0)
Run Code Online (Sandbox Code Playgroud)

`

crs*_*nbr 5

我想你应该阅读Julia文档的这一部分.

您的问题的简短答案:

1)一种方法可以做任何事情.构造函数创建一个对象.(如果你愿意,这是一种特殊的方法)

2)它用于在类型定义中创建对象.

3)使用new{T}(55,y)而不是new(55,y).

第一个案例

Point{G}(x,y) where {G<:Integer} = new(55,y)在第一种情况下是一个内部构造函数.内在因为它存在于类型定义中.构造函数总是存在,即使你注释掉这些行,你仍然可以做Point{Int64}(3,2).所以你实际做的是通过明确告诉Julia如何为特定情况做什么来覆盖默认构造函数G<:Integer,即始终设置x55.在new(55,y)随后实际上创建了一个Point{G}与对象x=55y=y.(请注意,如果你要编写Point{G}(x,y) where {G<:Integer} = Point{G}(55,y),我们会有一个循环,所以new()确实需要这样的东西.)从技术上讲,你应该new{G}(55,y)用来创建Point{G}对象,但Julia足够聪明,可以自动G从lhs上的构造函数中获取.这将是案例2中的问题.

的输出

julia> println(methods(Point)) # Interesting!!!
# 1 method for generic function "(::Type)":
(::Type{T})(arg) where T in Base at sysimg.jl:77
Run Code Online (Sandbox Code Playgroud)

告诉您,例如,只需将类型名称(包括类型参数)作为函数名称即可调用构造函数Point{Float64}(1.2, 3.4).我不知道为什么Julia没有明确列出不同的构造函数.

# Are these methods? Are they 'constructors'?
Point(x::T, y::T) where {T<:Integer} = Point{T}(x,y)
Point(x::T, y::T) where {T<:AbstractFloat} = Point{T}(x,y)
println(methods(Point))
Run Code Online (Sandbox Code Playgroud)

这些是方法.对此的一个指示是你可以用不同的名字命名:create_a_point(x::T, y::T) where {T<:Integer} = Point{T}(x,y).你不能为(内部)构造函数执行此操作.但是,有时它们被称为"构造方法".

第二种情况

您必须明确指定类型参数:new{T}(55,y).与案例1不同,Julia可以自动使用的lhs上没有给出类型参数.

也许对朱莉娅有更多技术知识的人可以提供更准确的答案.但在那之前,我希望这会有所帮助.