# Parametric methods

**URL:** <https://discourse.julialang.org/t/parametric-methods/108496>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 8, 2024, 3:14am UTC](https://discourse.julialang.org/t/parametric-methods/108496 "2024-01-08T03:14:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 8, 2024, 3:14am UTC](https://discourse.julialang.org/t/parametric-methods/108496/1 "2024-01-08T03:14:29Z")

</div>

I have just read the “parametric methods” part of the julia docs:

[https://docs.julialang.org/en/v1/manual/methods/#Parametric-Methods](https://docs.julialang.org/en/v1/manual/methods/#Parametric-Methods)

Is it possible to parametrize the value (but not the type) of arguments of a function?

For example, suppose we want to calculate the nth power of x, and we want to pass the parameter n using the ‘where’ keyword. Is that possible?

Of course, you can say, it is easy with the more conventional way:

```julia
function f(x , n )
    return x^n 
end 

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 8, 2024, 3:18am UTC](https://discourse.julialang.org/t/parametric-methods/108496/2 "2024-01-08T03:18:11Z")

</div>

It’s unclear to me what that means. Can you elaborate?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 8, 2024, 3:55am UTC](https://discourse.julialang.org/t/parametric-methods/108496/3 "2024-01-08T03:55:51Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> Is it possible to parametrize the value (but not the type) of arguments of a function?

You can dispatch on argument values by moving values into the type domain — that is, you wrap the value in a type. A generic type that is provided for this purpose is [`Val`](https://docs.julialang.org/en/v1/base/base/#Base.Val).

> [@jiang\_ming\_zhang](#):
>
> For example, suppose we want to calculate the nth power of x, and we want to pass the parameter n using the ‘where’ keyword.

This is exactly what is done by the [`literal_pow` function](https://github.com/JuliaLang/julia/blob/8dc2c30f1660f297e15502b9001a1a793c277cb7/base/intfuncs.jl#L367-L372), which gets called when you write [`x^n` where `n` is a literal constant](https://docs.julialang.org/en/v1/base/math/#Base.:%5E-Tuple%7BNumber,%20Number%7D).

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 8, 2024, 4:03am UTC](https://discourse.julialang.org/t/parametric-methods/108496/4 "2024-01-08T04:03:51Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> pass the parameter n using the ‘where’ keyword

Parameters are always involved in the type domain, even when they’re not types. If you go beyond type-based polymorphism, you start losing the distinction between types and their values.

> [@stevengj](#):
>
> A generic type that is provided for this purpose is [`Val`](https://docs.julialang.org/en/v1/base/base/#Base.Val).

A rewrite of the original example could look like this:

```julia
f(x, n::Number) = f(x, Val(n))

f(x, ::Val{N}) where N = x^N

```

I really suggest that you don’t use methods like the first method on a regular basis, `literal_pow` is only used because powers of integer literals can lower to it and leverage constant optimizations. If you use it on a variable whose runtime value can vary without any restrictions, then you’re compiling the second method repeatedly. That can easily ruin any performance benefits.
