julia - 在 Julia 中构建非默认构造函数

标签 julia

如何在 Julia 中构建输入少于值的构造函数?我有一个 Int64 数字数组,其中每个数字代表 24 个 bool 值。最好的情况是我可以发送数组并取回包含每个组件数组的复合类型。这是我试过的代码。

type Status
   Valve1::Array{Bool}
   Valve2::Array{Bool}
   Valve3::Array{Bool}
   Valve4::Array{Bool}
   Valve5::Array{Bool}
   Valve6::Array{Bool}
   Valve7::Array{Bool}
   Valve8::Array{Bool}

   # Constructor for Status type
   function Status(vals::Array{Int64})
   l = int64(length(vals))

   Valve1 = Array(Bool,l)
   Valve2 = Array(Bool,l)
   Valve3 = Array(Bool,l)
   Valve4 = Array(Bool,l)
   Valve5 = Array(Bool,l)
   Valve6 = Array(Bool,l)
   Valve7 = Array(Bool,l)
   Valve8 = Array(Bool,l)

   # Parse Inputs
   for i=1:l
      # Byte 1
      Valve1[i] = vals[i] & 2^(1-1) > 0
      Valve2[i] = vals[i] & 2^(2-1) > 0
      Valve3[i] = vals[i] & 2^(3-1) > 0
      Valve4[i] = vals[i] & 2^(4-1) > 0
      Valve5[i] = vals[i] & 2^(5-1) > 0
      Valve6[i] = vals[i] & 2^(6-1) > 0
      Valve7[i] = vals[i] & 2^(7-1) > 0
      Valve8[i] = vals[i] & 2^(8-1) > 0
   end # End of conversion

   new(Valve1,Valve2,Valve3,Valve4,Valve5,Valve6,Valve7,Valve8)

   end # End of constructor
end # End of type

这会导致 no method convert(Type{Bool},Array{Bool,1}) 错误。我尝试用 statuses = Status(StatusW) 实例化它,其中 StatusW 是一个 Int64 值数组。

有用的引用资料:TypesConstructorsJulia documentation

最佳答案

声明需要如下。

Valve1::Vector{Bool}

造成我困惑的另一个因素是 new(Valve1,...) 应该是构造函数中的最后一个东西。我在 new(Valve1,...) 之后添加了调试 println() 行,导致类型返回 Nothing

Julia Google Groups 论坛上的 Tim Holy 提供了 solution .

完整示例应如下所示。

type Status
    Valve1::VectorBool}
    Valve2::Vector{Bool}
    Valve3::Vector{Bool}
    Valve4::Vector{Bool}
    Valve5::Vector{Bool}
    Valve6::Vector{Bool}
    Valve7::Vector{Bool}
    Valve8::Vector{Bool}

    # Constructor for Status type
    function Status(vals::Array{Int64})
        l = int64(length(vals))

        Valve1 = Array(Bool,l)
        Valve2 = Array(Bool,l)
        Valve3 = Array(Bool,l)
        Valve4 = Array(Bool,l)
        Valve5 = Array(Bool,l)
        Valve6 = Array(Bool,l)
        Valve7 = Array(Bool,l)
        Valve8 = Array(Bool,l)

        # Parse Inputs
        for i=1:l
            # Byte 1
            Valve1[i] = vals[i] & 2^(1-1) > 0
            Valve2[i] = vals[i] & 2^(2-1) > 0
            Valve3[i] = vals[i] & 2^(3-1) > 0
            Valve4[i] = vals[i] & 2^(4-1) > 0
            Valve5[i] = vals[i] & 2^(5-1) > 0
            Valve6[i] = vals[i] & 2^(6-1) > 0
            Valve7[i] = vals[i] & 2^(7-1) > 0
            Valve8[i] = vals[i] & 2^(8-1) > 0
        end # End of conversion

        new(Valve1,Valve2,Valve3,Valve4,Valve5,Valve6,Valve7,Valve8)

    end # End of constructor
end # End of type

关于julia - 在 Julia 中构建非默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21992422/

相关文章:

julia - 用多维数组求解微分方程有没有更快的方法

python - 什么是 julia 中的 ".=="以及它在 python 中的等价物?

julia - 如何在Julia中实现二叉搜索树?

visual-studio-code - 如何在 Julia 中开发包时调试单元测试

julia - Julia 中的数组/列表操作

dataframe - 加入多个数据帧

linux - 在 Julia (Linux) 中获取当前用户名

terminal - Jupyter 笔记本中的 Julia .jl

julia - Julia 中两个向量的笛卡尔积

julia - 如何将字符串转换为数组?