# Understanding generic functions and docstrings

**URL:** <https://discourse.julialang.org/t/understanding-generic-functions-and-docstrings/74279>\
**Category:** New to Julia\
**Created:** [January 9, 2022, 12:16pm UTC](https://discourse.julialang.org/t/understanding-generic-functions-and-docstrings/74279 "2022-01-09T12:16:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeeviL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leevil/32/32586_2.png) [@LeeviL](https://discourse.julialang.org/u/LeeviL)\
**Post date:** [January 9, 2022, 12:16pm UTC](https://discourse.julialang.org/t/understanding-generic-functions-and-docstrings/74279/1 "2022-01-09T12:16:17Z")

</div>

Hi!

Julia newcomer here who is trying to understand generic functions.

Consider the following two blocks of code:

```julia
abstract type A end

"""
Function that does something.
"""
somefunction(t::A)

struct B <: A
    # Code
end

struct C <: A
   # Code 
end

function somefunction(t::B)
    # Code
end

function somefunction(t::C)
    # Code
end

```

and

```julia
abstract type A end

somefunction(t::A)

struct B <: A
    # Code
end

struct C <: A
   # Code 
end

function somefunction(t::B)
    # Code
end

function somefunction(t::C)
    # Code
end

```

It is easy to see that the second block will fail since type assertion is tried to be made for undefined variable `t`. However, I don’t understand why the first block of code works, where `somefunction` has docstring included. My first thought would have been to implement the generic function just as a function without body:

```julia
function somefunction(t::A) end

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 9, 2022, 1:23pm UTC](https://discourse.julialang.org/t/understanding-generic-functions-and-docstrings/74279/2 "2022-01-09T13:23:58Z")

</div>

What you are seeing is documented here:  
[https://docs.julialang.org/en/v1.7/manual/documentation/#Functions-and-Methods-2](https://docs.julialang.org/en/v1.7/manual/documentation/#Functions-and-Methods-2)

Your first block just adds documentation to the specific (non existing) method `somefunction(::Any)`:

```julia
help?> somefunction()
  Function that does something.

```

Your second block fails, because you are just trying to call method `somefunction(::A)` with a parameter `t` which isn’t defined. If you define `t` it will fail, because there is no method `somefunction(::A)`.
