# Efficient way to avoid repetitive functions with multiple methods

**URL:** <https://discourse.julialang.org/t/efficient-way-to-avoid-repetitive-functions-with-multiple-methods/29539>\
**Category:** New to Julia\
**Created:** [October 6, 2019, 2:10am UTC](https://discourse.julialang.org/t/efficient-way-to-avoid-repetitive-functions-with-multiple-methods/29539 "2019-10-06T02:10:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [October 6, 2019, 2:10am UTC](https://discourse.julialang.org/t/efficient-way-to-avoid-repetitive-functions-with-multiple-methods/29539/1 "2019-10-06T02:10:35Z")

</div>

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

```julia
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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [October 6, 2019, 2:23am UTC](https://discourse.julialang.org/t/efficient-way-to-avoid-repetitive-functions-with-multiple-methods/29539/2 "2019-10-06T02:23:06Z")

</div>

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

```julia
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)

```
