Difference between get(), getproperty(), and getfield()
Difference between get(), getproperty(), and getfield()
get
- Scope:Generally used to retrieve values from indexable data structures (such as
Dict
,Array
) or other containers with customizable interfaces. - Function:
get(collection, key, default)
will attempt to retrieve the value forkey
fromcollection
. 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.
- Scope:Generally used to retrieve values from indexable data structures (such as
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 objectobj
, Julia will callgetproperty(obj, :someproperty)
to get the property. You can also define/override theBase.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
.
- Scope:This is the underlying mechanism for attribute access* in Julia, which is mainly related to dot notation syntax (
getfield
- Scope:This is the function for directly accessing the fields of a structure in Julia.
- Function:
getfield(obj, :fieldname)
is low-level access to a field of a structure and does not trigger the overriding logic ofgetproperty
. 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 possiblegetproperty
overloads or when you explicitly want to access the fields of the underlying structure directly. For example, in a custom implementation ofgetproperty
,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 customizinggetproperty
.
This post is licensed under CC BY 4.0 by the author.