I saw code like
return new{T}(a, b)
as the last the line of some constructor function of a struct.
I am wondering what is the purpose of ‘new’ here? Typically, we see
return (a,b)
I saw code like
return new{T}(a, b)
as the last the line of some constructor function of a struct.
I am wondering what is the purpose of ‘new’ here? Typically, we see
return (a,b)
In inner constructors (ie constructors within the struct
block) you can’t call a constructor by name since it hasn’t been defined yet, so we use the new
keyword.
That is, new{T}(a, b)
should be in a context like:
struct Foo{T} where T
a::T
b::T
function Foo(a, b)
# do some checks on a, b
return new{T}(a, b)
end
end
Here we use new
to do the actual allocation/construction of the type.