This repository contains some examples and code snippets. Tested and developed with Julia version 1.12.0-rc3 and:
Cthulhu@v2.17.8
JET@v0.10.7
JuliaSyntax@v1.0.2
Specific code issues:
(vec...,)
- this operation requires an awkward
for
-loop code to--trim
- often causes downstream inference issues due to
Tuple{Vararg{...}}
- this operation requires an awkward
[vec_a..., vec_b...]
- this is "Vector{Int}" all the way down, but poor
apply_iterate
support breaks this (takeaway: only ever splat Tuples, not Vectors) - JuliaLang/julia#57830
- this is "Vector{Int}" all the way down, but poor
return (; attributes...)
- very easy for this to become type-unstable when considering multiple
return
values - best to explicitly write the
@NamedTuple{...}
type you want to return and use that (similar to an anonymous struct)
- very easy for this to become type-unstable when considering multiple
eval(...)
(CPUSummary.jl / HostCPUFeatures.jl are the notable examples in the community)- CPUSummary.jl has been fixed already, HostCPUFeatures.jl has not
Common patterns that cause issues:
- Returning different types based on a runtime value
- De-serializing data
- if it can be done at top-level, this is a "compile-time" operation and (mostly) trimmable for free!
- be careful about using heavy de-serialization libraries, since the binaries will ship with your
--trim
code
- Manual specialization required
foo(f::F) where {F <: Function}
(versusfoo(f::Function)
)foo(::Type{T}) where T
(versusfoo(T)
)
- (Advanced) Manual de-specialization required
- a function that only constructs an object (w/ no type-parameters)
- a function that constructs a type and then compares it using other polymorphic operations (e.g.
===
/!==
/isa
)
Good patterns:
- Returning a
Const(...)
value (Tuple / Integer / Symbol / Type) based on a value's type- this is the "Tim Holy Trait trick"
- Make all struct fields "concretely-typed" (i.e. parameterized or a specific concrete type)
Good "natural-feeling" examples:
- TOML stdlib, before
--trim
fixes (JuliaLang/julia#55016)