Struct Embedding in Julia
# Julia supports composition of types through composition and multiple inheritance.
# This is similar to struct embedding in other languages.
# Define a base type
struct Base
num::Int
end
# Define a method for the base type
function describe(b::Base)
return "base with num=$(b.num)"
end
# Define a container type that includes Base
struct Container
base::Base
str::String
end
# Main function to demonstrate the usage
function main()
# Create an instance of Container
co = Container(Base(1), "some name")
# We can access the base's fields directly on `co`
println("co={num: $(co.base.num), str: $(co.str)}")
# We can also access the base fields using the full path
println("also num: $(co.base.num)")
# Since `Container` includes `Base`, we can call methods defined for `Base` on `Container` instances
println("describe: $(describe(co.base))")
# Define an abstract type for objects that can be described
abstract type Describer end
# Extend the Describer interface for Base
Describer(b::Base) = b
# We can use Base as a Describer because it implements the necessary method
d = Describer(co.base)
println("describer: $(describe(d))")
end
# Run the main function
main()This Julia code demonstrates concepts similar to struct embedding in other languages. Here’s a breakdown of the translation:
We define a
Basestruct with anumfield, similar to the originalbasestruct.We define a
describefunction forBase, which is similar to the method in the original code.We create a
Containerstruct that includes aBaseinstance and an additionalstrfield. This is similar to embedding in the original code.In the
mainfunction, we create an instance ofContainerand demonstrate how to access fields and methods.We define an abstract type
Describerto demonstrate interface-like behavior. In Julia, we use multiple dispatch instead of explicit interfaces.We extend the
Describertype forBaseto show how a type can implement an “interface”.Finally, we demonstrate how a
Baseinstance can be used where aDescriberis expected.
To run this program, save it as struct_composition.jl and use the Julia REPL or run it from the command line:
$ julia struct_composition.jl
co={num: 1, str: some name}
also num: 1
describe: base with num=1
describer: base with num=1This example showcases how Julia’s type system and multiple dispatch can be used to achieve similar functionality to struct embedding in other languages, while following Julia’s idiomatic practices.