Skip to content

Coding Templates

Dehann Fourie edited this page Feb 14, 2022 · 15 revisions

Copyright NavAbility (2022) - All rights reserved.

Using Traits

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

Union Splitting

Tim Holy blog post: https://julialang.org/blog/2018/08/union-splitting/

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

Using ::MyType or ::Type{<:MyType}

From style guide docs: Decide whether the concept in question will be written as MyType or MyType(), and stick to it. The preferred style is to use instances by default, and only add methods involving Type{MyType} later if they become necessary to solve some problem.

Stopping An Async Task

tsk = @async sleep(100)
schedule(tsk, InterruptException(), error=true)

Get Local Code Coverage

using Coverage
coverage = process_folder()
LCOV.writefile("lcov.info", coverage)

BSON IOBuffer and Base64

# the object you wish to store as binary
# using Flux
model = Chain(Dense(5,2), Dense(2,3))

io = IOBuffer()

# iob64 = Base64EncodePipe(io)
# using BSON
BSON.@save io model

# close(iob64)

# get base64 binary
# sdata = String(take!(io))

io_ = PipeBuffer(base64decode(sdata))
# io_ = PipeBuffer(take!(io_))

BSON.@load io_ model

Also see: https://github.com/JuliaIO/BSON.jl/issues/64