Skip to content

Coding Templates

Dehann Fourie edited this page Aug 2, 2020 · 15 revisions

Copyright NavAbility (2020) - All rights reserved.

Using Traits

https://github.com/JuliaRobotics/IncrementalInference.jl/issues/783#issuecomment-666695221

Deprecation

Deprecate Struct Field

When working with a field of a structure directly (in the Caesar ecosystem). I prefer the dot notation with get/setproperty[!] overloaded if needed.

  • It is easier to read and clearer that you are working with a reference:
    • val1 = mytype.a[1]
    • mytype.a[1] = val1 + 1
    • compared to:
    • vals = getA(mytype)
    • val1 = vals[1]
    • vals[1] = val1 + 1
      • DF, I don't mind that much on the user side, but is slightly easier to modify the package internal code if accessors are used, since old verbNoun can go through a deprecation cycle first. JT - See code below as an example for deprecation cycle.
  • Autocomplete. I'm lazy and can't remember method names.

We do not have to fore one method as deprecation can just as easily be done with get/setproperty[!].

struct MyType
  new::Int
end

Base.propertynames(x::MyType, private::Bool=false) = private ? (:old, :new) : (:new,)

Base.getproperty(x::MyType,f::Symbol) = begin
    if f == :old
      @warn "old is deprecated, use new"
      getfield(x, :new)
    else
      getfield(x,f)
    end
  end

julia> MyType(5).old
┌ Warning: old is deprecated, use new
└ @ Main untitled-69d23f940d4201fb549f03efe37c8103:11
5

Static Parameters

This is bad:

struct Prior{T} <: AbstractPrior where T <: SamplableBelief
  Z::T
end

This is good:

struct Prior{T<: SamplableBelief} <: AbstractPrior
  Z::T
end
Clone this wiki locally