Structures and DataFrames

Learning about structures. Appreciate any comments on the below

struct BASE
    a::Int64
    b::Int64
end

struct B1
    base::BASE
    b1::Int64
end

struct B2
    base::BASE
    b2::Int64
end


df = DataFrame(   type = [:B1,:B2], a=[1,1], b=[1,1], b1=[1,missing], b2=[missing,2]  )

value( x::B1 ) = x.base.a + x.b1 * 2
value( x::B2 ) = x.base.a + x.b2 * 3

Question 1
is there an easy way to put each row of df into the appropriate structure (B1, B2) ?
I’d like to have a new column of df with the structures.
then apply the value function to this column.

Question 2
is it possible to avoid the x in the value function. Something like
value( ::B1... ) = base.a + b1 * 2

Question 3
I believe putting base::BASE into B1 and B2 like this is the best way to do inheritance in Julia (please correct me otherwise).

Question 4
Is it possible to include BASE in other structures but not require a prefix base. to access the a and b parameters. Assuming the B1 parameter set is unique e.g. a, b, b1

My actual example is FX Options. I’d like to have a struct for the parameters common to all (Strike, Expiry, Settlement etc) and paste this into structs for the various types (Barrier, Digital, Asian etc).
I’d prefer not to preface the base parameters with base.

julia> @rtransform df :s = begin
           base = BASE(:a, :b)
           :type == ^(:B1) ? B1(base, :b1) : B2(base, :b2)
       end
2Ɨ6 DataFrame
 Row │ type    a      b      b1       b2       s                 
     │ Symbol  Int64  Int64  Int64?   Int64?   Any               
─────┼───────────────────────────────────────────────────────────
   1 │ B1          1      1        1  missing  B1(BASE(1, 1), 1)
   2 │ B2          1      1  missing        2  B2(BASE(1, 1), 2)

No.

1 Like

thanks Peter. helpful as always:)