Fortran to Julia with a nested struct

Don’t use mutable struct, use struct. The problem is that a3::n_t for a mutable struct is (loosely) analogous to struct nt *a3 in C.

The reason is that if you do something like:

m = m_t()
x = m.a3 # a "pointer" to the same object as m.a3, not a copy!
x.b1 = 2

then you need to get m.a3.b1 == 2 — this is what it means to have a mutable object, and it requires mutable-struct fields to be references (“pointers”), not stored inline as in a nested C struct or nested immutable struct.

Then, when you call the Fortran routine, construct an object mref = Ref{m_t}() to pass to the ccall, after which you can run m = mref[] to extract the struct data.

3 Likes