Search code examples
arraystypesjuliauser-defined-types

User defined type in Julia for "marking" purposes


In my code I need to keep track of two kinds of data items, namely the original data items and the transformed data items. Both original and transformed data items I store as Array{Float64,1}. However, I need to keep track if a data item is a transformed one or not as some functions in my code work with the original data items and some with the transformed ones.

In order to ensure correctness and avoid ever passing a transformed data item to a function that is supposed to work with the original data item, I was thinking that I could create a type called Transformed. This type could be used in the function declarations thus enforcing correctness.

Of course I looked in the documentation, but this did not help me enough. I guess what I need to do is something like:

primitive type Transformed :< Array{Float64,1} end

but of course this doesn't work (it is not even a primitive!)

Do I have to go for a struct? any suggestions? Cheers.


Solution

  • Yes, you likely want a struct or a mutable struct containing an Array{Float64, 1}. You can learn more about these in the Julia manual section on composite types (i.e. structs): https://docs.julialang.org/en/stable/manual/types/#Composite-Types-1

    A simple example would be:

    struct Transformed
      data::Array{Float64, 1}
    end