# Is there way to do math on type parameters for function defintions

**URL:** <https://discourse.julialang.org/t/is-there-way-to-do-math-on-type-parameters-for-function-defintions/89400>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 27, 2022, 8:51pm UTC](https://discourse.julialang.org/t/is-there-way-to-do-math-on-type-parameters-for-function-defintions/89400 "2022-10-27T20:51:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [October 27, 2022, 8:51pm UTC](https://discourse.julialang.org/t/is-there-way-to-do-math-on-type-parameters-for-function-defintions/89400/1 "2022-10-27T20:51:19Z")

</div>

I would like to do something like this,

```julia
pt_to_line_dist(x::SVector{N-1},l::SVector{N}) where N <: UInt = print("hi")

```

Is it possible?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 27, 2022, 9:14pm UTC](https://discourse.julialang.org/t/is-there-way-to-do-math-on-type-parameters-for-function-defintions/89400/2 "2022-10-27T21:14:58Z")

</div>

no

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [October 27, 2022, 9:16pm UTC](https://discourse.julialang.org/t/is-there-way-to-do-math-on-type-parameters-for-function-defintions/89400/3 "2022-10-27T21:16:53Z")

</div>

Math on type parameters is not possible. Also, your `N <: UInt` will not do what you expect. In this case, it’s likely that `N isa Int` (not `isa UInt` and definitely not `<:UInt`, which could only apply to a type rather than value). Note that `isa` is not something you can do with type parameters anyway.

You could implement this as

```julia
function pt_to_line_dist(x::SVector{N},l::SVector{M}) where {N,M}
    if N == M-1
        print("hi")
    else
        # do other stuff or throw an error
    end
end

```

Although in this case I wouldn’t bother with type parameters at all

```julia
function pt_to_line_dist(x::AbstractVector,l::AbstractVector)
    if length(x) == length(l)-1
        print("hi")
    else
        # do other stuff or throw an error
    end
end

```

When given `SVector`s for inputs, `length` will evaluate to a compile-time constant and the `if`/`else` block will get compiled away, leaving only the correct branch. When given non-static vectors, the code will still work but it will have to check at run-time which branch to use.
