# Provide an implementation of f \*only\* if g is available

**URL:** https://discourse.julialang.org/t/provide-an-implementation-of-f-only-if-g-is-available/931
**Category:** Offtopic
**Tags:** question
**Created:** [December 14, 2016, 4:50am UTC](https://discourse.julialang.org/t/provide-an-implementation-of-f-only-if-g-is-available/931 "2016-12-14T04:50:53Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![zsunberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsunberg/32/1883_2.png) [@zsunberg](https://discourse.julialang.org/u/zsunberg)
#### Post date: [December 14, 2016, 4:50am UTC](https://discourse.julialang.org/t/provide-an-implementation-of-f-only-if-g-is-available/931/1 "2016-12-14T04:50:53Z")

</div>

Hi,

The interface to my package includes two functions, `f`, and `g` that can be implemented by users for their types. In the package, `f` and `g` are empty generic functions, so `MethodError`s will be thrown if they are not implemented for the user’s type. `f` is part of the _core_ interface, and, if `f` is implemented, there is an easy way to construct `g`:

```julia
function g(x)
    y = f(x)
    return modify(y)
end

```

However, some functionality of the package only requires `g`, and `f` is essentially impossible to implement for some user types while `g` (skipping the intermediate `y`) is easy, so I want the user to only have to implement `g` if they only want to use that part of the package. So my question is **is there a way to provide a default implementation of `g` using `f`only when `f` is available?**

If you want details, the actual use case is `POMDPs.jl` and `GenerativeModels.jl` where `f` is `transition` and `g` is `generate_s`.

## What I have tried so far

I have tried using generated functions and SimpleTraits.jl, but they both have the side effect of making julia think that there is a method of `g` for all types.

What I would like to do is this

```julia
@generated function g(x)
    if method_exists(f, Tuple{x})
        return quote
            y = f(x)
            return modify(y)
        end
    else
        # don't return anything and leave g without a method for typeof(x)
    end
end

```

The reason I _don’t_ want Julia to think there are methods available is because I want to be able to use `method_exists` to print out a nice table of the functions that the users still need to implement to use the package.

The users of my package are usually non-CS engineers new to Julia, are not very experienced with object-oriented programming, and I’m trying to show them how powerful the language is in addition to introducing them to the package, so I’m trying to keep things as simple as possible.

---

<div class="post-metadata">

### Author: ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)
#### Post date: [December 14, 2016, 9:02am UTC](https://discourse.julialang.org/t/provide-an-implementation-of-f-only-if-g-is-available/931/2 "2016-12-14T09:02:06Z")

</div>

Why can’t you simply implement

```julia
function f(args...)
    # analyse the types in `args...`
    error("""
         `f(::T1, ::T2, ...)` is not implemented.
          . . . some instructions on what to do . . . 
      """)
end

```

Any new implementation of `g` (or `f`) with more specific types will anyhow take precedence?

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [December 14, 2016, 6:15pm UTC](https://discourse.julialang.org/t/provide-an-implementation-of-f-only-if-g-is-available/931/3 "2016-12-14T18:15:37Z")

</div>

> [@zsunberg](#):
>
> The reason I don’t want Julia to think there are methods available is because I want to be able to use method\_exists to print out a nice table of the functions that the users still need to implement to use the package.

To build on cortner’s answer, in addition to providing the runtime failure case with an informative error (rather than MethodError), you could use `which() != the_error_method` instead of `method_exists` to build the table of which methods still need to be overridden.

---

<div class="post-metadata">

### Author: ![zsunberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsunberg/32/1883_2.png) [@zsunberg](https://discourse.julialang.org/u/zsunberg)
#### Post date: [December 14, 2016, 6:19pm UTC](https://discourse.julialang.org/t/provide-an-implementation-of-f-only-if-g-is-available/931/4 "2016-12-14T18:19:06Z")

</div>

@jameson, thanks I was literally just typing `?which` to try to figure out how to do that, haha

---

<div class="post-metadata">

### Author: ![zsunberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsunberg/32/1883_2.png) [@zsunberg](https://discourse.julialang.org/u/zsunberg)
#### Post date: [December 14, 2016, 6:28pm UTC](https://discourse.julialang.org/t/provide-an-implementation-of-f-only-if-g-is-available/931/5 "2016-12-14T18:28:11Z")

</div>

@cortner, thanks for the reply! That’s actually what we have been doing. There are a few problems with this

1. It suppresses or messes up the very useful `MethodErrors` that give similar methods for debugging. Of course we can throw one, but then the method that throws the error is included in the list of similar methods
2. There is now a method of f for the types so `method_exists` returns true even though the user has not implemented it.
3. It is not so easy to give instructions on what to do. As I said above, `f` is nearly impossible to implement in some cases. If I just say `implement f`, the user will likely decide that this package is not suitable for him and miss out on the package.

@jameson’s reply below addresses 2, so maybe I can get it to work

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [December 14, 2016, 7:35pm UTC](https://discourse.julialang.org/t/provide-an-implementation-of-f-only-if-g-is-available/931/6 "2016-12-14T19:35:33Z")

</div>

> [@zsunberg](#):
>
> It suppresses or messes up the very useful MethodErrors that give similar methods for debugging. Of course we can throw one, but then the method that throws the error is included in the list of similar methods

On the other hand, you can potentially directly throw a useful error message (“your solver `T` doesn’t support method `f`”), rather than falling back on the MethodError heuristics.

---

<div class="post-metadata">

### Author: ![zsunberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zsunberg/32/1883_2.png) [@zsunberg](https://discourse.julialang.org/u/zsunberg)
#### Post date: [December 15, 2016, 12:25am UTC](https://discourse.julialang.org/t/provide-an-implementation-of-f-only-if-g-is-available/931/7 "2016-12-15T00:25:58Z")

</div>

@jameson, what do you think is the most robust way to see if the method returned by which() is my default definition? Say `m` is the method returned by `which()`. Do you think I should look at

1. `m.module` (the default implementation is the only one in the module)  
or
2. `functionloc(m)`  
or
3. compare to the actual default implementation method with `!=`

In case 3 I’m not sure how I would get a reference to the default implementation method.

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [December 16, 2016, 3:52pm UTC](https://discourse.julialang.org/t/provide-an-implementation-of-f-only-if-g-is-available/931/8 "2016-12-16T15:52:16Z")

</div>

For 3, you could look up the default method by again using `which` (and probably cache it somewhere). Option 1 sounds simplest though.
