# Is it possible to use "supertype" in function type declaration

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320>\
**Category:** Data\
**Tags:** type\
**Created:** [April 13, 2018, 9:22am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320 "2018-04-13T09:22:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [April 13, 2018, 9:22am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320/1 "2018-04-13T09:22:11Z")

</div>

Hi,

When I process strings in Julia, I have often a problem with the ::String type because sometime my function need a String and sometime a SubString{String} and I have this error

no method matching processData(::SubString{String})

A quick (and dirty) solution to solve this issue is to avoid type declaration, for example, replace

```julia
processData(data::String)
by
processData(data)

```

But this is not optimal for performances. Of course it is possible to use multiple dispatch and write 2 functions

```julia
processData(::SubString{String})
processData(::String)

```

This is a simple example, but imagine that I have a function which process 10 strings that could also be SubString{String}, it is not possible to write all functions with all types combinations. So I wonder if there is a more efficient solution, if it would be possible to use a supertype in function declaration, something like

```julia
processData(<:String)

```

Thanks ! 😉

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 13, 2018, 9:33am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320/2 "2018-04-13T09:33:51Z")

</div>

> [@Fred](#):
>
> But this is not optimal for performances.

This does not impact performance at all.

Anyway, here, you would do:

```julia
processData(::AbstractString)

```

because

```julia
julia> supertype(String)
AbstractString

```

This function would accept anything that is `<: AbstractString`.

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [April 13, 2018, 11:40am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320/3 "2018-04-13T11:40:38Z")

</div>

Thank you very much for your quick answer @kristoffer.carlsson

I delcare types in Julia, because in performance tips it is said : “Julia’s compiler specializes code for argument types at function boundaries, so in the original implementation it does not know the type of a during the loop (since it is chosen randomly). Therefore the second version is generally faster since the inner loop can be recompiled as part of fill\_twos! for different types of a.”

---

<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:** [April 13, 2018, 11:56am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320/4 "2018-04-13T11:56:25Z")

</div>

You are talking about the [function barrier example](https://docs.julialang.org/en/latest/manual/performance-tips/#kernal-functions-1), where the problem is that the type is not known to the compiler (in fact, it is generated randomly).

However, in your code it should be known, if the compiler can figure it out from the caller of `processData`.

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [April 13, 2018, 12:07pm UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320/5 "2018-04-13T12:07:43Z")

</div>

You are right @Tamas_Papp, the type of “data” my function caller is not random 😉

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 13, 2018, 12:16pm UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320/6 "2018-04-13T12:16:54Z")

</div>

> [@Fred](#):
>
> I delcare types in Julia, because in performance tips it is said : “Julia’s compiler specializes code for argument types at function boundaries,

You have misunderstood. This has nothing to do with what types you manually set on the function arguments. Julia will specialize the function on the argument types that occur _during runtime_.

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [April 13, 2018, 12:21pm UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320/7 "2018-04-13T12:21:06Z")

</div>

Thanks @kristoffer.carlsson it is more clear with your explanation.

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [April 13, 2018, 12:28pm UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320/8 "2018-04-13T12:28:05Z")

</div>

To word it in an other way, because I have seen people make the same mistake:

Argument type declarations in function signatures only serve to _filter_ the input arguments. Having an `AbstractString` declaration will thus have your `processData` function accept only `AbstractString`s and its subtypes. Trying to call `processData` with, for example, a number, will result in a method error.

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [April 13, 2018, 12:30pm UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320/9 "2018-04-13T12:30:01Z")

</div>

Thanks @jebej ! It is also said that “Declare types of keyword arguments will reduce the overhead of calls to the function” 🙂

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [April 13, 2018, 12:38pm UTC](https://discourse.julialang.org/t/is-it-possible-to-use-supertype-in-function-type-declaration/10320/10 "2018-04-13T12:38:30Z")

</div>

That’s for keyword arguments, and I believe that this overhead has been almost eliminated in 0.7. See [#24987](https://github.com/JuliaLang/julia/pull/24987).
