Basic struct usage questions

I think this is relevant:
https://docs.julialang.org/en/v1/manual/interfaces/index.html#man-interface-array

in general:
https://docs.julialang.org/en/v1/manual/interfaces/index.html

1 Like

I’m not sure what you’re referring to here. Can you give an example?

There are no “private” types in Julia, and there’s no way to hide a struct definition. I’d say don’t worry about trying to hide things unless you have some really compelling special reason.

1 Like

I think this is not a particular concern in general. Just not export the types, if the user doesn’t have to access them directly.

AbstractVector or AbstractArray (depending on whether you actually want precisely a 1-dimensional array or you just want any array of any number of dimensions) is usually a good choice. Restricting your number-like scalar types to a subtype of Number is probably a good compromise between flexibility and providing an explicit signal to your users about what kind of input is expected.

Also, I think you’ve been misinformed about Number and Unitful:

julia> supertypes(typeof(1u"m"))
(Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}, Unitful.AbstractQuantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}, Number, Any)


julia> 1u"m" isa Number
true

A Unitful.Quantity is a subtype of Number, so you can use ::Number for type annotations even if you want to accept unitful quantities.

3 Likes

Reference for question #3:

Reference for question #2:

Fields being private unless indicated otherwise is just a convention, not something enforced by the language.

Cf the custom of not entering someone else’s home without being invited in, even if the door is unlocked.

Perhaps you can help us understand the implementation more. To calculate the norm of your CylidricalStress, is it just the euclidian norm of the elements?

Or are there other calculations involved, given one is the radial stress, one is the longitudinal stress?

Sorry, I don’t mean “How do I enforce privacy?”.
I am wondering how you can get away with not explaining your types to the user? Define every possible function they could want to use it with in the package, and make sure those functions return only Base types?

I think the key thing is whether or not it is iterable. If you define iteration for your type, (which is easy), then std, var, sum, etc. all intuitively make sense to the user, and don’t need to be overloaded or documented. Since its the same behavior as the docstring for sum(::Any) which exists in base.

Yes, the standard Euclidian norm equals the von Mises equivalent stress since these are principal stresses. +, -, *, mean, etc. will be used mostly for comparisons between datasets. I am going to be comparing the theoretical value above to FEA data. All those functions should act component-wise like a vector which is why I don’t want to bother defining all my own methods. However, I like the convenience of naming the components in my own struct.

Yep, that’s misinformation.

There is a point to not needlessly restricting types, but Number is a fairly high level, not much of a restriction.

1 Like

I am not sure what “explaining your types” means in this context.

Generally, users should understand the API that your types conform to; if that is clear then they have no need to understand type internals. The API can be a polished interface like iteration, or something that you just define for this package.

Then FieldVector from StaticArrays is probably the easiest path.

2 Likes

I 2nd this.

You could implement the vector interface yourself, but if StaticArrays.jl does it for you, use that.

You can still overload other, more specialized, functions for your type. And document those. Seems pretty convenient.

1 Like

Is using the type annotations ::Number and ::AbstractArray the best way to enforce scalars vs. collections respectively then?

I think you want parameteric types so that the struct members are not abstract.

3 Likes

Can you show me or point me to documentation on defining iteration on my type?

Note, you don’t need to do this if you go with the StaticArrays.jl solution. StaticArrays.jl will do this for you.

A quick google search of “iteration define julia” gives the following link to the docs.

I don’t have a good understanding of what API means yet, so I’m not following the distinction you are making.