# Designing APIs. defining new methods versus passing in functions

**URL:** https://discourse.julialang.org/t/designing-apis-defining-new-methods-versus-passing-in-functions/18699
**Category:** General Usage
**Tags:** design
**Created:** [December 15, 2018, 9:00pm UTC](https://discourse.julialang.org/t/designing-apis-defining-new-methods-versus-passing-in-functions/18699 "2018-12-15T21:00:33Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)
#### Post date: [December 16, 2018, 12:08am UTC](https://discourse.julialang.org/t/designing-apis-defining-new-methods-versus-passing-in-functions/18699/3 "2018-12-16T00:08:20Z")

</div>

I don’t have a concrete answer to your question about benefits, but I do fear that my example didn’t illustrate what I actually had in mind. So if I may try again.

What business do you have trying to sort a “thing” that doesn’t have `isless` defined on it? Shouldn’t it just have an `isless` method? If it has the wrong `isless`method defined on it, then you can introduce a new thing that has the desired behavior. That’s what the wrapper was meant to illustrate.

The arguments to the sort function feel to me out of place, actually (I know there is a history in other languages of sorting functions taking in these kinds of arguments). Why doesn’t the `maximum` function take in an `lt` argument too? Or for that matter, any function that calls `isless`, why doesn’t that function take an `lt` argument? That’s what the first API would do.

Once you define `isless` on your type (your thing), then you can use `min`, `minimum`, `sort`, you can use the lexicographic ordering induced by `isless` on `NTuple{Thing}`, and so forth. (So, here’s a benefit of the second API)

I understand that `sort` has this interface almost as a convenience. Sometimes you want to introduce a very context-specific notion of `isless` that you don’t want anywhere else in your program. And it could be a pain to define a new type with the appropriate behavior. (This is a benefit of the first API)

On the other hand, defining new types with new methods is favored in other contexts, e.g. defining new `Number` types with different arithmetic behavior.

---

_[View the full topic](https://discourse.julialang.org/t/designing-apis-defining-new-methods-versus-passing-in-functions/18699)._
