Thanks for the hint with the type alias. Using an array of projects does not help me, because I want to have an easy way to plot any of the numbers in the list of projects. And I don't know any easy way to convert one field of an array of projects into a one dimensional vector that can be plotted.
Furthermore I use the type Number because I use OpenOpt with automatic differentiation. The numbers can also have the type ForwardDiff.GradientNumber . Best regards: Uwe On Sunday, March 13, 2016 at 3:05:31 PM UTC+1, FQ wrote: > > I'm not an expert on this, but instead of > > NumberOrVector = Union{Number, Vector} > > one should probably always use > > typealias NumberOrVector Union{Number, Vector} > > which makes the names equivalent statically, opposed to the =, which > assigns a value (Union{Number,Vector}) to the variable NumberOrVector > which could be changed later, so this is probably bad if the compiler > tries to optimze things. > > Can you describe in words what you are trying to accomplish with the > code in that gist? It looks like you implement the whole NumberOrVector > type just so you could have multiple "Project"s in a single instance of > "Project", which does not make sense semantically. Judging from your > variable names, it looks like a single project has serveral parameters, > each of which is just a single number. This is not good design. You > could simply make 'projects' an Array{Project,1} like so: > > julia> type Project > p_el_nom::Number > rel_drum_diameter::Number > end > > julia> projects = Array{Project,1}() > 0-element Array{project,1} > > julia> p1 = project(1.4,3) > project(1.4,3) > > julia> p2 = project(-4,99) > project(-4,99) > > julia> push!(projects,p1) > 1-element Array{project,1}: > project(1.4,3) > > julia> push!(projects,p2) > 2-element Array{project,1}: > project(1.4,3) > project(-4,99) > > julia> projects > 2-element Array{project,1}: > project(1.4,3) > project(-4,99) > > julia> projects[2].p_el_nom > -4 > > The 1 in Array{Project,1} means "one-dimensional"; if it were 2 (or > more) it would create an n-dimensional matrix, but that does not seem to > make sense for your case. > > Also, you might wish to be a little more specific with your types. > "Number" is very general, and might also be a complex number for > instance. Judging from your variable names, AbstractFloat would probably > be a good idea. If any of your parameters is always a whole number, you > could also use Int for that. > > regards > fq > > Am 13.03.2016 um 13:58 schrieb Uwe Fechner: > > Hello, > > > > I need a custom type, that can hold scalars or vectors, that I can print > > and copy. > > > > I created an example: > > https://gist.github.com/ufechner7/8545f18ad6d65b80b334 > > > > Is this good programming style? > > > > Is it a good idea to use a union like: > > NumberOrVector = Union{Number, Vector} > > > > Is there a way to implement the copy and the push! functions in a > > generic way, so they > > do not need to be modified if I add fields? > > > > Perhaps there is also a generic way to implement the constructor: > > Project(f::Vector) > > > > Any hints appreciated! > > > > Uwe Fechner > > > > > >