Post

Difference between get(), getproperty(), and getfield()

Difference between get(), getproperty(), and getfield()
  1. get
    • Scope:Generally used to retrieve values from indexable data structures (such as Dict, Array) or other containers with customizable interfaces.
    • Functionget(collection, key, default) will attempt to retrieve the value for key from collection. If the value is not found, default will be returned.
    • Example
      1
      2
      3
      
      d = Dict(:a => 1, :b => 2)
      println(get(d, :a, 0))  # 1
      println(get(d, :c, 0))  # 0 (Due to :c not existing)
      
    • Application:When there is no guarantee that a key will always exist, you can gracefully provide a default value and avoid throwing an error.
  2. getproperty
    • Scope:This is the underlying mechanism for attribute access* in Julia, which is mainly related to dot notation syntax (.).
    • Function:When we use the obj.someproperty syntax on an object obj, Julia will call getproperty(obj, :someproperty) to get the property. You can also define/override the Base.getproperty method in your custom types to execute some custom logic when accessing properties (such as lazy loading, redirecting to other fields, calculating virtual properties, etc.).
    • Example
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      
      struct MyType
          x::Int
      end
      
      function Base.getproperty(m::MyType, sym::Symbol)
          if sym === :y
              return m.x + 1
          else
              return getfield(m, sym)  # fallback to default field access
          end
      end
      
      m = MyType(10)
      println(m.x)  # Actual call getproperty(m, :x) -> getfield(m, :x)
      println(m.y)  # Actual call getproperty(m, :y) -> Custom logic returns 11
      
    • Application:Intercept and dynamically handle access to the properties of structures/objects. For example, properties that are “calculated” rather than actually stored can be made to be accessed like normal fields by overriding getproperty.
  3. getfield
    • Scope:This is the function for directly accessing the fields of a structure in Julia.
    • Functiongetfield(obj, :fieldname) is low-level access to a field of a structure and does not trigger the overriding logic of getproperty. In other words, it just “gets the actual field in the object” and does not execute additional custom methods.
    • Example
      1
      2
      3
      4
      5
      6
      
      struct MyType
          x::Int
      end
      
      m = MyType(10)
      println(getfield(m, :x))  # 10
      
    • Application:Use getfield when you need to bypass any possible getproperty overloads or when you explicitly want to access the fields of the underlying structure directly. For example, in a custom implementation of getproperty, getfield is often used to get the actual field value.

Summary

  • get is a high-level “safe” getter function for data structures that provides default values to handle missing keys.
  • getproperty is the mechanism behind the dot syntax (.), allowing users to customize property access behavior through overloading.
  • getfield is the lowest-level function that directly accesses the fields of a structure without any overloading. It is generally only explicitly called when writing low-level code or customizing getproperty.
This post is licensed under CC BY 4.0 by the author.