JKRT
JKRT

Reputation: 1207

Is it possible to have "public/global" fields in Julia structs?

In Julia it is possible to have public fields in functions for instance

function foo(arg)
  global a = arg
  a
end

Is it possible to achieve something similar using Julia structures. For instance what I would like to do is:

julia> struct foobarfoo
       global a
       end

julia>

julia> test = foobarfoo(1)
ERROR: MethodError: no method matching foobarfoo(::Int64)
Stacktrace:
 [1] top-level scope at none:0

julia> a
ERROR: UndefVarError: a not defined

Instead of:

julia> struct foobarfoo
   a
   end

julia> test = foobarfoo(1)
foobarfoo(1)

julia> test.a
1

julia>

Upvotes: 0

Views: 97

Answers (1)

Korsbo
Korsbo

Reputation: 724

I think that the short answer is no but you may be able to achieve what you want using the @unpack macro of Parameters.jl.

Upvotes: 2

Related Questions