# How to add methods to a function

**URL:** <https://discourse.julialang.org/t/how-to-add-methods-to-a-function/81730>\
**Category:** General Usage\
**Tags:** question, methods\
**Created:** [May 26, 2022, 3:59pm UTC](https://discourse.julialang.org/t/how-to-add-methods-to-a-function/81730 "2022-05-26T15:59:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 26, 2022, 3:59pm UTC](https://discourse.julialang.org/t/how-to-add-methods-to-a-function/81730/1 "2022-05-26T15:59:51Z")

</div>

Suppose it has the task of writing a function that splits a given string at the points where there is a character change from one `from` to another `to` set.  
To delimit the context, suppose that the `from` and the `to` can be defined as Set or as Array or as functions, even in mixed combination if necessary.

Below is an idea of one of the methods that handles the combination from :: Function and to :: Function.

What is (or one of) Julian’s “correct” way (syntax, strategy) to set up this function and its methods?

```julia
function splitonswitch(str, from, to)
l=1
spl=String[]
d=length(str)
for i in eachindex(str)
    if from(str[i])&&to(str[min(i+1,d)])
        push!(spl, str[l:i])
        l=i+1
    end
    
end
push!(spl,str[l:end])
end

```

is the following way okay?

```julia
splitonswitch(str, From::Set, To::Set)= splitsonwitch(str, c->c ∈ From,c->c ∈ To)

```

since the method that uses functions as parameters `from` and `to` expects functions that have a boolean as output, do I have to, in any way, impose it in the definition of the parameters?  
or should i do the checking inside the code?  
and how?

---

<div class="post-metadata">

**Author:** ![s-broda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s-broda/32/3946_2.png) [@s-broda](https://discourse.julialang.org/u/s-broda)\
**Post date:** [May 26, 2022, 4:12pm UTC](https://discourse.julialang.org/t/how-to-add-methods-to-a-function/81730/2 "2022-05-26T16:12:21Z")

</div>

You have a typo in the function name for the second method.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 26, 2022, 4:14pm UTC](https://discourse.julialang.org/t/how-to-add-methods-to-a-function/81730/3 "2022-05-26T16:14:27Z")

</div>

Thanks! I fix it.
