new() for Julia Arrays

Viewed 71

I struggle with writing a proper constructor for my struct PERK. What gives my trouble is the usage of new() with my member arrays. The code looks like this:

abstract type PERKTYPE end

struct PERK <: PERKTYPE
    NumStages::Int

    ACoeffs::Array{BigFloat}
    c::Array{BigFloat}
  
    function PERK()

      ACoeffsFile = open("some/existing/file.txt", "r")
      NumStages = countlines(ACoeffsFile)
      close(ACoeffsFile)
      #println(NumStages)

      ACoeffs = Array{BigFloat}(undef, NumStages, 2)
      
      # Fille ACoeffs with data from file, omitted here

      c = Array{BigFloat}(undef, NumStages)
      for i in 1:NumStages
        c[i] = (i-1)/(2*(NumStages-1))
      end

      new(NumStages) # Fine
      new(ACoeffs, c) # Not working if un-commented

    end # PERK()
  end # struct PERK

I get the error

ERROR: LoadError: MethodError: Cannot `convert` an object of type Matrix{BigFloat} to an object of type Int64
Closest candidates are:
  convert(::Type{T}, ::LLVM.GenericValue) where T<:Signed at ~/.julia/packages/LLVM/gE6U9/src/execution.jl:27
  convert(::Type{T}, ::LLVM.ConstantInt) where T<:Signed at ~/.julia/packages/LLVM/gE6U9/src/core/value/constant.jl:89
  convert(::Type{T}, ::Ptr) where T<:Integer at ~/Software/julia-1.7.2/share/julia/base/pointer.jl:23
  ...
Stacktrace:
 [1] PERK()
   @ Trixi ~/.../methods_PERK.jl:26

What is going on here? Of course, I am not interested in any conversion of my arrays at all.

1 Answers

I can be wrong here, but new() creates an PERK struct. Meaning a struct with three arguments. If you call new() with just a NumStages it is fine, because it is the first argument of the struct. However

new(ACoeffs, c)

tries to feed an array ACoeffs into NumStages which yields the error you see. You can overcome this using

new(NumStages, ACoeffs, c)

Hope this helps a little.

Related