# Specifying Function Contract

**URL:** <https://discourse.julialang.org/t/specifying-function-contract/108983>\
**Category:** Internals & Design\
**Created:** [January 18, 2024, 8:24pm UTC](https://discourse.julialang.org/t/specifying-function-contract/108983 "2024-01-18T20:24:57Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [January 19, 2024, 8:40pm UTC](https://discourse.julialang.org/t/specifying-function-contract/108983/4 "2024-01-19T20:40:03Z")

</div>

This is an interesting take. To me it appears related to the distinction between _parametric_ and _ad-hoc_ polymorphism in Haskell:

- A generic function is _parametric polymorphic_ if it works with arguments of different types (originally all types, but extended to allow restrictions to certain type classes). Here is an example similar to yours:

- A generic function is _ad-hoc polymorphic_ if it’s part of a type-class – which are rather similar to interfaces or traits. In this case, it can have **multiple implementations** for every concrete type implementing the type class.

In Julia the situation is considerably more complicated as every function can be written in a generic way, i.e., work for any type adhering to some interface of used methods, as well as being overloaded for different types. In this respect, generic functions could be considered as parametric and ad-hoc polymorphic at the same time. According to this view, your `sum` example defines a parametric polymorphic function (Imho, the requirement of an `iterate` method should not be part of it as it’s an implementation detail of `reduce` – which might or might not actually require it).

Overall, I’m not even sure how to best describe the flexible approach of Julia in a type system at all. Not even considering multiple dispatch which further complicates the situation.

* * *

1. As Haskell has higher-kinded types, the container type `t` itself can be parametric. Thus, the element type does not need to associated with container, e.g., via `eltype`, but it’s defined via `t a`.

---

_[View the full topic](https://discourse.julialang.org/t/specifying-function-contract/108983)._
