Types Julia v0.6构造函数和';新';关键词

Types Julia v0.6构造函数和';新';关键词,types,julia,Types,Julia,Julia中的参数复合类型(结构)让我很困惑。我使用的是v0.6。我想知道是否有人能向我解释这两位代码之间的区别?第一个似乎有效,但第二个给出了错误(ERROR:LoadError:syntax:new{…}中指定的类型参数太少)。我特别感到困惑的是: 例如,{g}(x,y)点,{g我认为你应该读什么 对您的问题的简短回答: 1) 方法做任何事情。构造函数创建一个对象。(如果你愿意,它是一个特殊的方法) 2) 它用于在类型定义中创建对象 3) 使用new{T}(55,y)代替new(55,y)

Julia中的参数复合类型(结构)让我很困惑。我使用的是v0.6。我想知道是否有人能向我解释这两位代码之间的区别?第一个似乎有效,但第二个给出了错误(
ERROR:LoadError:syntax:new{…}
中指定的类型参数太少)。我特别感到困惑的是:

  • 例如,{g}(x,y)点,{g我认为你应该读什么

    对您的问题的简短回答:

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

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

    3) 使用
    new{T}(55,y)
    代替
    new(55,y)

    第一例


    你的
    点{G}(x,y){G感谢你,这非常有帮助。是的,我读了文档的那一部分,但是你的回答帮助我把事情弄清楚了很多!
    
    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)
    
    julia> println(methods(Point)) # Interesting!!!
    # 1 method for generic function "(::Type)":
    (::Type{T})(arg) where T in Base at sysimg.jl:77
    
    # 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))