Efficient way to avoid repetitive functions with multiple methods

I have a structure which has a function with many methods.

struct shape
    type  :: Symbol
    mtl   :: String
    t     :: Real
    A     :: Float64
    Asy   :: Float64
    Asz   :: Float64
    function shape(mtl::String,t::Real)
        A = t*t;
        Asy = (5/6)*A;
        Asz = (5/6)*A;
        return new(:square,mtl,t,A,Asy,Asz)
    end
    function shape(type::Symbol,mtl::String,t::Real)
        A = t*t;
        Asy = (5/6)*A;
        Asz = (5/6)*A;
        return new(type,mtl,t,A,Asy,Asz)
    end
    function shape(t::Real)
        A = t*t;
        Asy = (5/6)*A;
        Asz = (5/6)*A;
        return new(:square,"mtl",t,A,Asy,Asz)
    end
end

Is there a way to group the methods?
i.e

struct shape
    type  :: Symbol
    mtl   :: String
    t     :: Real
    A     :: Float64
    Asy   :: Float64
    Asz   :: Float64
    function shape(arg)
        A = t*t;
        Asy = (5/6)*A;
        Asz = (5/6)*A;
        return new(type,mtl,t,A,Asy,Asz)
    end
end

Do you really want t to be Real in your struct, and not something like:

struct Shape{T}
    type  :: Symbol
    mtl   :: String
    t     :: T
    A     :: Float64
    Asy   :: Float64
    Asz   :: Float64
    function Shape(type::Symbol,mtl::String,t::Real)
        A = t*t;
        Asy = (5/6)*A;
        Asz = (5/6)*A;
        return new(type,mtl,t,A,Asy,Asz)
    end
end
Shape(mtl::String,t::Real) = Shape(:square,mtl,t)
Shape(t::Real, type=:square, mtl = "mtl") = Shape(type, mtl, t)
2 Likes