Declaring types from packages

Hello, I am facing an issue that I’m sure is very minor, but I cannot find anything in the documentation for either the package or Julia documentation. I’m hoping that someone here can help me out (without knowing too much about abstractalgebra.jl)

My main question is that I want to create a new “compound” struct from that assembles two types from the package AbstractAlgebra.jl. Consider the following

using AbstractAlgebra
FreeModule=AbstractAlgebra.Generic.FreeModule
Morphism=AbstractAlgebra.Generic.ModuleHomomorphism
mutable struct ChainComplex
    modules::Array{FreeModule}
    morphisms::Array{Morphism}
end

Now, apriori, the types FreeModule are already parametric. Indeed they take as input types such as BigInt, float{64} etc. I wanted to somehow include this in my definition, such as

FreeModule=AbstractAlgebra.Generic.FreeModule{T}
Morphism=AbstractAlgebra.Generic.ModuleHomomorphism{T}

but this appears to be nonsense in Julia, so I’m wondering how I can define this new compound type. When I try the first option above, and try to construct it, I get the error

M_1 = FreeModule(ZZ, 2)
M_2=FreeModule(ZZ,2)
f = ModuleHomomorphism(M_1, M_2, matrix(ZZ, 2, 2, [1, 2, 3, 4]))
ChainComplex([M_1,M_2],[f])

MethodError: Cannot convert an object of type
AbstractAlgebra.Generic.ModuleHomomorphism{BigInt} to an object of type
Array{AbstractAlgebra.Generic.ModuleHomomorphism,N} where N

which is a little bit jibberish, but I take it to mean that I declared this to be of a particular type without specifying BigInt, so once that specification comes in, it breaks my type.

If it is helpful, here is the return from the function typeof(M_1) and typeof(f) respectively:

AbstractAlgebra.Generic.FreeModule{BigInt}
AbstractAlgebra.Generic.ModuleHomomorphism{BigInt}

Is there any simple way to get the functionality that I want?

What you want is

mutable struct ChainComplex{F<:FreeModule,M<:ModuleHomorphism}
    modules::Array{F}
    morphisms::Array{M}
end

This makes your ChainComplex parametrized on the type of your module and morphism. It’s in the docs here Types · The Julia Language

3 Likes

From the error message, it looks like you pass in the second argument directly, without wrapping it into an array. But looking at your example code in the first post, you actually have [f], so maybe it’s just some copy-n-paste error?

ugh, sorry I grabbed the wrong error message. My apologies. I’ve edited the comment to include the correct one.

That doesn’t look like an error, just a really long type.

3 Likes

facepalm issue resolved. Thank you so much to all who assisted me in this very beginner problem. This seems like a great community.

1 Like

No problem. I was actually recently thinking about how to handle nested parametetric types, that is, parametric types where the type parameters are themselves parametric types. And your example shows that there’s nothing special about that scenario :+1:

1 Like