# (d::Int)(x::Int) = d + x?

**URL:** https://discourse.julialang.org/t/d-int-x-int-d-x/47321
**Category:** Data
**Created:** [September 26, 2020, 3:58pm UTC](https://discourse.julialang.org/t/d-int-x-int-d-x/47321 "2020-09-26T15:58:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bigdataman2015](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@bigdataman2015](https://discourse.julialang.org/u/bigdataman2015)
#### Post date: [September 26, 2020, 3:58pm UTC](https://discourse.julialang.org/t/d-int-x-int-d-x/47321/1 "2020-09-26T15:58:27Z")

</div>

julia\> (d::Int)(x::Int) = d + x # what does this mean ?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [September 26, 2020, 4:05pm UTC](https://discourse.julialang.org/t/d-int-x-int-d-x/47321/2 "2020-09-26T16:05:13Z")

</div>

It defines a method so that you can call a `Int` (i.e. `(d::Int)`) with a single argument of type `Int` (i.e. `x::Int`) that returns the sum of the two.

Don’t define this method.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [September 26, 2020, 4:07pm UTC](https://discourse.julialang.org/t/d-int-x-int-d-x/47321/3 "2020-09-26T16:07:19Z")

</div>

> [@yuyichao](#):
>
> Don’t define this method.

Also my first thought. What is that? That’s problematic and should be deleted.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [September 26, 2020, 4:10pm UTC](https://discourse.julialang.org/t/d-int-x-int-d-x/47321/4 "2020-09-26T16:10:57Z")

</div>

It was in REPL syntax so I hope it either come from doing something random that didn’t error, or from a tutorial demostrating some funny/dangerous things that had a poor explaination.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [September 27, 2020, 2:31am UTC](https://discourse.julialang.org/t/d-int-x-int-d-x/47321/5 "2020-09-27T02:31:50Z")

</div>

> [@bigdataman2015](#):
>
> (d::Int)(x::Int) = d + x

Ppl please correct me if I am wrong, but I think this is called a “functor” but it’s not the same functor as in category theory.

It allows you to use an element of a type as a `callable` i.e. like a function.

You can do this for any type. But it doesn’t really make that much sense for integers. But there is precedent in Ruby where 3.times(fn) runs `fn` 3 times. But in Ruby everything is an object and their implementation causes performance issues IIUC.

```julia
(d::Int)(x::Int) = d + x

```

Basically, it says, any element `d` that is of `Int` type, you can now write `d(x)` and return `d+x` if `x` is also an `Int`.

E.g. `y = 3; y(4) # yields 7`

This kind of things is used in Flux.jl to make `Dense` layers easier to write. It’s almost like a function factory kinda thing.

See my tweet also [https://twitter.com/search?q=julia%20ruby&src=typed\_query](https://twitter.com/search?q=julia%20ruby&src=typed_query)

---

<div class="post-metadata">

### Author: ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)
#### Post date: [September 28, 2020, 11:12am UTC](https://discourse.julialang.org/t/d-int-x-int-d-x/47321/6 "2020-09-28T11:12:12Z")

</div>

> [@xiaodai](#):
>
> Ppl please correct me if I am wrong, but I think this is called a “functor” but it’s not the same functor as in category theory.

Formally speaking this is just a method of Int which than makes Int a functor. It is not functor itself. As mentioned before this is a really bad idea for Int, but implementing method () provide useful syntax for function-like types, e.g. for own implementation of polynomials or different class of parametrisable operations.
