# Passing functions as arguments: confusion with constructors

**URL:** <https://discourse.julialang.org/t/passing-functions-as-arguments-confusion-with-constructors/21832>\
**Category:** General Usage\
**Created:** [March 13, 2019, 6:44pm UTC](https://discourse.julialang.org/t/passing-functions-as-arguments-confusion-with-constructors/21832 "2019-03-13T18:44:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [March 13, 2019, 6:44pm UTC](https://discourse.julialang.org/t/passing-functions-as-arguments-confusion-with-constructors/21832/1 "2019-03-13T18:44:30Z")

</div>

Let’s say I have a type `Foo`, and I would like to pass its constructor as an argument to another function, `bar`, that only takes functions as arguments. How can I distinguish between the type, `Foo`, from its constructor when passing it to `bar`? That is, if `bar` has a signature `bar(f::Function, ...)` how can I call `bar(Foo, ...)` without running into problems? The naive approach seems to throw errors, because `Foo` is also a type.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 13, 2019, 6:53pm UTC](https://discourse.julialang.org/t/passing-functions-as-arguments-confusion-with-constructors/21832/2 "2019-03-13T18:53:34Z")

</div>

Functions are not the only thing that can be called in julia, there are also types and callable objects. The `::Function` argument type is fairly worthless and the best imo is just to do duck typing. Have no argument type and just try to call it, if it can’t be called it will error.

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [March 13, 2019, 7:01pm UTC](https://discourse.julialang.org/t/passing-functions-as-arguments-confusion-with-constructors/21832/3 "2019-03-13T19:01:17Z")

</div>

My actual use case is trying to use `open(f, ...)` with the `CSV.File` constructor, which doesn’t work because `open` is not duck typed ☹.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 13, 2019, 7:06pm UTC](https://discourse.julialang.org/t/passing-functions-as-arguments-confusion-with-constructors/21832/4 "2019-03-13T19:06:17Z")

</div>

Sounds like you should `open` an issue. 🥁

You can always do `f(args...) = CSV.File(args...)` and pass `f`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 13, 2019, 8:14pm UTC](https://discourse.julialang.org/t/passing-functions-as-arguments-confusion-with-constructors/21832/5 "2019-03-13T20:14:52Z")

</div>

Have you tried wrapping it in an anonymous function? As in

```julia
bar(x->Foo(x), ...)

```

or

```julia
bar((args...)->Foo(args...), ...)

```

This is basically the same as @kristoffer.carlsson’s approach, but a bit more compact.

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [March 13, 2019, 8:48pm UTC](https://discourse.julialang.org/t/passing-functions-as-arguments-confusion-with-constructors/21832/6 "2019-03-13T20:48:40Z")

</div>

That’s what I ended up doing, though it turns out that closing the file handle on `CSV.File` is not a good idea.

Too bad Julia doesn’t have interfaces! Are there any plans to add such an idea? It would be great to have some sort of `callable` interface, or not have to resort to duck typing.
