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



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

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

later on you can use it like this

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

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

newtype[some_thing] = some_value


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

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).                                                                                                                                        


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...).