中构建非默认构造函数

中构建非默认构造函数

本文介绍了在 Julia 中构建非默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I build a constructor in Julia with fewer inputs than values? I have an Int64 array of numbers where each number represents 24 boolean values. The best situation would be that I could send in the array and get back a composite type with arrays of each component. Here is the code I've tried.

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 值数组.

This results in a no method convert(Type{Bool},Array{Bool,1}) error. I tried to instantiate it with statuses = Status(StatusW) where StatusW is an Int64 array of values.

有用的参考:类型构造函数部分 Julia 文档

推荐答案

声明需要如下.

Valve1::Vector{Bool}

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

Another factor contributing to my confusion was that new(Valve1,...) should be the last thing in the constructor. I had added debugging println()lines after the new(Valve1,...) causing the type to return Nothing.

Julia Google Groups 论坛上的 Tim Holy 提供了解决方案.

Tim Holy on the Julia Google Groups forum provided the solution.

完整的示例应该如下所示.

The full example should look like this.

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 中构建非默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:28