Hello,
I’m new to Julia and am watching the Juliacon 2020 video series. I have a question about the use of a mutable struct in this video here.
From what I am seeing, I think he defines a mutable struct with some fields, then defines two methods for that struct. So If I wanted to do something similar I could do
a = 3
b = 5
# Example of multiple dispatch with a mutable struct
begin
mutable struct MyStruc
C::Float64
a::Int64
end
# Multiple dispatch of struct
MyStruc(C::Float64; aValue=a) = MyStruc(C,aValue)
MyStruc(C::Int64; bValue=b) = MyStruc(C,bValue)
end
That seems to work. But the syntax is strange to me, doing MyStruc = MyStruc
. Am I correct there are two methods for this struct? I’m using Pluto and I don’t see “MyStruct with two methods” above the cell with the struct defined (unlike when I define a function with multiple methods and it does say the number of corresponding methods above the cell).
I also tried the following
# Example of multiple dispatch with a mutable struct
begin
mutable struct MyStruc
C::Float64
a::Int64
end
# Multiple dispatch of struct
MyStruc(C::Float64; aValue=a) = MyStruc(C,aValue)
MyStruc(C::Int64, a::Float64) = MyStruc(C,a)
end
But when I try to use the second method like this
TestStruct = MyStruc(3, 101.1)
I get a StackOverflowError. Not sure why though - would appreciate any comments and insight on this.