Hi, Could someone explain what’s is going on here?
module test
struct Stuff{A, B, C, D}
a::A
b::B
c::C
d::D
Stuff{A, B, C, D}(v1, v2, v3, v4) where {A, B, C, D} = new(v1, v2, v3, v4)
end
function func1(s::Stuff{A, B, C, D}) where {A, B, C, D}
println(s.a, " ", typeof(s.a))
println(s.b, " ", typeof(s.b))
println(s.c, " ", typeof(s.c))
println(s.d, " ", typeof(s.d))
end
stuff = Stuff{Int64, Float64, Bool, String}(1, 1.0, false, "hello")
func1(stuff)
# found that this also works...
const S = Stuff{A, B, C, D} where {A, B, C, D}
function func2(s::S)
println(s.a, " ", typeof(s.a))
println(s.b, " ", typeof(s.b))
println(s.c, " ", typeof(s.c))
println(s.d, " ", typeof(s.d))
end
func2(stuff)
end
With reference to func2, I’ve found I can define a type alias for complex parametric types that greatly cleans up the function signature. That being said, I’m not sure I understand the placement of the “where” syntax in func1, since for a crude find and replace (I know that’s not what’s its doing) the where syntax would be within the function argument not afterwards. Is what I’m doing legal and performant? Is it recommended? I have to be honest when I started learning Julia (about 2 weeks ago) I just iterated my syntax for parametric types and the “where” until I got it working. I don’t really get what it’s doing for undefined types like A, B etc or if I should be doing something better.
I come from c++ I can’t help thinking {A, B, C, D} as the template types, but I have no analogy for “where {A, B, C, D}” and so can’t really get my head round it…any help appreciated.
Thanks,
Andy