how can is solve this problem?
mutable struct Parent
name::String
children::Vector{Child}
function Parent(name)
return new(name)
end
end
mutable struct Child
name::String
parent::Parent
function Child(name)
return new(name)
end
end
parent = Parent("father")
child = Child("son")
Produces an error
LoadError: UndefVarError: Child not defined
Is there some way to handle this case?
As far as I know the only way to handle this currently is via a parametric type (I know it is not perfect). Here is an example which additionally restricts the parameter so that you get almost what you want:
abstract type AbstractChild end
mutable struct Parent{T<:AbstractChild}
name::String
children::Vector{T}
function Parent{T}(name) where {T<:AbstractChild}
return new{T}(name)
end
end
mutable struct Child <: AbstractChild
name::String
parent::Parent
function Child(name)
return new(name)
end
end
Parent(name) = Parent{Child}(name)
parent = Parent("father")
child = Child("son")
And just to add on to #Bogumił Kamiński's answer the abstract type AbstractChild end creates a node for julia to traverse from at runtime in the program.
Related
Recently started to try and learn Julia through examples. I am basically trying to figure out how to access a struct property from within a function inside the struct itself. E.g:
struct Test
a::Int
foo::Function
function Test()
return new(777, xfoo)
end
function xfoo()
println(a)
end
end
t = Test()
t.foo()
I get:
ERROR: LoadError: UndefVarError: a not defined
Stacktrace:
[1] (::var"#xfoo#1")()
# Main /tmp/j.jl:10
[2] top-level scope
# /tmp/j.jl:15
in expression starting at /tmp/j.jl:15
Am I using Julia wrong or am I missing something?
Julia is not object oriented language so object oriented patterns are usually not a good idea.
Hence xfoo should be outside of Test:
function xfoo(t::Test)
println(t.a)
end
There are packages that try to emulate OOP with Julia (however this is not a Julian pattern): https://github.com/Suzhou-Tongyuan/ObjectOriented.jl
You can also easily find quite a lot of discussion behind the design decision no to make Julia OOP. Start with: https://discourse.julialang.org/t/why-there-is-no-oop-object-oriented-programming-in-julia/86723
Workaround
Just out of curiosity one can find some workaround to attach a function to a struct (not a recommended design pattern!). For an example:
mutable struct MyTest
a::Int
foo::Function
function MyTest()
s = Ref{MyTest}()
s[] = new(777, () -> println(s[].a))
s[]
end
end
And some sample usage:
julia> t = MyTest();
julia> t.foo()
777
julia> t.a = 900;
julia> t.foo()
900
I know that Julia does not have OOP but that multiple dispatch enables similar ideas. Given how seemingly contentious the use of singletons are in Python, I am wondering if there is a similar idea Julia (i.e. a struct that can only be instantiated once).
I am wondering if there's a way to have the constructor keep track of the number of times an object was instantiated with a global var or something like that? Or it's altogether not be possible?
The main way people make singletons in Julia is to define an empty struct (which means that it has 0 size), and define methods that return information for it.
struct Singleton
end
data(::Singleton) = "whatever you want it to do"
etc.
From this book, a singleton can be defined as a type without fields:
struct MySingleton end
julia> MySingleton() === MySingleton()
true
You can also use Val, which can receive any value (of bit type):
julia> Val(1) === Val(1)
true
julia> Val(:foo) === Val(:foo)
true
using Val you can write something like this:
julia> do_something(::Val{:asymbol}) = println("foo")
julia> do_something(::Val{:anothersymbol}) = println("bar")
julia> do_something(s::String) = do_something(Val{Symbol(s)})
julia> do_something("asymbol")
foo
The definition of Val is:
struct Val{x} end
So, for a more clear readability of your code, you could define your own singleton type as, for example:
struct Command{x} end
I have a Julia struct:
struct WindChillCalc
location::Tuple;
w_underground_url::String;
WindChillCalc(location, wug) = new(location, w_underground_url);
end
How do I hard code w_underground_url to contain "someString" upon the constructor of WindChillCalc being called?
Try something like below
struct testStruct
x::Real
y::String
testStruct(x,y) = new(x,"printThis")
end
test = testStruct(1,"")
test2 = testStruct(2,"")
println(test.y)
println(test2.y)
It prints "printThis" for any object.
Just write for example:
struct WindChillCalc{T}
location::T;
w_underground_url::String;
WindChillCalc(location::T) where {T <: NTuple{2, Real}} =
new{T}(location, "some string");
end
and now Julia automatically creates a concrete type for you:
julia> WindChillCalc((1, 2.5))
WindChillCalc{Tuple{Int64,Float64}}((1, 2.5), "some string")
Note that I have restricted the type of the parameter to be a two element tuple where each element is a Real. You could of course use another restriction (or use no restriction).
With this approach your code will be fast as during compile time Julia will know exact types of all fields in your struct.
I have a mutable struct with optional fields like this:
mutable struct MyStruct
field1::Union{Int, Nothing}
field2::Union{String, Nothing}
field3::Union{Int, Nothing}
field4::Union{String, Nothing}
# ...
end
I can now write a default constructor which initializes the fields with nothing:
MyStruct() = MyStruct(nothing, nothing, nothing, nothing)
This is not so nice when my struct has many fields. Also, I have to count the fields to get the constructor call with all the 'nothings' correct in this case. Is there a better way to do that?
Depending on the field content, I want to call different functions later:
if mystruct.field1 == nothing
do_this()
else
do_that()
end
You can use fieldcount function to achieve that. This function gives you the number of fields of that an instance of given type would have. Here is an example containing a mutable struct and an outer constructor.
julia> mutable struct Foo
x
y
z
end
julia> Foo() = Foo(ntuple(x->nothing, fieldcount(Foo))...); # you can also fill an array and use `...`
julia> Foo()
Foo(nothing, nothing, nothing)
I am using the code below and it seems Julia should be able to infer the type parameters by itself, however this is not the case. Any ideas, maybe I'm doing something wrong?
abstract type ABS{A,B} end
struct MyStruct{A,B,K<:ABS{A,B}}
a::A
b::B
MyStruct{A,B,K}(a::A,b::B) where {A,B,K<:ABS{A,B}} = new(a,b)
end
MyStruct{Int64,Float64,ABS{Int64,Float64}}(1,2.1) # <<-- works
MyStruct(1,2.1) # <<-- doesn't work
I forgot the outer constructor, as #gnimuc pointed out. This code works:
abstract type ABS{A,B} end
struct Myk <: ABS{Int64,Float64} end
struct MyStruct{A,B,K<:ABS{A,B}}
a::A
b::B
MyStruct{A,B,K}(a::A,b::B) where {A,B,K<:ABS{A,B}}= new(a,b)
end
# this is the outer constructor:
MyStruct(a::A, b::B, ::K) where {A,B,K<:ABS{A,B}} = MyStruct{A,B,K}(a,b)
# now this works:
MyStruct(1,2.1,Myk())