# How is = defined? Or is it a non-question in Julia

**URL:** https://discourse.julialang.org/t/how-is-defined-or-is-it-a-non-question-in-julia/6232
**Category:** New to Julia
**Tags:** question
**Created:** [October 4, 2017, 6:12am UTC](https://discourse.julialang.org/t/how-is-defined-or-is-it-a-non-question-in-julia/6232 "2017-10-04T06:12:37Z")
**Posts on this page:** 3
**Page:** 1

<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: [October 4, 2017, 6:12am UTC](https://discourse.julialang.org/t/how-is-defined-or-is-it-a-non-question-in-julia/6232/1 "2017-10-04T06:12:37Z")

</div>

I come from R where you can define methods for assignment e.g.

```r
`[<-.classname` <- function(x, i, j, value) {
  # some code
}

```

later on you can use it like this

```r
class(some_var) <- "classname"
some_var[some_num] <- new_value

```

will call the `[<-.classname` function. I wonder if Julia’s `[` is also defined in such a way?

Consider the following example in Julia

```julia
using DataFrames
df = DataFrame([[1],[2]])
df[:x3] = df[:x1] + df[:x2]

```

How does Julia know that `df[:x3]` means create a new column? I tried to look at at `DataFrames.jl`’s source code but can only find that `DataFrame` is defined as `<: AbstractDataFrame`, so it’s not clear how to define my own `[=` function so that if I define a type `NewType` and newtype is of `NewType`, then can I define the below like I can in R

```julia
newtype[some_thing] = some_value

```

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [October 4, 2017, 6:42am UTC](https://discourse.julialang.org/t/how-is-defined-or-is-it-a-non-question-in-julia/6232/2 "2017-10-04T06:42:50Z")

</div>

Generally assignment `=` cannot be changed, except for index assignment (which is what you’re after, as far as I can tell):

````julia
help?> setindex!                                                                                                                                      
search: setindex! broadcast_setindex! broadcast_getindex                                                                                              
                                                                                                                                                      
  setindex!(A, X, inds...)                                                                                                                            
                                                                                                                                                      
  Store values from array X within some subset of A as specified by inds.                                                                             

  setindex!(collection, value, key...)

  Store the given value at the given key or index within a collection. The syntax a[i,j,...] = x is converted by the compiler to (setindex!(a, x, i,  
  j, ...); x).                                                                                                                                        
                                                                                                                                                      ```
````

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [October 4, 2017, 6:43am UTC](https://discourse.julialang.org/t/how-is-defined-or-is-it-a-non-question-in-julia/6232/3 "2017-10-04T06:43:48Z")

</div>

> [@xiaodai](#):
>
> df[:x3] = df[:x1] + df[:x2]

This becomes `setindex!(df, df[:x1] + df[:x2], :x3)`, so take a look at the `setindex!` method for dataframes. This is not specific to dataframes; in general `A[inds...] = value` calls `setindex!(A, value, inds...)`.
