# Search for functions whose first argument can be a specific type

**URL:** <https://discourse.julialang.org/t/search-for-functions-whose-first-argument-can-be-a-specific-type/38318>\
**Category:** General Usage\
**Tags:** documentation, type, help-database\
**Created:** [April 27, 2020, 5:15pm UTC](https://discourse.julialang.org/t/search-for-functions-whose-first-argument-can-be-a-specific-type/38318 "2020-04-27T17:15:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [April 27, 2020, 5:15pm UTC](https://discourse.julialang.org/t/search-for-functions-whose-first-argument-can-be-a-specific-type/38318/1 "2020-04-27T17:15:35Z")

</div>

In object-oriented languages, it is easy to see what methods a type has. For example, to find a replace command for strings, I just need to search in the methods that a string object exposes (using, e.g., ` __dict__ ` in python). In Julia, because of multiple dispatch, methods just occupy the global namespace, and I can’t restrict my search in the function space according to types. I was wondering if there are ways to do this.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 27, 2020, 5:23pm UTC](https://discourse.julialang.org/t/search-for-functions-whose-first-argument-can-be-a-specific-type/38318/2 "2020-04-27T17:23:29Z")

</div>

have you tried `methods` and `methodswith`?

```julia
  methodswith(typ[, module or function]; supertypes::Bool=false])
  Return an array of methods with an argument of type typ.
  The optional second argument restricts the search to a particular module or function (the default is all top-level modules).
  If keyword supertypes is true, also return arguments with a parent type of typ, excluding type Any.

```

```julia
  methods(f, [types], [module])
  Return the method table for f.
  If types is specified, return an array of methods whose types match. If module is specified, return an array of methods defined in that module. A
  list of modules can also be specified as an array.
  │ Julia 1.4
  │
  │ At least Julia 1.4 is required for specifying a module.

```

---

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [July 21, 2020, 10:43am UTC](https://discourse.julialang.org/t/search-for-functions-whose-first-argument-can-be-a-specific-type/38318/3 "2020-07-21T10:43:21Z")

</div>

Can you provide an example of using this to get methods for `Poisson` from `Distributions`? I tried the following, but it did not show, e.g., `rand(Poisson(1))`.

```julia
julia> typeof(Poisson(1))
Poisson{Float64}

julia> methods(typeof(Poisson(1)))
# 1 method for type constructor:
[1] (::Type{Poisson{T}})(λ::Real) where T<:Real in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:26

julia> methodswith(typeof(Poisson(1)))

julia> methods(Poisson(1))
# 0 methods:

julia> methodswith(Poisson(1))
ERROR: MethodError: no method matching methodswith(::Poisson{Float64})
Closest candidates are:
  methodswith(::Type; supertypes) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/InteractiveUtils/src/InteractiveUtils.jl:168
  methodswith(::Type, ::Function) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/InteractiveUtils/src/InteractiveUtils.jl:136
  methodswith(::Type, ::Function, ::Any; supertypes) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/InteractiveUtils/src/InteractiveUtils.jl:136
  ...
Stacktrace:
 [1] top-level scope at REPL[121]:1

julia> methodswith(Poisson)
[1] ccdf(d::Poisson, x::Int64) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:557
[2] cdf(d::Poisson, x::Int64) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:556
[3] cf(d::Poisson, t::Real) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:113
[4] convolve(d1::Poisson, d2::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/convolution.jl:45
[5] cquantile(d::Poisson, q::Real) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:562
[6] entropy(d::Poisson{T}) where T<:Real in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:68
[7] invlogccdf(d::Poisson, lq::Real) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:564
[8] invlogcdf(d::Poisson, lq::Real) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:563
[9] kurtosis(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:65
[10] logccdf(d::Poisson, x::Int64) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:559
[11] logcdf(d::Poisson, x::Int64) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:558
[12] logpdf(d::Poisson, x::Int64) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:554
[13] mean(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:52
[14] mgf(d::Poisson, t::Real) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:108
[15] mode(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:54
[16] modes(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:57
[17] params(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:45
[18] pdf(d::Poisson, x::Int64) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:553
[19] quantile(d::Poisson, q::Real) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:561
[20] rate(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:48
[21] sampler(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:147
[22] skewness(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:63
[23] var(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:61
[24] broadcast(::typeof(pdf), d::Poisson, X::UnitRange) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:102
[25] broadcast!(::typeof(pdf), r::AbstractArray, d::Poisson, rgn::UnitRange) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:99
[26] convert(::Type{Poisson{T}}, d::Poisson{S}) where {T<:Real, S<:Real} in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariate/discrete/poisson.jl:41
[27] maximum(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:148
[28] minimum(d::Poisson) in Distributions at /Users/evar/.julia/packages/Distributions/dTXqn/src/univariates.jl:147

julia>

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 21, 2020, 11:23am UTC](https://discourse.julialang.org/t/search-for-functions-whose-first-argument-can-be-a-specific-type/38318/4 "2020-07-21T11:23:05Z")

</div>

You can find `rand` if you add the keyword argument `supertypes=true`:

```julia
julia> methodswith(Poisson; supertypes=true)
[snip output ...]
[96] rand(s::Sampleable) in Distributions at D:\.julia\packages\Distributions\vskB8\src\genericrand.jl:22

```

(Just note that `Sampleable` is a supertype of `Poisson`)

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 21, 2020, 1:06pm UTC](https://discourse.julialang.org/t/search-for-functions-whose-first-argument-can-be-a-specific-type/38318/5 "2020-07-21T13:06:38Z")

</div>

One more note: see that, although it is a good approximation, `methodswith` does not fully solve the original question (“search for functions whose first argument can be of a specific type”):

- On the one hand, `methodswith` tells you about functions that have been written with methods that take specific types (or optionally their supertypes) for arguments _in any position_.
- On the other hand, `Any` is excluded from the list of supertypes considered by `methodswith` (otherwise the output would be endless), but for many types there are numberless generic functions that work with them, even without the authors of the type or the function taking care of that.
- Also, the output of `methodswith` is not fixed, but depends on what packages are loaded.

Anyway, Julia is not like Python: methods are not subordinate to types/classes, so I think that it is reasonable that the original question, asked from a Pythonic perspective, does not have a precise answer in Julia.
