# Refactoring into parent type possible?

**URL:** <https://discourse.julialang.org/t/refactoring-into-parent-type-possible/16019>\
**Category:** New to Julia\
**Created:** [October 8, 2018, 1:16pm UTC](https://discourse.julialang.org/t/refactoring-into-parent-type-possible/16019 "2018-10-08T13:16:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [October 8, 2018, 1:16pm UTC](https://discourse.julialang.org/t/refactoring-into-parent-type-possible/16019/1 "2018-10-08T13:16:54Z")

</div>

suppose an abstract type `R` has several concrete children types, say `S` and `T`, i.e. `S <: R` and `S <: T`. Now, a function `fun` is overloaded on _every_ child of `R` like:

```julia
function fun(x::S)
   @assert common_check(x)
  return S_specific(x)
end

function fun(x::T)
   @assert common_check(x)
  return T_specific(x)
end

```

It would be nice to be able to refactor `common_check()`, in the following _imaginative_ manner:

```julia
function fun(x::R)
  @assert common_check(x)
  # ??? then, how to dispatch???
end

function fun(x::S)
  return S_specific(x)
end

function fun(x::T)
  return T_specific(x)
end

```

_how_ could it be done??? the problem is: if I call `fun(::S)` or `fun(::T)`, `fun(::R)` would not be called at all. Thanks.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 8, 2018, 1:20pm UTC](https://discourse.julialang.org/t/refactoring-into-parent-type-possible/16019/2 "2018-10-08T13:20:14Z")

</div>

The following pattern is quite common:

```julia
function fun(x::R)
    @assert common_check(x)
    return _fun(x)
end

_fun(x::S) = S_specific(x)
_fun(x::T) = T_specific(x)

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [October 8, 2018, 1:27pm UTC](https://discourse.julialang.org/t/refactoring-into-parent-type-possible/16019/3 "2018-10-08T13:27:16Z")

</div>

oic (use another function name).

what if the function is `(*)`? what should be the name of “\_fun”?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 8, 2018, 2:00pm UTC](https://discourse.julialang.org/t/refactoring-into-parent-type-possible/16019/4 "2018-10-08T14:00:46Z")

</div>

> [@tomtom](#):
>
> what should be the name of “\_fun”?

It doesn’t matter. The choice is up to you.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 8, 2018, 2:25pm UTC](https://discourse.julialang.org/t/refactoring-into-parent-type-possible/16019/5 "2018-10-08T14:25:06Z")

</div>

You can prefix `*` with `_` too:

```julia
julia> @eval $(Symbol("_*"))(x) = "Hello, $x"
_* (generic function with 1 method)

julia> getfield(Main, Symbol("_*"))("tomtom")
"Hello, tomtom"

```

:troll:
